LurchMan Posted January 17, 2013 Posted January 17, 2013 Hello everyone, I'm working on a project that is migrating my companies print server from one server to another and I have a script that I have been using to run on people's machine to migrate the computer. The script works great as long as the printer exists on the new server. Due to some cleanup I have done some of the printers don't exists on the new server. I was wondering if there was a way to check to see if the printer exists on the new server prior to attempting to add it as the script will crash if the printer doesn't exist. My script is below. Thank you for any help or suggestions on different things I could accomplish this with. expandcollapse popup$strComputer = "." $strOldServer = "PRINT" $strNewServer = "PRINT01" $bDefaultPrinter = False $read = RegRead ("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\", "Device") If StringInStr($read, "\") > 0 Then $aSplit = StringSplit ($read, ",") If IsArray ($aSplit) Then $aSplit2 = StringSplit ($aSplit[1], "\") $sDefServer = $aSplit2[3] If StringUpper($sDefServer) <> $strNewServer Then $sDefServer = $strNewServer $sDefPrinter = $aSplit2[4] $bDefaultPrinter = True EndIf EndIf $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2") $wn = ObjCreate("WScript.Network") $colItems = $objWMIService.ExecQuery("Select * from Win32_Printer Where Local=False") For $objItem In $colItems $printer = $objItem.ShareName $server = $objItem.ServerName If $objItem.Local = False Then If StringUpper($server) = "\\" & $strOldServer Then $wn.RemovePrinterConnection ($server & "\" & $printer, True, True) $wn.AddWindowsPrinterConnection ("\\" & $strNewServer & "\" & $printer, $printer, True) EndIf EndIf Next If $bDefaultPrinter = True Then $objPrinter = ObjCreate("WScript.Network") $objPrinter.SetDefaultPrinter ("\\" & $sDefServer & "\" & $sDefPrinter) EndIf Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.
spudw2k Posted January 17, 2013 Posted January 17, 2013 (edited) One recommendation is to add a COM error handler to deal with errors with the WMI method (.AddWindowsPrint....) and prevent the script from crashing. An example from the help file. Global $g_eventerror = 0 ; to be checked to know if com error occurs. Must be reset after handling. $oMyError = ObjEvent("AutoIt.Error","MyErrFunc") ; Install a custom error handler ; Performing a deliberate failure here (object does not exist) $oIE = ObjCreate("InternetExplorer.Application") $oIE.visible = 1 $oIE.bogus if $g_eventerror then Msgbox(0,"","the previous line got an error.") Exit ; This is my custom error handler Func MyErrFunc() $HexNumber=hex($oMyError.number,8) Msgbox(0,"","We intercepted a COM Error !" & @CRLF & _ "Number is: " & $HexNumber & @CRLF & _ "Windescription is: " & $oMyError.windescription ) $g_eventerror = 1 ; something to check for when this function returns Endfunc edit: Darn format artifacts from copy/paste. Edited January 17, 2013 by spudw2k Spoiler Things I've Made: Always On Top Tool ◊ AU History ◊ Deck of Cards ◊ HideIt ◊ ICU ◊ Icon Freezer ◊ Ipod Ejector ◊ Junos Configuration Explorer ◊ Link Downloader ◊ MD5 Folder Enumerator ◊ PassGen ◊ Ping Tool ◊ Quick NIC ◊ Read OCR ◊ RemoteIT ◊ SchTasksGui ◊ SpyCam ◊ System Scan Report Tool ◊ System UpTime ◊ Transparency Machine ◊ VMWare ESX Builder Misc Code Snippets: ADODB Example ◊ CheckHover ◊ Detect SafeMode ◊ DynEnumArray ◊ GetNetStatData ◊ HashArray ◊ IsBetweenDates ◊ Local Admins ◊ Make Choice ◊ Recursive File List ◊ Remove Sizebox Style ◊ Retrieve PNPDeviceID ◊ Retrieve SysListView32 Contents ◊ Set IE Homepage ◊ Tickle Expired Password ◊ Transpose Array Projects: Drive Space Usage GUI ◊ LEDkIT ◊ Plasma_kIt ◊ Scan Engine Builder ◊ SpeeDBurner ◊ SubnetCalc Cool Stuff: AutoItObject UDF ◊ Extract Icon From Proc ◊ GuiCtrlFontRotate ◊ Hex Edit Funcs ◊ Run binary ◊ Service_UDF
LurchMan Posted January 21, 2013 Author Posted January 21, 2013 Thank you for this! When I read over this my little knowledge of objects kicked in and I didn't realize COM objects is the same thing as the objects I'm using. Again, thank you for this! My script doesn't crash anymore and it works perfectly! Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.
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