'----------------------------------------------------------------------
'
' 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
generic打印机const kActionPause = 1
const kActionResume = 2
const kActionCancel = 3
const kActionList = 4
const kErrorSuccess = 0
const KErrorFailure = 1
const kNameSpace = "root\cimv2"
'
' Job status constants
'
const kJobPaused = 1
const kJobError = 2
const kJobDeleting = 4
const kJobSpooling = 8
const kJobPrinting = 16
const kJobOffline = 32
const kJobPaperOut = 64
const kJobPrinted = 128
const kJobDeleted = 256
const kJobBlockedDevq = 512
const kJobUserInt = 1024
const kJobRestarted = 2048
const kJobComplete = 4096
'
' Generic strings
'
const L_Empty_Text = ""
const L_Space_Text = " "
const L_Error_Text = "错误"
const L_Success_Text = "成功"
const L_Failed_Text = "失败"
const L_Hex_Text = "0x"
const L_Printer_Text = "打印机"
const L_Operation_Text = "操作"
const L_Provider_Text = "提供程序"
const L_Description_Text = "描述"
const L_Debug_Text = "调试:"
'
' General usage messages
'
const L_Help_Help_General01_Text = "用法: prnjobs [-zmxl?] [-s 服务器][-p 打印机][-j 作业 id][-u 用户名][-w 密码]"
const L_Help_Help_General02_Text = "参数:"
const L_Help_Help_General03_Text = "-j - 作业 id"
const L_Help_Help_General04_Text = "-l - 列出所有作业"
const L_Help_Help_General05_Text = "-m - 恢复作业"
const L_Help_Help_General06_Text = "-p - 打印机名"
const L_Help_Help_General07_Text = "-s - 服务器名"
const L_Help_Help_General08_Text = "-u - 用户名"
const L_Help_Help_General09_Text = "-w - 密码"
const L_Help_Help_General10_Text = "-x - 取消作业"
const L_Help_Help_General11_Text = "-z - 暂停作业"
const L_Help_Help_General12_Text = "-? - 显示命令用法"
const L_Help_Help_General13_Text = "例如:"
const L_Help_Help_General14_Text = "prnjobs -z -p 打印机 -j 作业 id"
const L_Help_Help_General15_Text = "prn
jobs -l -p 打印机"
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 = "请用 CScript 运行这个脚本。"
const L_Help_Help_Host02_Text = "这可以通过以下操作达到"
const L_Help_Help_Host03_Text = "1. 用 ""CScript script.vbs arguments"" 或"
const L_Help_Help_Host04_Text = "2. 用 ""CScript //H:CScript //S"" 将默认 Windows Scripting Host"
const L_Help_Help_Host05_Text = " 改为 CScript 并运行脚本 "
const L_Help_Help_Host06_Text = " ""script.vbs arguments""。"
'
' General error messages
'
const L_Text_Error_General01_Text = "无法决定脚本操作主机。"
const L_Text_Error_General02_Text = "无法分析命令行。"
const L_Text_Error_General03_Text = "Win32 错误代码"
'
' Miscellaneous messages
'
const L_Text_Msg_General01_Text = "无法枚举打印作业"
const L_Text_Msg_General02_Text = "已枚举的打印作业数量"
const L_Text_Msg_General03_Text = "无法设定打印作业"
const L_Text_Msg_General04_Text = "无法获取 SWbemLocator 对象"
const L_Text_Msg_General05_Text = "无法连接到 WMI 服务"
'
' Print job properties
'
const L_Text_Msg_Job01_Text = "作业 id"
const L_Text_Msg_Job02_Text = "打印机"
const L_Text_Msg_Job03_Text = "文档"
const L_Text_Msg_Job04_Text = "数据类型"
const L_Text_Msg_Job05_Text = "驱动程序名"
const L_Text_Msg_Job06_Text = "描述"
const L_Text_Msg_Job07_Text = "经过时间"
const L_Text_Msg_Job08_Text = "机器名"
const L_Text_Msg_Job09_Text = "通知"
const L_Text_Msg_Job10_Text = "所有者"
const L_Text_Msg_Job11_Text = "打印的页数"
const L_Text_Msg_Job12_Text = "参数"
const L_Text_Msg_Job13_Text = "大小"
const L_Text_Msg_Job14_Text = "开始时间"
const L_Text_Msg_Job15_Text = "结束时间"
const L_Text_Msg_Job16_Text = "状态"
const L_Text_Msg_Job17_Text = "提交的时间"
const L_Text_Msg_Job18_Text = "总页数"
'
' Job status strings
'
const L_Text_Msg_Status01_Text = "驱动程序无法打印作业"
const L_Text_Msg_Status02_Text = "已发送到打印机"
const L_Text_Msg_Status03_Text = "作业已被删除"
const L_Text_Msg_Status04_Text = "作业正在被删除"
const L_Text_Msg_Status05_Text = "这个作业有个错误"
const L_Text_Msg_Status06_Text = "打印机脱机"
const L_Text_Msg_Status07_Text = "打印机缺纸"
const L_Text_Msg_Status08_Text = "作业已被暂停"
const L_Text_Msg_Status09_Text = "作业已打印"
const L_Text_Msg_Status10_Text = "作业正在打印"
const L_Text_Msg_Status11_Text = "作业已被重新启动"
const L_Text_
Msg_Status12_Text = "作业缓存"
const L_Text_Msg_Status13_Text = "打印机有问题,需要用户干涉"
'
' Action strings
'
const L_Text_Action_General01_Text = "暂停"
const L_Text_Action_General02_Text = "继续"
const L_Text_Action_General03_Text = "取消"
'
' Debug messages
'
const L_Text_Dbg_Msg01_Text = "于函数 ListJobs"
const L_Text_Dbg_Msg02_Text = "于函数 ExecJob"
const L_Text_Dbg_Msg03_Text = "于函数 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
ho(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
& 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 for
m "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
& Mid(CStr(oJob.ElapsedTime), 11, 2) & ":" _
& Mid(CStr(oJob.ElapsedTime), 13, 2)
if CStr(oJob.StartTime) <> "********000000.000000+000" and _
CStr(oJob.UntilTime) <> "********000000.000000+000" then
& Mid(Mid(CStr(oJob.StartTime), 9, 4), 3, 2)
& Mid(Mid(CStr(oJob.UntilTime), 9, 4), 3, 2)
end if
& 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)
Err.Clear
end if
next
ListJobs = kErrorSuccess
end function
'
' Convert the job status from bit mask to string
'
function JobStatusToString(Status)
on error resume next
dim strString
strString = L_Empty_Text
if (Status and kJobPaused) = kJobPaused then strString = strString & L_Text_Msg_Status08_Text & L_Space_Text end if
if (Status and kJobError) = kJobError then strString = strString & L_Text_Msg_Status05_Text & L_Space_Text end if
if (Status and kJobDeleting) = kJobDeleting then strString = strString & L_Text_Msg_Status04_T
ext & L_Space_Text end if
if (Status and kJobSpooling) = kJobSpooling then strString = strString & L_Text_Msg_Status12_Text & L_Space_Text end if
if (Status and kJobPrinting) = kJobPrinting then strString = strString & L_Text_Msg_Status10_Text & L_Space_Text end if
if (Status and kJobOffline) = kJobOffline then strString = strString & L_Text_Msg_Status06_Text & L_Space_Text end if
if (Status and kJobPaperOut) = kJobPaperOut then strString = strString & L_Text_Msg_Status07_Text & L_Space_Text end if
if (Status and kJobPrinted) = kJobPrinted then strString = strString & L_Text_Msg_Status09_Text & L_Space_Text end if
if (Status and kJobDeleted) = kJobDeleted then strString = strString & L_Text_Msg_Status03_Text & L_Space_Text end if
if (Status and kJobBlockedDevq) = kJobBlockedDevq then strString = strString & L_Text_Msg_Status01_Text & L_Space_Text end if
if (Status and kJobUserInt) = kJobUserInt then strString = strString & L_Text_Msg_Status13_Text & L_Space_Text end if
if (Status and kJobRestarted) = kJobRestarted then strString = strString & L_Text_Msg_Status11_Text & L_Space_Text end if
if (Status and kJobComplete) = kJobComplete then strString = strString & L_Text_Msg_Status02_Text & L_Space_Text end if
JobStatusToString = strString
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 = oSe
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论