Hello,
I have 2 questions on using PowerCLI to edit vSphere Host Profiles:
1) How can I clone a host profile, by using PowerCLI ?
2) How can I modify Host profiles, by using PowerCLI, to update these settings ?
a) Networking configuration - DNS configuration - Host name
b) Authentication configuration - Active Directory configuration - Domain Name
Thanks!
Regards,
Harold
Your code used to work to change the password in the host profile, did something change on the VMware side?
This code errors $spec.ApplyProfile.Security.Policy = @($secpol)
The property 'Policy' cannot be found on this object. Verify that the property exists and can be set.
At C:\VirtualCenterScripts\ESXi_Password_Reset\Everything_Root\ESXi_Password_Reset_StagingProfiles.ps1:67 char:41
+ $spec.ApplyProfile.Security.Policy = @($secpol)
+ ~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
VMware Cis Core PowerCLI Component PowerCLI Component 11.0 build 10335701
vSphere 6.5 U1
VMware VimAutomation VICore Commands PowerCLI Component PowerCLI Component 11.0 build 10336080
VMWare ImageBuilder PowerCLI Component 6.7 build 8250345
VMWare AutoDeploy PowerCLI Component 6.7 build 8250345
VMware VimAutomation Srm PowerCLI Component PowerCLI Component 10.0 build 7893900
VMware VimAutomation License PowerCLI Component PowerCLI Component 10.0 build 7893904
VMware VimAutomation Vds Commands PowerCLI Component PowerCLI Component 11.0 build 10336077
VMware Vmc PowerCLI Component PowerCLI Component 11.0 build 10336076
VMware Nsxt PowerCLI Component PowerCLI Component 11.0 build 10364044
VMware VimAutomation vROps PowerCLI Component PowerCLI Component 10.0 build 7893921
VMware HorizonView PowerCLI Component 7.1.0 build 10230451
VMware VimAutomation Cloud PowerCLI Component PowerCLI Component 11.0 build 10379994
VMware VimAutomation Storage PowerCLI Component PowerCLI Component 11.0 build 10380343
VMware vSphere Update Manager PowerCLI 6.5 build 7862888
VMware VimAutomation Security PowerCLI Component PowerCLI Component 11.0 build 10380515
Looks like you might be using a HostProfile that doesn't have the 'root' entry.
But this version of the function creates an empty object in that case.
Give it a try.
function Set-VMHostProfileExtended{
<#
.SYNOPSIS Update the root password in a Host Profile
.DESCRIPTION The function will update the root password in
a Host Profile.
.NOTES Author: Luc Dekens
.PARAMETER Profile
The Host Profile for which you want to change the root
password. You can pass the name of the Host Profile
or the VMHostProfile object
.PARAMETER AdminPassword
The new root password.
.EXAMPLE
PS> $prof = Get-VMHostProfile -Name MyProfile
PS> Set-VMHostProfileExtended -Profile $prof -AdminPassword "abc"
#>
param(
[CmdletBinding()]
[parameter(Mandatory = $true, ValueFromPipeline = $true)]
[PSObject]$Profile,
[string]$AdminPassword
)
begin{
function Copy-Property ($From, $To, $PropertyName ="*")
{
foreach ($p in Get-Member -In $From -MemberType Property -Name $propertyName)
{ trap {
Add-Member -In $To -MemberType NoteProperty -Name $p.Name -Value $From.$($p.Name) -Force
continue
}
$To.$($P.Name) = $From.$($P.Name)
}
}
}
process{
if($Profile.GetType().Name -eq "string"){
$Profile = Get-VMHostProfile -Name $Profile -Server $defaultVIServers[0]
}
$spec = New-Object VMware.Vim.HostProfileCompleteConfigSpec
Copy-Property -From $Profile.ExtensionData.Config -To $spec
$secpol = New-Object VMware.Vim.ProfilePolicy
$secpol.Id = "AdminPasswordPolicy"
$secpol.PolicyOption = New-Object VMware.Vim.PolicyOption
$secpol.PolicyOption.Id = "FixedAdminPasswordOption"
$secpol.PolicyOption.Parameter += New-Object VMware.Vim.KeyAnyValue
$secpol.PolicyOption.Parameter[0].Key = "password"
$secpol.PolicyOption.Parameter[0].Value = New-Object VMware.Vim.PasswordField
$secpol.PolicyOption.Parameter[0].Value.Value = $AdminPassword
if($spec.ApplyProfile.Security -eq $null){
$spec.ApplyProfile.Security = New-Object VMware.Vim.SecurityProfile
}
$spec.ApplyProfile.Security.Policy = @($secpol)
$Profile.ExtensionData.UpdateHostProfile($spec)
Get-VMHostProfile -Name $Profile.Name
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference