VMware Cloud Community
lucasreginato
Contributor
Contributor

How to set the ESX Host hostname and root password using Power CLI commandlets ?

Hello,

These are the versions that I am using:

    PowerCLI 5.8 Release1,

    Auto Deploy 5.5 U2,

    vCenter 5.5.0 Build 2646482 ,

    ESX image VMware-ESXi-5.5.0-Update2-2403361-HP-550.9.2.27-Mar2015-depot.

And I would like to know the best practice to set the hostname and the root password for a new ESX Host using the Power CLI commandlets.


I already searched the forums, the internet and I have found a lof of different ways, such as:

1) use the apply-VmHostProfile cmd to set the root password and the hostname.

With this approach I saw codes such as this:

    $configuration = @{}

    # Hostname

    $configuration['network.GenericNetStackInstanceProfile["key-vim-profile-host-GenericNetStackInstanceProfile-defaultTcpipStack"].GenericDnsConfigProfile.HostNamePolicy.hostName'] = $host_name

    # Apply-VMHostProfile commandlet

    $ret = Apply-VMHostProfile -Entity $host_name -Profile $host_profile -Variable $configuration -confirm:$false

I tried to use this code, but the power cli or the host does not accept the hostname key and value.

2) use other Power CLI cmds to set the root password and hostname, such as these:

    set-vmhostaccount -useraccount $user1 -password  "$newpass" -confirm:([System.convert]::toBoolean(''false''))'

    set-vmhostadvancedconfiguration -name "Misc.PreferredHostName" -value "$fqdn"


I did not tried this yet, but is this the best practice?

3) Using PolicyOptions such as:

    # Setting the Admin Password in the Host Profile
    $newAdminPswd="MyPassword"
    $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 =$newAdminPswd
    $spec.ApplyProfile.Security.Policy = @($secpol)# Setting the DNS Host Name setting in the Host Profile
    $nwpol=New-ObjectVMware.Vim.ProfilePolicy
    $nwpol.Id ="HostNamePolicy"
    $nwpol.PolicyOption =New-ObjectVMware.Vim.PolicyOption
    $nwpol.PolicyOption.Id ="UserInputHostName"
    $spec.applyprofile.network.dnsconfig.policy = @($nwpol)# Setting the Domain Name and OU setting in the Host Profile


But how am I supposed to know the named of the PolicyOptions?

The ids, the parameters, the keys?


I would really appreciate any help here.


Thanks,


-Lucas Reginato

3 Replies
LucD
Leadership
Leadership

As you discovered, there are indeed several ways to change the root password.

Some remarks:

1) Use a VMHostProfile works afaik.

See the 2nd method in my Change the root password in hosts and Host Profiles post.

Update the HostProfile and then apply through the Invoke-VMHostProfile cmdlet.

2) The Set-VMHostAccount cmdlet is in my opinion the simplest and cleanest solution.

Make sure you are connected to the ESXi server, not the vCenter.

3) Is in principle the same as 1).

You are just using the direct API call instead of the Invoke-VMHostProfile cmdlet

It is a bit cumbersome to discover the properties in a VMHost profile that you need to use, but that is where blogs and community threads come in handy.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
lucasreginato
Contributor
Contributor

Hi LucD,

Thanks for the answer.

Yes, these forums are very handy.

My code is using the "Set-VmHostAccount" cmdlet (which is not working today) because the Power CLI cannot connect to the new ESX Host.

Maye because the port 443 is not open in the ESX host?

For the hostname I was not able to execute the "Set-VmHostAdvancedConfiguration" cmdlet too because of the previous error (not able to connect to the ESX Host).

I will use these commands in my initial tests, for sure.

But reading the VMWare documentation they recommend to use Host Profiles to set the root password and the hostname to the ESX Host.

I mean, they recommend to use ProfilePolicy and ProfileOption.

But how to get all these keys and ids from ProfilePolicy and ProfileOptrion and use them with Host Profiles?

As an example, we have this key that could be used as part of the Apply-VmHostProfile cmdlet in the "-Variable" parameter.

['network.GenericNetStackInstanceProfile["key-vim-profile-host-GenericNetStackInstanceProfile-defaultTcpipStack"].GenericDnsConfigProfile.HostNamePolicy.hostName']

Where this key comes from?

I mean, where I can find more details about this key in the VMWare or Power CLI documentation?

I was not able to find it.

Also, where I can find more details about the ProfilePolicy "AdminPasswordPolicy"? "HostNamePolicy"?

Thanks,

-Lucas Reginato

0 Kudos
LucD
Leadership
Leadership

When you apply a hostprofile, you will get back an object that contains a property AdditionConfiguratuin,

In that table you will find the properties that need to be set.

Something like this

$profileName = 'Profile1'

$esxName = 'MyESx'

$esx = Get-VMHost -Name $esxName

if($esx.ConnectionState -eq 'Maintenance'){

    $conf = Invoke-VMHostProfile -Profile $profileName -Entity $esx -ApplyOnly -Confirm:$false

    $conf.GetEnumerator()| select Name

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference