30 July 2012

VBScript to rename a computer

This script will append the existing computer name with "-V"  The function of this script is to change the machine name to comply with the naming convention that all virtual machines need to be named as follows:

ComputerName-V

This script can be run as part of your P2V preparation scripts to ensure that the virtual machine does not conflict with the physical machines should both accidentally be turned on at the same time on the network.



dim NewName
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
For Each objItem in colItems
Wscript.echo "Existing Computername " & objItem.DNSHostName
NewName = objItem.DNSHostName &"-v"
wscript.echo NewName
Next
Set colComputers = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputers
err = objComputer.Rename(NewName)
if err <> 0 then
'wscript.echo "There was an error - Rename manually"
MSGBOX "There was an error - Rename manually",16,"Error"
else
wscript.echo "Machine successfully renamed: " & Name
end if
Next




I have set the script to pop up a message box should the script fail.  You can comment this out and use the normal script echo if you wish.


No comments:

Post a Comment