How to chang hostname of VM via PowerCLI

williamwlk
williamwlk used Ask the Experts™
on
I'd like to change hostname of VM via PowerCLI

VSphere 4/5

I found this :

Get-VM | `
Where-Object { $_.Guest.HostName } | `
ForEach-Object { Set-VM -VM $_ -Name $_.Guest.HostName -Confirm:$false }

Open in new window


http://communities.vmware.com/message/1737030?tstart=0

But I do not properly understand Where-Object and $_.Guest.Hostname properties.

VMTools installed in VM.

Help,  Folks.

Appreciate it.

W
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
if you change the name of the vm see

Generating new certificates for the ESX host
 
The ESX host generates certificates the first time the system is started. Under certain circumstances, it might be required to force the host to generate new certificates. Typically new certificates only need to be generated if the hostname has been changed or the certificates have been deleted accidentally.
 
Each time you restart the vmware-hostd process, the mgmt-vmware script searches for existing certificate files ( rui.crt and rui.key). If the files cannot be found, new certificate files are generated.
 
To generate new certificates:
 1.Put the host into Maintenance Mode.
2.In the directory /etc/vmware/ssl, back up any existing certificates by renaming them with these commands:

[root@server root]# mv /etc/vmware/ssl/rui.crt /etc/vmware/ssl/orig.rui.crt
[root@server root]# mv /etc/vmware/ssl/rui.key /etc/vmware/ssl/orig.rui.key

Note: If you are regenerating certificates because you accidentally deleted them, you do not have to rename them.

 
3.Reboot your host to allow it to begin using the new certificate, or restart the host services:

 a.Restart hostd, using this command:

service mgmt-vmware restart

 
b.Restart vmkauthd, using this command:

service vmware-vmkauthd restart


 
4.Exit Maintenance Mode.
5.Confirm that the ESX host successfully generated new certificates by running this command and comparing the time stamps of the new certificate files with orig.rui.crt and orig.rui.key:

[root@server root]# ls -la /etc/vmware/ssl/rui*

Commented:
Mentioned script changes name of the machine to its hostname.

If you want to change hostname of the machine try to use Invoke-VMScript with wmic:

wmic computersystem where name=`"%computername%`" call rename name=`"$VM`"
How to Generate Services Revenue the Easiest Way

This Tuesday! Learn key insights about modern cyber protection services & gain practical strategies to skyrocket business:

- What it takes to build a cloud service portfolio
- How to determine which services will help your unique business grow
- Various use-cases and examples

Commented:
Channge hostname of the VM to its VM name:

Function Set-WinVMHostname ($VM, $GC) {
 $netsh = "wmic computersystem where name=`"%computername%`" call rename name=`"$VM`""
 Write-Host "Setting hostname address for $VM..."
 Invoke-VMScript -VM $VM -GuestCredential $GC -ScriptType bat -ScriptText $netsh
 Write-Host "Setting hostname completed."
}

$GuestCred = $Host.UI.PromptForCredential("Please enter credentials", "Enter Guest credentials for $VM", "Administrator", "")

$VM = Get-VM <yourVM>
Set-WinVMhostname $VM.name $GuestCred

Open in new window

Author

Commented:
Thanks Folks,

Papalala, mine is Linux VM.

So,  i need to do the following changes :

[root@rhel6 ~]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=rhel6.tester.pro

[root@rhel6 ~]# hostname
rhel6.tester.pro

[root@rhel6 ~]# cat /etc/hosts
10.200.20.55 rhel6.tester.pro
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.10.10.10 rhel6.tester.pro rhel6
$VMName = "test-redhat"
$oldName = "wako-wako"
$newName = "waka-waka"
$GC = Get-Credential
Get-VM $VMName  | Invoke-VMScript -GuestCredential $GC "sed -i 's/$oldName/$newName/g' /etc/sysconfig/network; sed -i 's/$oldName/$newName/g' /etc/hosts; echo $newName > /proc/sys/kernel/hostname; service network restart"


:) thanks.
W

Author

Commented:
Thanks so much, Folks!

W

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial