ebuker
Contributor
Contributor

Script to rename VM with DNS Name listed in VC

Jump to solution

I'm very new to powershell, and I've been trying to figure this out using portions of code I've found, but to no avail.

I'm looking for a script to take the DNS hostname listed in VCenter for a given VM and rename the VM.

Eg.

Host01VM01 (as listed in Vcenter) - DNS name of "something.xyz.com".   Rename VM to "something.xyz.com" in Vcenter.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership

To run the script only for a cluster called "MyCluster" you can do:

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

This script will only work for VM's that have the VMware Tools installed and probably only for VM's that are powered on.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

0 Kudos
4 Replies
RvdNieuwendijk
Leadership
Leadership

You can try:

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


Regards, Robert

Message was edited by: RvdNieuwendijk

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
ebuker
Contributor
Contributor

Thanks for the quick reply.

When I run this I don't get any output, just returns to the powershell command line.  I'm obviously doing something wrong...

Also -  I should have included this in my original post, but I need to rename all the VM's within one cluster in VCenter, leaving the others untouched.

0 Kudos
RvdNieuwendijk
Leadership
Leadership

To run the script only for a cluster called "MyCluster" you can do:

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

This script will only work for VM's that have the VMware Tools installed and probably only for VM's that are powered on.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

0 Kudos
ebuker
Contributor
Contributor

Works perfectly.  Thanks for your help.

0 Kudos