@echo off
Setlocal enabledelayedexpansion
reg query HKU\S-1-5-20>nul || (
echo;CreateObject^("Shell.Application"^).ShellExecute "%~f0", "", "", "runas", 1 > "%temp%\getadmin.vbs" && cscript //b "%temp%\getadmin.vbs" && exit /b & del "%temp%\getadmin.vbs" /f /q>nul 2>nul)
pushd "%CD%" && CD /D "%~dp0"
set TASK_NAME="PrinterOnlineMonitor"
set SCRIPT_PATH="%SystemRoot%\printer_monitor.vbs"
echo Creating monitor script...
(
rem 下一行修改你的打印机实际名称
echo Dim targetPrinterName
echo targetPrinterName = "HP LaserJet Professional CP1020 Series"
echo Dim maxRetryCount, retryInterval, checkInterval
echo maxRetryCount = 60
echo retryInterval = 5000
echo checkInterval = 30000
echo Set wmi = GetObject^("winmgmts:\\.\root\cimv2"^)
echo Set fso = CreateObject^("Scripting.FileSystemObject"^)
echo Set shell = CreateObject^("WScript.Shell"^)
echo Dim printerFound, printJobStarted, retryCount, lastCheckTime
echo printerFound = False
echo printJobStarted = False
echo retryCount = 0
echo lastCheckTime = Now
echo Do While retryCount ^< maxRetryCount
echo Set printers = wmi.ExecQuery^("SELECT * FROM Win32_Printer WHERE Name = '" ^& targetPrinterName ^& "'"^)
echo.
echo For Each printer In printers
echo printerFound = True
echo If Not printJobStarted Then
echo If IsPrinterReady^(printer^) Then
echo 'WScript.Echo "[" ^& Time ^& "] 打印机就绪,开始打印..."
echo Call PrintBlankPage
echo printJobStarted = True
echo retryCount = 0
echo lastCheckTime = Now
echo Else
echo If retryCount Mod 6 = 0 Then
echo 'WScript.Echo "[" ^& Time ^& "] 打印机不可用: " ^& GetPrinterStatusDetail^(printer^)
echo End If
echo End If
echo Else
echo If IsPrinterReady^(printer^) And DateDiff^("s", lastCheckTime, Now^) ^> 10 Then
echo If IsPrintJobComplete^(targetPrinterName^) Then
echo 'WScript.Echo "[" ^& Time ^& "] 打印完成!"
echo WScript.Quit 0
echo End If
echo lastCheckTime = Now
echo ElseIf DateDiff^("s", lastCheckTime, Now^) ^> 5 Then
echo lastCheckTime = Now
echo End If
echo End If
echo Next
echo If Not printerFound Then
echo If retryCount Mod 12 = 0 Then
echo 'WScript.Echo "[" ^& Time ^& "] 未找到名为 '" ^& targetPrinterName ^& "' 的打印机。"
echo End If
echo End If
echo retryCount = retryCount + 1
echo Dim waitTime
echo If printJobStarted Then
echo waitTime = 2000
echo Else
echo waitTime = retryInterval
echo If retryCount ^> 12 Then
echo waitTime = checkInterval
echo End If
echo End If
echo If retryCount ^< maxRetryCount Then
echo WScript.Sleep waitTime
echo End If
echo Loop
echo 'WScript.Echo "[" ^& Time ^& "] 错误:打印超时或失败。"
echo WScript.Quit 1
echo Function IsPrinterReady^(printer^)
echo Dim isReady
echo Dim isOnline
echo isOnline = ^(^(printer.Attributes And 128^) = 128^)
echo Dim isOffline
echo isOffline = ^(^(printer.Attributes And 256^) = 256^)
echo Dim hasError
echo hasError = ^(^(printer.PrinterStatus And 1^) = 1^) Or _
echo ^(^(printer.PrinterStatus And 2^) = 2^) Or _
echo ^(^(printer.PrinterStatus And 3^) = 3^) Or _
echo ^(^(printer.PrinterStatus And 4^) = 4^) Or _
echo ^(^(printer.PrinterStatus And 5^) = 5^)
echo Dim isInErrorState
echo isInErrorState = False
echo Select Case printer.PrinterState
echo Case 1:
echo isInErrorState = True
echo Case 2:
echo isInErrorState = True
echo Case 5:
echo isInErrorState = True
echo Case 6:
echo isInErrorState = True
echo Case 7:
echo isInErrorState = True
echo Case 8:
echo isInErrorState = True
echo Case 9:
echo isInErrorState = True
echo Case 10:
echo isInErrorState = True
echo Case 11:
echo isInErrorState = True
echo Case 4194432:
echo isInErrorState = True
echo End Select
echo isReady = isOnline And ^(Not isOffline^) And ^(Not isInErrorState^) And _
echo ^(printer.PrinterState = 0 Or printer.PrinterState = 3 Or printer.PrinterState = 4^)
echo IsPrinterReady = isReady
echo End Function
echo Function GetPrinterStatusDetail^(printer^)
echo Dim statusText
echo statusText = "状态码: " ^& printer.PrinterState ^& "(" ^& GetPrinterStateDescription^(printer.PrinterState^) ^& ")"
echo Dim isOnline, isOffline
echo isOnline = ^(^(printer.Attributes And 128^) = 128^)
echo isOffline = ^(^(printer.Attributes And 256^) = 256^)
echo If Not isOnline Then
echo statusText = statusText ^& " [未联机]"
echo End If
echo If isOffline Then
echo statusText = statusText ^& " [脱机]"
echo End If
echo Select Case printer.PrinterState
echo Case 5
echo statusText = statusText ^& " [纸张问题]"
echo Case 6
echo statusText = statusText ^& " [无纸张]"
echo Case 7, 10
echo statusText = statusText ^& " [需要用户干预]"
echo Case 8
echo statusText = statusText ^& " [卡纸]"
echo Case 9
echo statusText = statusText ^& " [缺纸]"
echo Case 11
echo statusText = statusText ^& " [未就绪]"
echo Case 4194432
echo statusText = statusText ^& " [脱机状态]"
echo End Select
echo If printer.Status ^<^> "" Then
echo statusText = statusText ^& " - " ^& printer.Status
echo End If
echo GetPrinterStatusDetail = statusText
echo End Function
echo Function GetPrinterStateDescription^(state^)
echo Select Case state
echo Case 0: GetPrinterStateDescription = "就绪"
echo Case 1: GetPrinterStateDescription = "暂停"
echo Case 2: GetPrinterStateDescription = "错误"
echo Case 3: GetPrinterStateDescription = "空闲"
echo Case 4: GetPrinterStateDescription = "打印中"
echo Case 5: GetPrinterStateDescription = "纸张问题"
echo Case 6: GetPrinterStateDescription = "无纸张"
echo Case 7: GetPrinterStateDescription = "需要用户干预"
echo Case 8: GetPrinterStateDescription = "卡纸"
echo Case 9: GetPrinterStateDescription = "缺纸"
echo Case 10: GetPrinterStateDescription = "需要用户干预"
echo Case 11: GetPrinterStateDescription = "未就绪"
echo Case 16: GetPrinterStateDescription = "初始化"
echo Case 18: GetPrinterStateDescription = "预热"
echo Case 4194432: GetPrinterStateDescription = "脱机"
echo Case Else: GetPrinterStateDescription = "未知状态"
echo End Select
echo End Function
echo Function IsPrintJobComplete^(printerName^)
echo On Error Resume Next
echo Dim printJobs, job
echo Set printJobs = wmi.ExecQuery^("SELECT * FROM Win32_PrintJob"^)
echo IsPrintJobComplete = True
echo For Each job In printJobs
echo If InStr^(1, job.Name, printerName, vbTextCompare^) ^> 0 Then
echo ' JobStatus: 1 = Paused, 2 = Error, 3 = Deleting, 4 = Printing, 5 = Offline, 6 = Paperout, 7 = Printed
echo If job.JobStatus ^<^> 7 Then
echo Dim jobTime
echo jobTime = CDate^(Mid^(job.TimeSubmitted, 1, 4^) ^& "-" ^& _
echo Mid^(job.TimeSubmitted, 5, 2^) ^& "-" ^& _
echo Mid^(job.TimeSubmitted, 7, 2^) ^& " " ^& _
echo Mid^(job.TimeSubmitted, 9, 2^) ^& ":" ^& _
echo Mid^(job.TimeSubmitted, 11, 2^) ^& ":" ^& _
echo Mid^(job.TimeSubmitted, 13, 2^)^)
echo If DateDiff^("n", jobTime, Now^) ^< 5 Then
echo IsPrintJobComplete = False
echo Exit Function
echo End If
echo End If
echo End If
echo Next
echo End Function
echo Sub PrintBlankPage^(^)
echo Dim tempFile
echo tempFile = shell.ExpandEnvironmentStrings^("%%TEMP%%"^) ^& "\blank_print_" ^& Year^(Now^) ^& Month^(Now^) ^& Day^(Now^) ^& Hour^(Now^) ^& Minute^(Now^) ^& Second^(Now^) ^& ".txt"
echo Dim tf
echo Set tf = fso.CreateTextFile^(tempFile, True^)
echo tf.Write " "
echo tf.Close
echo On Error Resume Next
echo shell.Run "NOTEPAD.EXE /P """ ^& tempFile ^& """", 0, False
echo WScript.Sleep 3000
echo If fso.FileExists^(tempFile^) Then
echo On Error Resume Next
echo fso.DeleteFile^(tempFile^)
echo End If
echo Set tf = Nothing
echo End Sub
)> %SCRIPT_PATH%
echo Creating scheduled task...
schtasks /create /tn %TASK_NAME% /tr "%SystemRoot%\System32\wscript.exe %SCRIPT_PATH%" /sc onlogon /ru System /rl HIGHEST /f
2.39 KB, 下载次数: 4, 下载积分: 无忧币 -2
| 欢迎光临 无忧启动论坛 (http://bbs.c3.wuyou.net/) | Powered by Discuz! X3.3 |