internetrush1
Contributor
Contributor

Changing VMHost name from powercli

Jump to solution

Is there a way to change the hostname and DNS settings from powercli?

Tags (4)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

The following script can be used to rename an ESXi that is in a cluster

$vcenterName = "MyvCenter"
$clusterName = "MyCluster"
$esxName = "oldname.mydomain"
$newName = "newname.mydomain"
$esxUser = "root"
$esxPswd = "password"

Connect-VIServer -Server $vcenterName
Get-VMHost -Name $esxName | Set-VMHost -State Disconnected -Confirm:$false |
Remove-VMHost -Confirm:$false

Connect-VIServer -Server $esxName -User $esxUser -Password $esxPswd
$esxcli = Get-EsxCli -VMHost $esxName
$esxcli.system.hostname.set($null,$newName,$null)
Disconnect-VIServer -Server $esxName -Confirm:$false

Connect-VIServer -Server $vcenterName
$cluster = Get-Cluster -Name $clusterName
Add-VMHost -Name $newName -Location $cluster -User $esxUser -Password $esxPswd -Force -Confirm:$false

If the ESXi host you want to rename is not part of cluster, you can just change the value on the Location parameter in the last line, to for example a datacenter.


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

View solution in original post

18 Replies
LucD
Leadership
Leadership

The following script can be used to rename an ESXi that is in a cluster

$vcenterName = "MyvCenter"
$clusterName = "MyCluster"
$esxName = "oldname.mydomain"
$newName = "newname.mydomain"
$esxUser = "root"
$esxPswd = "password"

Connect-VIServer -Server $vcenterName
Get-VMHost -Name $esxName | Set-VMHost -State Disconnected -Confirm:$false |
Remove-VMHost -Confirm:$false

Connect-VIServer -Server $esxName -User $esxUser -Password $esxPswd
$esxcli = Get-EsxCli -VMHost $esxName
$esxcli.system.hostname.set($null,$newName,$null)
Disconnect-VIServer -Server $esxName -Confirm:$false

Connect-VIServer -Server $vcenterName
$cluster = Get-Cluster -Name $clusterName
Add-VMHost -Name $newName -Location $cluster -User $esxUser -Password $esxPswd -Force -Confirm:$false

If the ESXi host you want to rename is not part of cluster, you can just change the value on the Location parameter in the last line, to for example a datacenter.


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

internetrush1
Contributor
Contributor

Awesome, ok. Well thats a bit more work than i had hoped for, but if its got to be done... Smiley Happy

Thanks for the answer!    

0 Kudos
LucD
Leadership
Leadership

Yes, the script is not too short, but it follows all the required steps as documented in KB1010821


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

internetrush1
Contributor
Contributor

As always you've been very helpful. Thanks again!    

0 Kudos
LucD
Leadership
Leadership

I created a full function of the rename, see Rename an ESXi 5.x host


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

0 Kudos
internetrush1
Contributor
Contributor

Hey LucD

The script you provided is helpful; however, the -Location parameter is a bit buggy. Its throwing errors at me even though i think im passing the right items.


Also, if you leave it blank it fails. From your description it seems to be an optional parameter?

Thanks!   


PS :

When i get-cluster against the host this is what i get: VMware.VimAutomation.ViCore.Impl.V1.Inventory.FolderImpl

0 Kudos
LucD
Leadership
Leadership

It is indeed an optional parameter.

Which errors do you get ?


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

0 Kudos
internetrush1
Contributor
Contributor

if($Location -isnot [VMware.VimAutomation.ViCore.Impl.V1.Inventory.ClusterImpl] -and

  $Location -isnot [VMware.VimAutomation.ViCore.Impl.V1.Inventory.DatacenterImpl]){

  Write-Error "Location $($Location.Name) must be a datacenter or a cluster"

  return

  }

Basically the write-error always happens even when i get-cluster $vmhost and pass that into location.

like i said above: "When i get-cluster against the host this is what i get: VMware.VimAutomation.ViCore.Impl.V1.Inventory.FolderImpl"

its not either inventory.clusterimpl or datacenterimpl.

0 Kudos
LucD
Leadership
Leadership

That is strange, when I do

Get-VMHost MyEsx | Get-Cluster

I do get a ClusterImpl object back.

How do you do the Get-Cluster ?


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

0 Kudos
internetrush1
Contributor
Contributor

$vmhost = get-vmhost -name "MyVmHost"
$Cluster = $VMhost | get-cluster

I'm currently debugging a much larger script in Sapien Powershell ISE / powershell 2.0 and the latest powerCLI release.

In the middle of the code, when its performing the check -isnot the location is a folderimpl. I've tried removing the [psobject] on the parameter pass with no success.

I'm going to comment out that piece tomorrow to see if it helps.

EDIT:

I just realized that im only connected to the host (not VCenter) when doing the get-cluster. its probably that it cannot connect with VCenter so its pulling something funny from the host.

Will attempt to pull the Cluster earlier in the code so that when i disconnect from everything and connect to the host only i get the accurate results.

0 Kudos
LucD
Leadership
Leadership

That explains it, the cluster concept is only available on the vCenter.


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

internetrush1
Contributor
Contributor

Yep, works now. Lots of trouble in this.

I'm attempting to basically rename all the hosts / add to active directory in VCenter to a new domain name. Unfortuantely we use the domain in the hostname, so its causing numerous problems.    

0 Kudos
internetrush1
Contributor
Contributor

Hey LucD

This piece is actually used in a larger script attempting to set the domain of the server. Can i use the command:

$esxcli.system.hostname.set($null,$NewName,$null)

to set the domain as well?

Finally, is it possible to change the SEARCH DOMAINS under DNS and Routing via PowerCLI?

Thanks!

0 Kudos
LucD
Leadership
Leadership

Do you mean changing the domain or joining the domain ?

Changing the domain can be done this way.

Yes, with

$esxcli.network.ip.dns.search

you have the list, add and remove methods to manipulate the DNS Search domains.

And with

$esxcli.network.ip.route.ipv4
$esxcli.network.ip.route.ipv6

you also have the list, add and remove methods to manipulate the routing configuration.


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

0 Kudos
internetrush1
Contributor
Contributor

Sorry, we are moving into active directory. I need to do a few things.

Enable all active directory services

Change the server name

Join the domain

Change the SEARCH DOMAINS (under DNS and Routing in the GUI)


I have a script working. It uses a modified version of your host rename function, then uses get-vmhostAuthentication | set-vmhostauthentication cmdlets; however, its over 500 lines long and can probably be cut significantly.


if i can do it all from ESXCLI id be happy to.

0 Kudos
internetrush1
Contributor
Contributor

When i do this:

$esxcli.network.ip.dns.search.DNSSearchList

I recieve null feedback; however, i know i have 3 domains listed under the servers dns search configuration.

EDIT:

get-member revealed the following:

$esxcli.network.ip.dns.search.list()

0 Kudos
LucD
Leadership
Leadership

That should be

$esxcli.network.ip.dns.search.List()


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

0 Kudos
MR-Z
VMware Employee
VMware Employee

Great tips! thanks for sharing.

I am trying to avoid the remove/re-add process as that i think historical data are lost on a host when remove and re-add. And some custom fileds and custom values on the host might be wiped out as well...

There must be a way to just update the hostname displayed under hosts and cluster views? (After changing the hostname on the esxi host and dns first of course).

0 Kudos