00001: ' WMI_Printer_Error.vbs 00002: ' x1.0-0 15-oct-2010 DTL Didier.Morandi at gmail dot com 00003: 00004: ' --- code not protected --- 00005: Option Explicit 00006: 00007: Dim strComputer, strPrinterName 00008: Dim objWMIService, objShare, objWMI_Error 00009: Dim intSavedErrNumber 00010: 00011: Const Sev_Success = 0, Sev_Warning = 1, Sev_Error = 2, Sev_Fatal = 4 00012: 00013: if Wscript.Arguments.Count=2 Then 00014: strComputer=wScript.Arguments(0) 00015: strPrinterName=wScript.Arguments(1) 00016: else 00017: if Wscript.Arguments.Count=1 Then 00018: strComputer=wScript.Arguments(0) 00019: wscript.stdout.write "Printer name =end: " 00020: strPrinterName = wscript.stdin.readLine 00021: if strPrinterName = "" then wscript.quit 0 00022: else 00023: If Wscript.Arguments.Count=0 Then 00024: wscript.stdout.write "Computer name =end: " 00025: strComputer = wscript.stdin.readLine 00026: if strComputer = "" then wscript.quit 0 00027: wscript.stdout.write "Printer name =end: " 00028: strPrinterName = wscript.stdin.readLine 00029: if strPrinterName = "" then wscript.quit 0 00030: end if 00031: end if 00032: End If 00033: 00034: if strComputer = "." then call GetComputerName(strComputer) 00035: strComputer = Ucase(strComputer) 00036: strPrinterName = Ucase(strPrinterName) 00037: 00038: wscript.echo vbcrlf & "* Bind to WMI provider on computer '" & strComputer & "'..." 00039: ' --- code protected --- 00040: on error resume next 00041: Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 00042: if not IsObject(objWMIService) then wscript.echo "WMI>> GetObject failed" 00043: call errmgt("GetObject", Sev_Fatal) 00044: Err.Clear 00045: wscript.echo "* Bind successful" 00046: 00047: wscript.echo "* Get printer '" & strPrinterName & "' characteristics by DeviceID" 00048: Set objShare = objWMIService.Get("Win32_Printer.DeviceID='" & strPrinterName & "'") 00049: if not IsObject(objShare) then 00050: wscript.echo "*>> Printer " & strPrinterName & " does not exist on computer " & strComputer & vbcrlf 00051: wscript.echo "[WMI> GetPrinterDeviceID failed]" 00052: end if 00053: intSavedErrNumber = Err.Number 00054: call errmgt("GetService", Sev_Error) 00055: if intSavedErrNumber = 0 then 00056: call GetPrinterCharacteristics(strComputer,strPrinterName) 00057: else 00058: wscript.echo "WMI>> Error context follows:" 00059: wscript.echo "[WMI> creating SwbemLastError object]" 00060: Set objWMI_Error = CreateObject("WbemScripting.SwbemLastError") 00061: if IsObject(objWMI_Error) then 00062: wscript.echo "[WMI> SwbemLastError object created successfully]" 00063: else 00064: wscript.echo "WMI>> SwbemLastError CreateObject failed" 00065: end if 00066: call errmgt("CreateObject", Sev_Error) 00067: wscript.echo "WMI Operation : " & objWMI_Error.Operation & vbcrlf & _ 00068: "WMI Parameter : " & objWMI_Error.ParameterInfo & Vbcrlf & _ 00069: "WMI Provider : " & objWMI_Error.ProviderName 00070: wscript.echo "Abort." 00071: wscript.quit 1 00072: end if 00073: 00074: wscript.echo vbcrlf & "* Get printer '" & strPrinterName & "' characteristics by Name" 00075: Set objShare = objWMIService.Get("Win32_Printer.Name='" & strPrinterName & "'") 00076: if not IsObject(objShare) then 00077: wscript.echo "*>> Printer " & strPrinterName & " does not exist on computer " & strComputer & vbcrlf 00078: wscript.echo "[WMI> GetPrinterName failed]" 00079: end if 00080: intSavedErrNumber = Err.Number 00081: call errmgt("GetService", Sev_Error) 00082: if intSavedErrNumber = 0 then 00083: call GetPrinterCharacteristics(strComputer,strPrinterName) 00084: else 00085: wscript.echo "WMI>> Error context follows:" 00086: wscript.echo "[WMI> creating SwbemLastError object]" 00087: Set objWMI_Error = CreateObject("WbemScripting.SwbemLastError") 00088: if IsObject(objWMI_Error) then 00089: wscript.echo "[WMI> SwbemLastError object created successfully]" 00090: else 00091: wscript.echo "WMI>> SwbemLastError CreateObject failed" 00092: call errmgt("CreateObject", Sev_Fatal) 00093: end if 00094: wscript.echo "WMI Operation: " & objWMI_Error.Operation & vbcrlf & _ 00095: "WMI Parameter: " & objWMI_Error.ParameterInfo & Vbcrlf & _ 00096: "WMI Provider : " & objWMI_Error.ProviderName 00097: end if 00098: wscript.quit 0 00099: 00100: '------------------------------------------------------------------------ 00101: Function GetComputerName(strComputer) 00102: 00103: Dim objWshNetwork 00104: 00105: Set objWshNetwork = WScript.CreateObject("WScript.Network") 00106: strComputer = objWshNetwork.ComputerName 00107: 00108: End Function 00109: 00110: '------------------------------------------------------------------------ 00111: Function GetPrinterCharacteristics(strComputer,strPrinterName) 00112: 00113: Dim objWMIService, colItems, objItem 00114: 00115: Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 00116: Set colItems = objWMIService.ExecQuery( _ 00117: "SELECT * FROM Win32_Printer where Name = '" & strPrinterName & "'",,48) 00118: 00119: For Each objItem in colItems 00120: wscript.echo vbcrlf & "Default : " & objItem.Default 00121: wscript.echo "DriverName : " & objItem.DriverName 00122: wscript.echo "Horizontal Resolution: " & objItem.HorizontalResolution 00123: wscript.echo "Network connection : " & objItem.Network 00124: wscript.echo "Port Name : " & objItem.PortName 00125: Next 00126: 00127: End Function 00128: 00129: '------------------------------------------------------------------------ 00130: '+ 00131: ' errmgt.vbs 00132: ' 00133: ' Error handling management routine to be added at the end of a VBscript. 00134: ' 00135: ' Usage: call errmgt(action,severity) 00136: ' 00137: ' Two params should be defined in the caller source: the action performed and the severity level. 00138: ' If error is not fatal, calling code execution is resumed, otherwise procedure stops. 00139: ' 00140: ' Note: caller script should be called via Cscript instead of Wscript. 00141: ' 00142: ' x1.0-0 15-oct-2010 DTL Didier.Morandi at gmail dot com 00143: '- 00144: sub errmgt(WshAction,severity) 00145: 00146: Dim err_nr, err_desc, err_src 00147: 00148: if Err.Number < 0 then 00149: err_nr = Hex(Err.Number) 00150: else 00151: err_nr = Err.Number 00152: end if 00153: 00154: if WshAction = "" then WshAction = "[missing]" 00155: err_desc = err.description 00156: if err_desc = "" then err_desc = "[missing]" 00157: err_src = err.source 00158: if err_src = "" then err_src = "[missing]" 00159: 00160: if Err.Number <> 0 then 00161: wscript.echo "WSH>> Error occurred" 00162: if severity = 0 then 00163: exit sub 00164: elseif severity = 1 then 00165: exit sub 00166: elseif severity = 2 then 00167: wscript.echo "WSH Action : " & WshAction 00168: wscript.echo "WSH Error nr : " & err_nr 00169: wscript.echo "WSH Description: " & err_desc 00170: wscript.echo "WSH Source : " & err_src 00171: elseif severity = 4 then 00172: wscript.echo "WSH Action : " & WshAction 00173: wscript.echo "WSH Fatal error: " & err_nr 00174: wscript.echo "WSH Description: " & err_desc 00175: wscript.echo "WSH Source : " & err_src 00176: wscript.echo "Abort." 00177: wscript.quit 1 00178: else 00179: wscript.echo "Invalid Severity parameter passed to Error Handler" & vbcrlf 00180: end if 00181: Err.Clear 00182: end if 00183: end sub 00184: '------------------------------------------------------------------------ 00185: 'eof errmgt.vbs ----- Listing provided by Line_numbers.vbs on 02/11/2010 14:19:17 (c) 2010 Didier.Morandi (at) gmail dot com http://Didier.Morandi.Free.fr/vbs