Jump to content
Sign in to follow this  
LurchMan

WMI Help - Or Other suggestions

Recommended Posts

LurchMan

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.

$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.

Share this post


Link to post
Share on other sites
spudw2k

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 !" &amp; @CRLF &amp; _
                "Number is: " &amp; $HexNumber &amp; @CRLF &amp; _
                "Windescription is: " &amp; $oMyError.windescription ) 

   $g_eventerror = 1 ; something to check for when this function returns 
Endfunc

edit: Darn format artifacts from copy/paste.

Edited by spudw2k

Share this post


Link to post
Share on other sites
LurchMan

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.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×