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.
functionSet-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{
functionCopy-Property ($From,$To,$PropertyName="*")
{
foreach ($pinGet-Member-In$From-MemberTypeProperty-Name$propertyName)
{ trap {
Add-Member-In$To-MemberTypeNoteProperty-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-ObjectVMware.Vim.HostProfileCompleteConfigSpec
Copy-Property-From$Profile.ExtensionData.Config -To$spec
$secpol=New-ObjectVMware.Vim.ProfilePolicy
$secpol.Id ="AdminPasswordPolicy"
$secpol.PolicyOption =New-ObjectVMware.Vim.PolicyOption
$secpol.PolicyOption.Id ="FixedAdminPasswordOption"
$secpol.PolicyOption.Parameter +=New-ObjectVMware.Vim.KeyAnyValue
$secpol.PolicyOption.Parameter[0].Key ="password"
$secpol.PolicyOption.Parameter[0].Value =New-ObjectVMware.Vim.PasswordField
$secpol.PolicyOption.Parameter[0].Value.Value =$AdminPassword
if($spec.ApplyProfile.Security -eq$null){
$spec.ApplyProfile.Security =New-ObjectVMware.Vim.SecurityProfile
}
$spec.ApplyProfile.Security.Policy = @($secpol)
$Profile.ExtensionData.UpdateHostProfile($spec)
Get-VMHostProfile-Name$Profile.Name
}
}