Rename a Computer Using WMI Scripting
This Wednesday I'm doing an MVC Hack Night at my work with Derek Fowler. This evening will hopefully give us a chance to show other employees what ASP.NET MVC is and to give them some simple tutorials to work on.
As ASP.NET MVC is only preview 5 we didn't want to install the extensions onto the work machines, so we decided to create a time based XP Virtual Machine that everyone could copy and use for the evening. Not only did this mean we could install the extensions without fear of breaking our development machines but it also meant we could ensure that Sql Express and Visual Studio Express was setup and working with the tutorial code properly.
There was one giltch with the plan and that was that the Virtual Machines would all be using the same Computer Name, this would result in the computers not logging onto the network and all sort of errors could occur.
My solution to this was to write a dead simple WMI Script in VB that would run at logon, this script would check to see if the Computer Name was the default, in this case MVC, and if it was to rename it to MVC-{Random Number}. It would then reboot the computer and then find the name changed and hopefully find it can logon to the network fine.
The source code is below, I've chosen to generate a number between 1 & 1000. This should hopefully not generate any machines with the same name, as there's only 10 of us.
The script really is quite simple, I first get the Computer WMI object and check it's name to see if it equals MVC, if it isn't I change it, echo out its been changed and then use the WMI Script object to run Shutdown.exe /r (reboot the computer). Now I could use the Operating System WMI Object to issue a shutdown but I didnt think of this until after I had wrote the script.
lowerbound = 0 ' lowest value
upperbound = 1000 ' highest value
Randomize
randomvalue = CInt(Int((upperbound - lowerbound + 1) * Rnd() + lowerbound)) 'generate random number
Name = "MVC-" & randomvalue ' our new computer name
Password = "abc123" 'my admin password
Username = "admin" 'my admin username
Set objWMIService = GetObject("Winmgmts:root\cimv2")
' Call always gets only one Win32_ComputerSystem object.
For Each objComputer in _
objWMIService.InstancesOf("Win32_ComputerSystem")
If objComputer.Name = "MVC" Then ' check to see if comp is using default name
Return = objComputer.rename(Name,Password,Username) ' rename comp
If Return <> 0 Then 'check for error
WScript.Echo "Rename failed. Error = " & Err.Number
Else
WScript.Echo "Rename succeeded." & _
" Rebooting for new name to go into effect"
set objWSHShell = WScript.CreateObject("WScript.Shell")
objWSHShell.Run "shutdown.exe /r" ' reboot comp for changes to take effect
End If
Else
WScript.Echo "Rename Not Needed"
End If
Next
If you want to use this script / adapt it for your needs feel free, it certainly made my life easier!
1 comment:
Blog is really informative and entertainng same time. I appriciate the efforts made by you int his post.
Refillable Ink Cartridge
Post a Comment