简体   繁体   English

Windows Batch:使用rundll32处理错误

[英]Windows Batch: Handle errors with rundll32

I'm trying to remove a printer from the user's printer and devices. 我正在尝试从用户的打印机和设备中删除打印机。 To make sure that I delete the printer, I have a series of if statements to remove a printer on the network or locally and to remove a printer via name or via name plus server connection. 为了确保删除打印机,我有一系列的if语句来删除网络上或本地的打印机,并通过名称或名称加上服务器连接来删除打印机。

My problem is that when I test the success of the printer being removed, the errorlevel always reports being 0 regardless of whether the printer was not successfully removed or not. 我的问题是,当我测试成功删除打印机时,无论是否未成功删除打印机,错误errorlevel始终报告为0

Does anyone know a way to handle errors with rundll32 printui.dll,PrintUIEntry ? 有谁知道用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'

So rundll32 doesn't set an errorlevel , so rather than going through if statements to delete the correct printer, I just removed all printer connections with the code below. 因此, rundll32不会设置errorlevel ,因此,我没有通过if语句删除正确的打印机,而是使用以下代码删除了所有打印机连接。 This code does no error handling, but it could if necessary. 这段代码没有错误处理,但是如果需要的话可以。

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

This batch script was meant to fix network printer's configuration anyways, so I didn't try to delete locally connected printers. 无论如何,此批处理脚本旨在修复网络打印机的配置,因此我没有尝试删除本地连接的打印机。 Here's my whole script to remove and add network printers in case anyone wants a look. 这是我的整个脚本,用于删除和添加网络打印机,以备不时之需。

@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   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM