2014-07-08 44 views
0

我試圖從用戶的打印機和設備中刪除打印機。爲了確保我刪除打印機,我有一系列if語句來刪除網絡上或本地的打印機,並通過名稱或通過名稱和服務器連接刪除打印機。Windows批處理:使用rundll32處理錯誤

我的問題是,當我測試打印機被刪除成功時,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' 
+0

rundll32沒有設置ERRORLEVEL,因爲它不知道它運行的進程的結果。它只是運行這個過程。有關rundll32,請參閱[documentation](http://technet.microsoft.com/zh-cn/library/ee649171.aspx)。它運行的過程將不得不返回一個ERRORLEVEL,並且我沒有看到任何說PrintUI.DLL的PrintUIEntry這樣做的事情。 –

+1

是否有任何理由不使用'c:\ windows \ system32 \ prn * .vbs'文件進行打印機管理? –

+0

@MCND除了我的知識缺乏之外,沒有。可以使用這些文件中的一個刪除打印機,並且可以指示我使用這些文件的某些文檔嗎? – pbars23

回答

0

所以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