Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

695 lines
20 KiB

'----------------------------------------------------------------------
'
' Copyright (c) Microsoft Corporation. All rights reserved.
'
' Abstract:
' prnjobs.vbs - job control script for WMI on Whistler
' used to pause, resume, cancel and list jobs
'
' Usage:
' prnjobs [-zmxl?] [-s server] [-p printer] [-j jobid] [-u user name] [-w password]
'
' Examples:
' prnjobs -z -j jobid -p printer
' prnjobs -l -p printer
'
'----------------------------------------------------------------------
option explicit
'
' Debugging trace flags, to enable debug output trace message
' change gDebugFlag to true.
'
const kDebugTrace = 1
const kDebugError = 2
dim gDebugFlag
gDebugFlag = false
'
' Operation action values.
'
const kActionUnknown = 0
const kActionPause = 1
const kActionResume = 2
const kActionCancel = 3
const kActionList = 4
const kErrorSuccess = 0
const KErrorFailure = 1
const kNameSpace = "root\cimv2"
'
' Generic strings
'
const L_Empty_Text = ""
const L_Space_Text = " "
const L_Error_Text = "Error"
const L_Success_Text = "Success"
const L_Failed_Text = "Failed"
const L_Hex_Text = "0x"
const L_Printer_Text = "Printer"
const L_Operation_Text = "Operation"
const L_Provider_Text = "Provider"
const L_Description_Text = "Description"
const L_Debug_Text = "Debug:"
'
' General usage messages
'
const L_Help_Help_General01_Text = "Usage: prnjobs [-zmxl?] [-s server][-p printer][-j jobid][-u user name][-w password]"
const L_Help_Help_General02_Text = "Arguments:"
const L_Help_Help_General03_Text = "-j - job id"
const L_Help_Help_General04_Text = "-l - list all jobs"
const L_Help_Help_General05_Text = "-m - resume the job"
const L_Help_Help_General06_Text = "-p - printer name"
const L_Help_Help_General07_Text = "-s - server name"
const L_Help_Help_General08_Text = "-u - user name"
const L_Help_Help_General09_Text = "-w - password"
const L_Help_Help_General10_Text = "-x - cancel the job"
const L_Help_Help_General11_Text = "-z - pause the job"
const L_Help_Help_General12_Text = "-? - display command usage"
const L_Help_Help_General13_Text = "Examples:"
const L_Help_Help_General14_Text = "prnjobs -z -p printer -j jobid"
const L_Help_Help_General15_Text = "prnjobs -l -p printer"
const L_Help_Help_General16_Text = "prnjobs -l"
'
' Messages to be displayed if the scripting host is not cscript
'
const L_Help_Help_Host01_Text = "Please run this script using CScript."
const L_Help_Help_Host02_Text = "This can be achieved by"
const L_Help_Help_Host03_Text = "1. Using ""CScript script.vbs arguments"" or"
const L_Help_Help_Host04_Text = "2. Changing the default Windows Scripting Host to CScript"
const L_Help_Help_Host05_Text = " using ""CScript //H:CScript //S"" and running the script "
const L_Help_Help_Host06_Text = " ""script.vbs arguments""."
'
' General error messages
'
const L_Text_Error_General01_Text = "The scripting host could not be determined."
const L_Text_Error_General02_Text = "Unable to parse command line."
const L_Text_Error_General03_Text = "Win32 error code"
'
' Miscellaneous messages
'
const L_Text_Msg_General01_Text = "Unable to enumarate print jobs"
const L_Text_Msg_General02_Text = "Number of print jobs enumerated"
const L_Text_Msg_General03_Text = "Unable to set print job"
const L_Text_Msg_General04_Text = "Unable to get SWbemLocator object"
const L_Text_Msg_General05_Text = "Unable to connect to WMI service"
'
' Print job properties
'
const L_Text_Msg_Job01_Text = "Job id"
const L_Text_Msg_Job02_Text = "Printer"
const L_Text_Msg_Job03_Text = "Document"
const L_Text_Msg_Job04_Text = "Data type"
const L_Text_Msg_Job05_Text = "Driver name"
const L_Text_Msg_Job06_Text = "Description"
const L_Text_Msg_Job07_Text = "Elaspsed time"
const L_Text_Msg_Job08_Text = "Machine name"
const L_Text_Msg_Job09_Text = "Job status"
const L_Text_Msg_Job10_Text = "Notify"
const L_Text_Msg_Job11_Text = "Owner"
const L_Text_Msg_Job12_Text = "Pages printed"
const L_Text_Msg_Job13_Text = "Parameters"
const L_Text_Msg_Job14_Text = "Size"
const L_Text_Msg_Job15_Text = "Start time"
const L_Text_Msg_Job16_Text = "Until time"
const L_Text_Msg_Job17_Text = "Status"
const L_Text_Msg_Job18_Text = "Status mask"
const L_Text_Msg_Job19_Text = "Time submitted"
const L_Text_Msg_Job20_Text = "Total pages"
'
' Action strings
'
const L_Text_Action_General01_Text = "Pause"
const L_Text_Action_General02_Text = "Resume"
const L_Text_Action_General03_Text = "Cancel"
'
' Debug messages
'
const L_Text_Dbg_Msg01_Text = "In function ListJobs"
const L_Text_Dbg_Msg02_Text = "In function ExecJob"
const L_Text_Dbg_Msg03_Text = "In function ParseCommandLine"
main
'
' Main execution starts here
'
sub main
dim iAction
dim iRetval
dim strServer
dim strPrinter
dim strJob
dim strUser
dim strPassword
'
' Abort if the host is not cscript
'
if not IsHostCscript() then
call wscript.echo(L_Help_Help_Host01_Text & vbCRLF & L_Help_Help_Host02_Text & vbCRLF & _
L_Help_Help_Host03_Text & vbCRLF & L_Help_Help_Host04_Text & vbCRLF & _
L_Help_Help_Host05_Text & vbCRLF & L_Help_Help_Host06_Text & vbCRLF)
wscript.quit
end if
iRetval = ParseCommandLine(iAction, strServer, strPrinter, strJob, strUser, strPassword)
if iRetval = kErrorSuccess then
select case iAction
case kActionPause
iRetval = ExecJob(strServer, strJob, strPrinter, strUser, strPassword, L_Text_Action_General01_Text)
case kActionResume
iRetval = ExecJob(strServer, strJob, strPrinter, strUser, strPassword, L_Text_Action_General02_Text)
case kActionCancel
iRetval = ExecJob(strServer, strJob, strPrinter, strUser, strPassword, L_Text_Action_General03_Text)
case kActionList
iRetval = ListJobs(strServer, strPrinter, strUser, strPassword)
case else
Usage(true)
exit sub
end select
end if
end sub
'
' Enumerate all print jobs on a printer
'
function ListJobs(strServer, strPrinter, strUser, strPassword)
on error resume next
DebugPrint kDebugTrace, L_Text_Dbg_Msg01_Text
dim Jobs
dim oJob
dim oService
dim iRetval
dim strTemp
dim iTotal
if WmiConnect(strServer, kNameSpace, strUser, strPassword, oService) then
set Jobs = oService.InstancesOf("Win32_PrintJob")
else
ListJobs = kErrorFailure
exit function
end if
if Err.Number <> kErrorSuccess then
wscript.echo L_Text_Msg_General01_Text & L_Space_Text & L_Error_Text & L_Space_Text _
& L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
ListJobs = kErrorFailure
exit function
end if
iTotal = 0
for each oJob in Jobs
'
' oJob.Name has the form "printer name, job id". We are isolating the printer name
'
strTemp = Mid(oJob.Name, 1, InStr(1, oJob.Name, ",", 1)-1 )
'
' If no printer was specified, then enumerate all jobs
'
if strPrinter = null or strPrinter = "" or LCase(strTemp) = LCase(strPrinter) then
iTotal = iTotal + 1
wscript.echo L_Empty_Text
wscript.echo L_Text_Msg_Job01_Text & L_Space_Text & oJob.JobId
wscript.echo L_Text_Msg_Job02_Text & L_Space_Text & strTemp
wscript.echo L_Text_Msg_Job03_Text & L_Space_Text & oJob.Document
wscript.echo L_Text_Msg_Job04_Text & L_Space_Text & oJob.DataType
wscript.echo L_Text_Msg_Job05_Text & L_Space_Text & oJob.DriverName
wscript.echo L_Text_Msg_Job06_Text & L_Space_Text & oJob.Description
wscript.echo L_Text_Msg_Job07_Text & L_Space_Text & Mid(CStr(oJob.ElapsedTime), 9, 2) & ":" _
& Mid(CStr(oJob.ElapsedTime), 11, 2) & ":" _
& Mid(CStr(oJob.ElapsedTime), 13, 2)
wscript.echo L_Text_Msg_Job08_Text & L_Space_Text & oJob.HostPrintQueue
wscript.echo L_Text_Msg_Job09_Text & L_Space_Text & oJob.JobStatus
wscript.echo L_Text_Msg_Job10_Text & L_Space_Text & oJob.Notify
wscript.echo L_Text_Msg_Job11_Text & L_Space_Text & oJob.Owner
wscript.echo L_Text_Msg_Job12_Text & L_Space_Text & oJob.PagesPrinted
wscript.echo L_Text_Msg_Job13_Text & L_Space_Text & oJob.Parameters
wscript.echo L_Text_Msg_Job14_Text & L_Space_Text & oJob.Size
if CStr(oJob.StartTime) <> "********000000.000000+000" and _
CStr(oJob.UntilTime) <> "********000000.000000+000" then
wscript.echo L_Text_Msg_Job15_Text & L_Space_Text & Mid(Mid(CStr(oJob.StartTime), 9, 4), 1, 2) & "h" _
& Mid(Mid(CStr(oJob.StartTime), 9, 4), 3, 2)
wscript.echo L_Text_Msg_Job16_Text & L_Space_Text & Mid(Mid(CStr(oJob.UntilTime), 9, 4), 1, 2) & "h" _
& Mid(Mid(CStr(oJob.UntilTime), 9, 4), 3, 2)
end if
wscript.echo L_Text_Msg_Job17_Text & L_Space_Text & oJob.Status
wscript.echo L_Text_Msg_Job18_Text & L_Space_Text & oJob.StatusMask
wscript.echo L_Text_Msg_Job19_Text & L_Space_Text & Mid(CStr(oJob.TimeSubmitted), 5, 2) & "/" _
& Mid(CStr(oJob.TimeSubmitted), 7, 2) & "/" _
& Mid(CStr(oJob.TimeSubmitted), 1, 4) & " " _
& Mid(CStr(oJob.TimeSubmitted), 9, 2) & ":" _
& Mid(CStr(oJob.TimeSubmitted), 11, 2) & ":" _
& Mid(CStr(oJob.TimeSubmitted), 13, 2)
wscript.echo L_Text_Msg_Job20_Text & L_Space_Text & oJob.TotalPages
Err.Clear
end if
next
wscript.echo L_Empty_Text
wscript.echo L_Text_Msg_General02_Text & L_Space_Text & iTotal
ListJobs = kErrorSuccess
end function
'
' Pause/Resume/Cancel jobs
'
function ExecJob(strServer, strJob, strPrinter, strUser, strPassword, strCommand)
on error resume next
DebugPrint kDebugTrace, L_Text_Dbg_Msg02_Text
dim oJob
dim oService
dim iRetval
dim uResult
dim strName
'
' Build up the key. The key for print jobs is "printer-name, job-id"
'
strName = strPrinter & ", " & strJob
iRetval = kErrorFailure
if WmiConnect(strServer, kNameSpace, strUser, strPassword, oService) then
set oJob = oService.Get("Win32_PrintJob.Name='" & strName & "'")
else
ExecJob = kErrorFailure
exit function
end if
'
' Check if getting job instance succeeded
'
if Err.Number = kErrorSuccess then
uResult = kErrorSuccess
select case strCommand
case L_Text_Action_General01_Text
uResult = oJob.Pause()
case L_Text_Action_General02_Text
uResult = oJob.Resume()
case L_Text_Action_General03_Text
oJob.Delete_()
case else
Usage(true)
end select
if Err.Number = kErrorSuccess then
if uResult = kErrorSuccess then
wscript.echo L_Success_Text & L_Space_Text & strCommand & L_Space_Text _
& L_Text_Msg_Job01_Text & L_Space_Text & strJob _
& L_Space_Text & L_Printer_Text & L_Space_Text & strPrinter
iRetval = kErrorSuccess
else
wscript.echo L_Failed_Text & L_Space_Text & strCommand & L_Space_Text _
& L_Text_Error_General03_Text & L_Space_Text & uResult
end if
else
wscript.echo L_Text_Msg_General03_Text & L_Space_Text & L_Error_Text & L_Space_Text _
& L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
'
' Try getting extended error information
'
call LastError()
end if
else
wscript.echo L_Text_Msg_General03_Text & L_Space_Text & L_Error_Text & L_Space_Text _
& L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
'
' Try getting extended error information
'
call LastError()
end if
ExecJob = iRetval
end function
'
' Debug display helper function
'
sub DebugPrint(uFlags, strString)
if gDebugFlag = true then
if uFlags = kDebugTrace then
wscript.echo L_Debug_Text & L_Space_Text & strString
end if
if uFlags = kDebugError then
if Err <> 0 then
wscript.echo L_Debug_Text & L_Space_Text & strString & L_Space_Text _
& L_Error_Text & L_Space_Text & L_Hex_Text & hex(Err.Number) _
& L_Space_Text & Err.Description
end if
end if
end if
end sub
'
' Parse the command line into its components
'
function ParseCommandLine(iAction, strServer, strPrinter, strJob, strUser, strPassword)
on error resume next
DebugPrint kDebugTrace, L_Text_Dbg_Msg03_Text
dim oArgs
dim iIndex
iAction = kActionUnknown
iIndex = 0
set oArgs = wscript.Arguments
while iIndex < oArgs.Count
select case oArgs(iIndex)
case "-z"
iAction = kActionPause
case "-m"
iAction = kActionResume
case "-x"
iAction = kActionCancel
case "-l"
iAction = kActionList
case "-p"
iIndex = iIndex + 1
strPrinter = oArgs(iIndex)
case "-s"
iIndex = iIndex + 1
strServer = RemoveBackslashes(oArgs(iIndex))
case "-j"
iIndex = iIndex + 1
strJob = oArgs(iIndex)
case "-u"
iIndex = iIndex + 1
strUser = oArgs(iIndex)
case "-w"
iIndex = iIndex + 1
strPassword = oArgs(iIndex)
case "-?"
Usage(true)
exit function
case else
Usage(true)
exit function
end select
iIndex = iIndex + 1
wend
if Err.Number = kErrorSuccess then
ParseCommandLine = kErrorSuccess
else
wscript.echo L_Text_Error_General02_Text & L_Space_Text & L_Error_Text & L_Space_Text _
& L_Hex_Text & hex(Err.Number) & L_Space_text & Err.Description
ParseCommandLine = kErrorFailure
end if
end function
'
' Display command usage.
'
sub Usage(bExit)
wscript.echo L_Help_Help_General01_Text
wscript.echo L_Empty_Text
wscript.echo L_Help_Help_General02_Text
wscript.echo L_Help_Help_General03_Text
wscript.echo L_Help_Help_General04_Text
wscript.echo L_Help_Help_General05_Text
wscript.echo L_Help_Help_General06_Text
wscript.echo L_Help_Help_General07_Text
wscript.echo L_Help_Help_General08_Text
wscript.echo L_Help_Help_General09_Text
wscript.echo L_Help_Help_General10_Text
wscript.echo L_Help_Help_General11_Text
wscript.echo L_Help_Help_General12_Text
wscript.echo L_Empty_Text
wscript.echo L_Help_Help_General13_Text
wscript.echo L_Help_Help_General14_Text
wscript.echo L_Help_Help_General15_Text
wscript.echo L_Help_Help_General16_Text
if bExit then
wscript.quit(1)
end if
end sub
'
' Determines which program is being used to run this script.
' Returns true if the script host is cscript.exe
'
function IsHostCscript()
on error resume next
dim strFullName
dim strCommand
dim i, j
dim bReturn
bReturn = false
strFullName = WScript.FullName
i = InStr(1, strFullName, ".exe", 1)
if i <> 0 then
j = InStrRev(strFullName, "\", i, 1)
if j <> 0 then
strCommand = Mid(strFullName, j+1, i-j-1)
if LCase(strCommand) = "cscript" then
bReturn = true
end if
end if
end if
if Err <> 0 then
wscript.echo L_Text_Error_General01_Text & L_Space_Text & L_Error_Text & L_Space_Text _
& L_Hex_Text & hex(Err.Number) & L_Space_Text & Err.Description
end if
IsHostCscript = bReturn
end function
'
' Retrieves extended information about the last error that occured
' during a WBEM operation. The methods that set an SWbemLastError
' object are GetObject, PutInstance, DeleteInstance
'
sub LastError()
on error resume next
dim oError
set oError = CreateObject("WbemScripting.SWbemLastError")
if Err = kErrorSuccess then
wscript.echo L_Operation_Text & L_Space_Text & oError.Operation
wscript.echo L_Provider_Text & L_Space_Text & oError.ProviderName
wscript.echo L_Description_Text & L_Space_Text & oError.Description
wscript.echo L_Text_Error_General03_Text & L_Space_Text & oError.StatusCode
end if
end sub
'
' Connects to the WMI service on a server. oService is returned as a service
' object (SWbemServices)
'
function WmiConnect(strServer, strNameSpace, strUser, strPassword, oService)
on error resume next
dim oLocator
dim bResult
oService = null
bResult = false
set oLocator = CreateObject("WbemScripting.SWbemLocator")
if Err = kErrorSuccess then
set oService = oLocator.ConnectServer(strServer, strNameSpace, strUser, strPassword)
if Err = kErrorSuccess then
bResult = true
oService.Security_.impersonationlevel = 3
'
' Required to perform administrative tasks on the spooler service
'
oService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege"
Err.Clear
else
wscript.echo L_Text_Msg_General05_Text & L_Space_Text & L_Error_Text _
& L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text _
& Err.Description
end if
else
wscript.echo L_Text_Msg_General04_Text & L_Space_Text & L_Error_Text _
& L_Space_Text & L_Hex_Text & hex(Err.Number) & L_Space_Text _
& Err.Description
end if
WmiConnect = bResult
end function
'
' Remove leading "\\" from server name
'
function RemoveBackslashes(strServer)
dim strRet
strRet = strServer
if Left(strServer, 2) = "\\" and Len(strServer) > 2 then
strRet = Mid(strServer, 3)
end if
RemoveBackslashes = strRet
end function