PowerCLI: Change Virtual Machines Guest OS Names: Difference between revisions

From VI-Toolkit
Jump to navigation Jump to search
(Created page with "=== PowerCLI: Change Virtual Machines Guest OS Names === author: Rawlinson ==== Description ==== The script displayed here queries the VM name at the host level and will use ...")
 
m (Adding the code itself (just in case))
 
(One intermediate revision by the same user not shown)
Line 3: Line 3:
==== Description ====
==== Description ====
The script displayed here queries the VM name at the host level and will use that to set the name of the operating system within the guest OS. The example code here is used for windows guests.
The script displayed here queries the VM name at the host level and will use that to set the name of the operating system within the guest OS. The example code here is used for windows guests.
==== Code ====
$VMs = Get-VM | Where {$_.PowerState -eq "PoweredOn"}
Foreach ($VM in $VMS){
      $VCName = $VM.Name
      $WinName = $VM.Guest.Hostname
      If ($WinName -ne $VCName) {
            Write-Host "$VCName is currently $WinName... renaming"
            $renamecomputer = "wmic path win32_computersystem where ""Name='%computername%'"" CALL rename name='$VCName'"
            Invoke-VMScript -VM $VM -GuestUser "Administrator" -GuestPassword "vmware" -ScriptType Bat -ScriptText $renamecomputer
            restart-vmguest -VM $VM -Confirm:$false
      }
}


==== Usage ====
==== Usage ====
Line 10: Line 25:
==== Location ====
==== Location ====


http://www.punchingclouds.com/2013/06/06/powercli-change-virtual-machines-guest-os-names/
* [http://web.archive.org/web/20160616222335/http://www.punchingclouds.com/2013/06/06/powercli-change-virtual-machines-guest-os-names/ http://www.punchingclouds.com/2013/06/06/powercli-change-virtual-machines-guest-os-names/]


[[Category: PowerCLI]] [[Category: VM Management]]
[[Category: PowerCLI]] [[Category: VM Management]]

Latest revision as of 18:11, 5 June 2021

PowerCLI: Change Virtual Machines Guest OS Names

author: Rawlinson

Description

The script displayed here queries the VM name at the host level and will use that to set the name of the operating system within the guest OS. The example code here is used for windows guests.

Code

$VMs = Get-VM | Where {$_.PowerState -eq "PoweredOn"} 

Foreach ($VM in $VMS){
      $VCName = $VM.Name
      $WinName = $VM.Guest.Hostname
      If ($WinName -ne $VCName) {
            Write-Host "$VCName is currently $WinName... renaming"
            $renamecomputer = "wmic path win32_computersystem where ""Name='%computername%'"" CALL rename name='$VCName'"
            Invoke-VMScript -VM $VM -GuestUser "Administrator" -GuestPassword "vmware" -ScriptType Bat -ScriptText $renamecomputer
            restart-vmguest -VM $VM -Confirm:$false
      }
}

Usage

Save as a script and run it.

Location