simeon Posted October 25, 2005 Share Posted October 25, 2005 Please post any feedback about this script - it's only my second with AutoIT. I'll respond only if I think it's important to. Thanks in advance for any useful feedback Simeon expandcollapse popup; AutoIT 3.0 http://www.autoitscript.com/autoit3/ ; Non-interactively renames an XP computer to its DNS hostname, found by looking ; up the DNS entry for the computer's current IP, via the command "nslookup". ; TODO: Embrace and extend more versions of Windows, with appropriate testing. ; For this script to work, the DNS server has to have a valid name for the computer at the current IP. ; If not, then the script will most likely fail somehow. ; Make sure the computer has only one IP, the one that matches the name you want. ; If this is too hard, then just let this script fail, and rename the computer ; yourself. ; Logs to standard output. This will *only* be readable when output is redirected. ; eg cmd /c AutoIt3.exe initialise_plugins.au3 > test.txt ; Return error value will *only* be detectable if this script is run from a cmd script, ; *not* if you try to test this script straight from the command line or by clicking. ; See http://www.autoitscript.com/forum/lofiversion/index.php?t5356.html ; Initialise GUI window to provide user with feedback on progress. ;#include <GUIConstants.au3> Global Const $DS_SETFOREGROUND = 0x00000200 Global Const $WS_EX_TOPMOST = 0x00000008 Global Const $WS_CAPTION = 0x00C00000 GUICreate("AutoIT configuration script", 400, 140, 100, 100, BitOr($DS_SETFOREGROUND, $WS_CAPTION), $WS_EX_TOPMOST ) GUICtrlCreateLabel("This AutoIT configuration should be finished within a minute or so.", 30, 20) GUICtrlCreateLabel("Blocking input. CTRL-ALT-DEL followed by ESC will unlock.", 30, 40) ; This needs to be large enough to contain any output later sent to $infotext... $infotext = GUICtrlCreateLabel("", 30, 60, 340, 40) GUISetState(@SW_SHOW) ; Block console input. BlockInput(1) ; ******************************************************************************** ; ** REMEMBER ** to update NAME and DATE if you modify the script!!! _Message(@ScriptFullPath & @LF & "Last edited by Simeon 25 Oct 2005") ; ******************************************************************************** _Message(@AutoItExe & @LF & 'AutoIT version "' & @AutoItVersion & '"') ; Note: warning message has 'float on top' property, so if commands don't address ; a specific window, then it's usually necessary to explicitely activate each window ; for any interaction. ; We will use a temp file to dump output from nslookup query on current IP address. ; On XP the temp directory is user-specific, so this should be a safe enough to put it. $filename = @TempDir & "\nslookup" & @MIN & @SEC & ".txt" ; About to use a temporary file. First delete any existing copy which may be left from a previous run. If FileExists($filename) Then If NOT FileDelete($filename) Then _Die_Message('Error: problem deleting pre-existing file "' &$filename & '"!' & @LF & 'Computername left unchanged.') EndIf EndIf _Message('Performing nslookup to get DNS name for IP "' & @IPAddress1 & '"') ; For nslookup.exe syntax, see ; http://support.microsoft.com/default.aspx?scid=KB;EN-US;q200525 $nslookup = @WindowsDir & '\System32\nslookup.exe -retry=10' ; Watch out for quoted quotes! (and spaces) .. really more should be quoted, but it broke. ; See http://www.autoitscript.com/forum/index.php?showtopic=17262 $commandline = @ComSpec & ' /A /C "' & $nslookup & '" ' & @IPAddress1 & ' > ' & $filename RunWait($commandline) ; Poor opportunity for error-checking. Bad errors will crash the script. ; Others may be detected below. $filehandle = FileOpen($filename, 0) If $filehandle = -1 Then _Die_Message('Error: unable to open file "' & $filename & '"') EndIf ; Now parse the nslookup output from the generated file. $computername = "invalidtoomanycharactersforastart" Do $line = FileReadLine($filehandle) If @error = -1 Then _Die_Message('Error: error parsing file "' & $filename & @LF & '" Computername left unchanged.') EndIf If StringInStr($line, "Name:") Then ; Found line with relevant DNS name in it. Break it down. $line = StringStripWS($line, 8 ) $line = StringReplace($line, "Name:", "") $splitline = StringSplit( $line, ".") If $splitline[0] < 2 Then _Die_Message('Error: Computername value from "' & $filename & '" is empty!' & @LF & ' Computername left unchanged.') Else $computername = $splitline[1] _Message('Using Computername "' & $computername & '"') EndIf EndIf Until $computername <> "invalidtoomanycharactersforastart" ; Close, and then delete the temporary file. FileClose($filename) If NOT FileDelete($filename) Then _Die_Message('Error: problem deleting temp file "' & $filename & '"!' & @LF & 'Computername left unchanged.') EndIf ; Check if name actually needs changing. If $computername = @ComputerName Then _Done_Message("Computername already set correctly!" & @LF & "Computername left unchanged.") ; Pop up the Windows 'System Properties' dialogue. Run('"' & @WindowsDir & '\System32\rundll32.exe" shell32,Control_RunDLL sysdm.cpl') ; No chance to check for result - if it fails, script will crash. ; Now make the GUI maneuvers to make the name change. ; Open System properties and find the Computername tab... If NOT WinWait("System Properties", "Microsoft Windows XP", 20) Then _Die_Message("Error: problem interacting with Windows GUI!" & @LF & "Computername left unchanged.") EndIf If NOT Send("{RIGHT}") Then _Die_Message("Error: problem interacting with Windows GUI!" & @LF & "Computername left unchanged.") EndIf If NOT WinWait("System Properties", "&Change...", 20) Then _Die_Message("Error: problem interacting with Windows GUI!" & @LF & "Computername left unchanged.") EndIf ; Click 'Change', set the name. If NOT ControlClick("System Properties", "To rename this computer or join a domain, click Change.", "&Change...") Then _Die_Message("Error: problem interacting with Windows GUI!" & @LF & "Computername left unchanged.") EndIf If NOT WinWait("Computer Name Changes", "&Computer name:", 20) Then _Die_Message("Error: problem interacting with Windows GUI!" & @LF & "Computername left unchanged.") EndIf If NOT ControlSetText("Computer Name Changes", "&Computer name:", "Edit1", $computername) Then _Die_Message("Error: problem interacting with Windows GUI!" & @LF & "Computername left unchanged.") EndIf If NOT ControlClick("Computer Name Changes", "&Computer name:", "OK") Then _Die_Message("Error: problem interacting with Windows GUI!" & @LF & "Computername left unchanged.") EndIf ; Now a warning window appears with the same title as the last one .. ! If NOT WinWait("Computer Name Changes", "You must restart this computer for the changes to take effect.", 20) Then _Die_Message("Error: problem interacting with Windows GUI!" & @LF & "Computername left unchanged.") EndIf If NOT ControlClick("Computer Name Changes", "You must restart this computer for the changes to take effect.", "OK") Then _Die_Message("Error: problem interacting with Windows GUI!" & @LF & "Computername left unchanged.") EndIf ; Confirm System Properties change in main System Properties window. If NOT ControlClick("System Properties", "&Change...", "OK") Then _Die_Message("Error: problem interacting with Windows GUI!" & @LF & "Computername status uncertain.") EndIf ; Prompts us to restart immediately? 'No', since that should be done outside this script. If NOT WinWait("System Settings Change", "Do you want to restart your computer now?", 20) Then _Die_Message("Error: problem interacting with Windows GUI!" & @LF & "Computername status uncertain.") EndIf If NOT ControlClick("System Settings Change", "Do you want to restart your computer now?", "&No") Then _Die_Message("Error: problem interacting with Windows GUI!" & @LF & "Computername status uncertain.") EndIf ; Success! _Done_Message('Success: computername set to "' & $computername & '".') ; Declare functions used above. Func _Die_Message ($msg) _Message($msg) Exit(1) EndFunc Func _Done_Message ($msg) _Message($msg) Exit(0) EndFunc Func _Message ($msg) ConsoleWrite($msg & @LF) GUICtrlSetData($infotext, $msg) Sleep(5000) EndFunc Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now