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.
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.
[ autoit ]
$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