' WMI_Printer_Error.vbs ' x1.0-0 15-oct-2010 DTL Didier.Morandi at gmail dot com ' --- code not protected --- Option Explicit Dim strComputer, strPrinterName Dim objWMIService, objShare, objWMI_Error Dim intSavedErrNumber Const Sev_Success = 0, Sev_Warning = 1, Sev_Error = 2, Sev_Fatal = 4 if Wscript.Arguments.Count=2 Then strComputer=wScript.Arguments(0) strPrinterName=wScript.Arguments(1) else if Wscript.Arguments.Count=1 Then strComputer=wScript.Arguments(0) wscript.stdout.write "Printer name =end: " strPrinterName = wscript.stdin.readLine if strPrinterName = "" then wscript.quit 0 else If Wscript.Arguments.Count=0 Then wscript.stdout.write "Computer name =end: " strComputer = wscript.stdin.readLine if strComputer = "" then wscript.quit 0 wscript.stdout.write "Printer name =end: " strPrinterName = wscript.stdin.readLine if strPrinterName = "" then wscript.quit 0 end if end if End If if strComputer = "." then call GetComputerName(strComputer) strComputer = Ucase(strComputer) strPrinterName = Ucase(strPrinterName) wscript.echo vbcrlf & "* Bind to WMI provider on computer '" & strComputer & "'..." ' --- code protected --- on error resume next Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") if not IsObject(objWMIService) then wscript.echo "WMI>> GetObject failed" call errmgt("GetObject", Sev_Fatal) Err.Clear wscript.echo "* Bind successful" wscript.echo "* Get printer '" & strPrinterName & "' characteristics by DeviceID" Set objShare = objWMIService.Get("Win32_Printer.DeviceID='" & strPrinterName & "'") if not IsObject(objShare) then wscript.echo "*>> Printer " & strPrinterName & " does not exist on computer " & strComputer & vbcrlf wscript.echo "[WMI> GetPrinterDeviceID failed]" end if intSavedErrNumber = Err.Number call errmgt("GetService", Sev_Error) if intSavedErrNumber = 0 then call GetPrinterCharacteristics(strComputer,strPrinterName) else wscript.echo "WMI>> Error context follows:" wscript.echo "[WMI> creating SwbemLastError object]" Set objWMI_Error = CreateObject("WbemScripting.SwbemLastError") if IsObject(objWMI_Error) then wscript.echo "[WMI> SwbemLastError object created successfully]" else wscript.echo "WMI>> SwbemLastError CreateObject failed" end if call errmgt("CreateObject", Sev_Error) wscript.echo "WMI Operation : " & objWMI_Error.Operation & vbcrlf & _ "WMI Parameter : " & objWMI_Error.ParameterInfo & Vbcrlf & _ "WMI Provider : " & objWMI_Error.ProviderName wscript.echo "Abort." wscript.quit 1 end if wscript.echo vbcrlf & "* Get printer '" & strPrinterName & "' characteristics by Name" Set objShare = objWMIService.Get("Win32_Printer.Name='" & strPrinterName & "'") if not IsObject(objShare) then wscript.echo "*>> Printer " & strPrinterName & " does not exist on computer " & strComputer & vbcrlf wscript.echo "[WMI> GetPrinterName failed]" end if intSavedErrNumber = Err.Number call errmgt("GetService", Sev_Error) if intSavedErrNumber = 0 then call GetPrinterCharacteristics(strComputer,strPrinterName) else wscript.echo "WMI>> Error context follows:" wscript.echo "[WMI> creating SwbemLastError object]" Set objWMI_Error = CreateObject("WbemScripting.SwbemLastError") if IsObject(objWMI_Error) then wscript.echo "[WMI> SwbemLastError object created successfully]" else wscript.echo "WMI>> SwbemLastError CreateObject failed" end if call errmgt("CreateObject", Sev_Fatal) wscript.echo "WMI Operation: " & objWMI_Error.Operation & vbcrlf & _ "WMI Parameter: " & objWMI_Error.ParameterInfo & Vbcrlf & _ "WMI Provider : " & objWMI_Error.ProviderName end if wscript.quit 0 '------------------------------------------------------------------------ Function GetComputerName(strComputer) Dim objWshNetwork Set objWshNetwork = WScript.CreateObject("WScript.Network") strComputer = objWshNetwork.ComputerName End Function '------------------------------------------------------------------------ Function GetPrinterCharacteristics(strComputer,strPrinterName) Dim objWMIService, colItems, objItem Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set colItems = objWMIService.ExecQuery( _ "SELECT * FROM Win32_Printer where Name = '" & strPrinterName & "'",,48) For Each objItem in colItems wscript.echo vbcrlf & "Default : " & objItem.Default wscript.echo "DriverName : " & objItem.DriverName wscript.echo "Horizontal Resolution: " & objItem.HorizontalResolution wscript.echo "Network connection : " & objItem.Network wscript.echo "Port Name : " & objItem.PortName Next End Function '------------------------------------------------------------------------ '+ ' errmgt.vbs ' ' Error handling management routine to be added at the end of a VBscript. ' ' Usage: call errmgt(action,severity) ' ' Two params should be defined in the caller source: the action performed and the severity level. ' If error is not fatal, calling code execution is resumed, otherwise procedure stops. ' ' Note: caller script should be called via Cscript instead of Wscript. ' ' x1.0-0 15-oct-2010 DTL Didier.Morandi at gmail dot com '- sub errmgt(WshAction,severity) Dim err_nr, err_desc, err_src if Err.Number < 0 then err_nr = Hex(Err.Number) else err_nr = Err.Number end if if WshAction = "" then WshAction = "[missing]" err_desc = err.description if err_desc = "" then err_desc = "[missing]" err_src = err.source if err_src = "" then err_src = "[missing]" if Err.Number <> 0 then wscript.echo "WSH>> Error occurred" if severity = 0 then exit sub elseif severity = 1 then exit sub elseif severity = 2 then wscript.echo "WSH Action : " & WshAction wscript.echo "WSH Error nr : " & err_nr wscript.echo "WSH Description: " & err_desc wscript.echo "WSH Source : " & err_src elseif severity = 4 then wscript.echo "WSH Action : " & WshAction wscript.echo "WSH Fatal error: " & err_nr wscript.echo "WSH Description: " & err_desc wscript.echo "WSH Source : " & err_src wscript.echo "Abort." wscript.quit 1 else wscript.echo "Invalid Severity parameter passed to Error Handler" & vbcrlf end if Err.Clear end if end sub '------------------------------------------------------------------------ 'eof errmgt.vbs