Saturday, October 10, 2009

Renaming a computer from a script

There's a lot of different ways to automate renaming computers.  An easy way to put a script together to automate computer renaming is to use AutoIt.  If you're not familiar with AutoIt you should check out their website, autoitscript.com/.  It's an easy to use (yet still powerful) scripting language with built-in functions for automating mouse movements and key strokes.  Here's a script I put together that will use the standard XP interface to rename a computer.

$name = "COMPUTERNAME"
$workgroup = "WORKGROUP"
$restart = 0 ;0 = no restart, 1 = restart

Run("control sysdm.cpl,,1 -1") ;Launch System Properties Page
WinWait("System Properties") ;Wait for the page to exist
WinActivate("System Properties") ;Activate it (it should be already, but just in case)
ControlClick("System Properties","","&Change...")
WinWait("Computer Name Changes")
WinActivate("Computer Name Changes") ;Activate it just in case
ControlSetText("Computer Name Changes","","Edit1",$name) ;Change computer name
ControlSetText("Computer Name Changes","","Edit4",$workgroup) ;Change workgroup
ControlClick("Computer Name Changes","","OK")
WinWaitNotActive("Computer Name Changes","You can change the") ;Wait for something else to take focus
WinWaitActive("Computer Name Changes","Welcome to the",5) ;Wait for new workgroup dialog box
If WinActive("Computer Name Changes","Welcome to the") Then
   ControlClick("Computer Name Changes","Welcome to the","OK") ;If the new workgroup dialog pops up click OK
EndIf
WinWaitActive("Computer Name Changes","You must restart")
ControlClick("Computer Name Changes","You must restart","OK")
WinWaitActive("System Properties") ;Wait for System Properties to come back to focus
ControlClick("System Properties","","OK")
WinWait("System Settings Change")
WinWaitActive("System Settings Change","You must restart your")
If $restart Then ;Pick the restart option
   ControlClick("System Settings Change","","&Yes")
Else
   ControlClick("System Settings Change","","&No")
EndIf

Set the 3 variables at the top, $name, $workgroup, and $restart.  The name and workgroup should be pretty obvious, the restart options just controls whether or not the script answers yes or no to the "Would you like to restart your computer" prompt that pops up after you rename your computer.

No comments:

Post a Comment