batch-file - Windows批处理: Handle errors with rundll32

标签 batch-file error-handling

我正在尝试从用户的打印机和设备中删除打印机。为了确保删除打印机,我有一系列的if语句来删除网络上或本地的打印机,并通过名称或名称加上服务器连接来删除打印机。

我的问题是,当我测试成功删除打印机时,不管打印机是否被成功删除,errorlevel始终报告为0

有谁知道用rundll32 printui.dll,PrintUIEntry处理错误的方法?

rundll32 printui.dll,PrintUIEntry /dn /n "%printerName%"
if %ERRORLEVEL% NEQ 0 goto first else (goto after)

:first
rundll32 printui.dll,PrintUIEntry /dl /n "%printerName%"
if %ERRORLEVEL% NEQ 0 goto second else (goto after)

:second
rundll32 printui.dll PrintUIEntry /dl /n \\DC1\%printerName%
if %ERRORLEVEL% NEQ 0 goto third else (goto after)

:third
rundll32 printui.dll PrintUIEntry /dn /n \\DC1\%printerName%
goto after

:after
ECHO If printer was not deleted, click on the 'start' menu button, go to 'Devices and Printers', right click on the printer you want to remove, and select 'Remove Device'

最佳答案

因此,rundll32不会设置errorlevel,因此,我没有经过if语句来删除正确的打印机,而是删除了所有带有以下代码的打印机连接。这段代码没有错误处理,但是如果需要的话可以。

cscript "C:\Windows\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs" -xc

无论如何,此批处理脚本旨在修复网络打印机的配置,因此我没有尝试删除本地连接的打印机。这是我的整个脚本,用于删除和添加网络打印机,以防万一。
@ECHO off

ECHO To solve your printer configuration problem, we will first remove the printers having the problem.
pause

REM remove all printer connections
cscript "C:\Windows\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs" -xc
if %ERRORLEVEL% GTR 0 (ECHO If printer was not deleted, click on the 'start' menu button, go to 'Devices and Printers', right click on the printer you want to remove, and select 'Remove Device') else (goto end)

:end
ECHO Continue to printer installation?

pause

REM install printers
start \\DC1\LaserP2055
start \\DC1\LexmarkX544
ECHo The network printers LaserP2055 and LexmarkX544 have been installed
ECHO Would you like to exit the program?
pause
exit   

关于batch-file - Windows批处理: Handle errors with rundll32,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24639899/

相关文章:

windows - 批处理 : set a variable with a filename

c# - 在 C# 中使用 P/Invoke 注册 _set_purecall_handler 函数

oracle - 我的程序中不断出现错误,这些错误有助于添加新类(class),并在说明中提出了要求

ios - 将自定义数据包含到 iOS 故障转储中

php - PHP错误.htaccess限制文件夹深度

java - 处理Servlet中的异常和错误页面

batch-file - 在高级安装程序中自动在 .exe 之前运行 .bat 文件

windows - 如何在窗口 bat 中获取整个 svn 提交消息?

windows - 在for循环中从 token 设置变量

windows - 如何在批处理中迭代两个列表