2014-04-25 29 views
0

而不是为每台计算机编写我想将使用相同打印机的计算机放入一个组中,并且如果任何计算机名称是该组的一部分,则它会说打印机。我已经尝试了许多不同的方式,并且无法将它单独指定为一组。它最终添加了所有的打印机。打印机脚本 - 试图创建组

on error resume next 
DIM ComputerName, RegEntry, CPWRIT_A, CPWRIT_B, CPWRNH_A, CPWRNH_B, CPWRNH_C 

`'***************_Get Computername_************************************************ 

RegEntry = "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\ComputerName" 
ComputerName = ReadRegValue(RegEntry) 

'**********************_Computer Groups_****************************************** 

CPWRIT_A = "CPWRIT00, CPWRIT01, CPWRIT02, CPWRIT03" 
CPWRIT_B = "CPWRIT04, CPWRIT05, CPWRIT06" 
CPWRNH_A = "CPWRNH01, CPWRNH02, CPWRNH03, CPWRNH04" 
CPWRNH_B = "CPWRNH05, CPWRNH06, CPWRNH07, CPWRNH08" 
CPWRNH_C = "CPWRNH09, CPWRNH10, CPWRNH11, CPWRNH12, CPWRNH13" 


'*****************_WR Nurse Hall_********************************************************* 

IF ComputerName(CPWRNH_A) THEN 

Set objNetwork = CreateObject("WScript.Network") 
objNetwork.AddWindowsPrinterConnection "\\hpdl120\Nurse1_WhitePaper" 
objNetwork.AddWindowsPrinterConnection "\\hpdl120\Nurse1_RX_Paper" 
objNetwork.AddWindowsPrinterConnection "\\cpwrnh03\DYMO_CPWRNH03" 
objNetwork.AddWindowsPrinterConnection "\\hpdl120\SAVIN_WRN" 
objNetwork.SetDefaultPrinter "\\hpdl120\Nurse1_WhitePaper" 

ELSE 
END IF 

IF ComputerName(CPWRNH_B) THEN 

Set objNetwork = CreateObject("WScript.Network") 
objNetwork.AddWindowsPrinterConnection "\\hpdl120\SAVIN_WRN" 
objNetwork.AddWindowsPrinterConnection "\\cpwrnh07\DYMO_CPWRNH07" 
objNetwork.SetDefaultPrinter "\\hpdl120\SAVIN_WRN" 

ELSE 
END IF 

IF ComputerName(CPWRNH_C) THEN 

Set objNetwork = CreateObject("WScript.Network") 
objNetwork.AddWindowsPrinterConnection "\\hpdl120\Nurse2_Tray1_RX" 
objNetwork.AddWindowsPrinterConnection "\\hpdl120\Nurse2_Tray2_PlainPaper" 
objNetwork.AddWindowsPrinterConnection "\\hpdl120\SAVIN_WRN" 
objNetwork.AddWindowsPrinterConnection "\\hpdl120\cdnurse_Dymo" 
objNetwork.SetDefaultPrinter "\\hpdl120\Nurse2_Tray2_PlainPaper" 

ELSE 
END IF 

'*****************_WR IT Dept_********************************************************* 

IF ComputerName(CPWRIT_A) THEN 

Set objNetwork = CreateObject("WScript.Network") 
objNetwork.AddWindowsPrinterConnection "\\hpdl120\WR_IT" 
objNetwork.AddWindowsPrinterConnection "\\hpdl120\Savin_Admin" 
objNetwork.AddWindowsPrinterConnection "\\cpsysop01\SAVIN_AdminTray4" 
objNetwork.SetDefaultPrinter "\\hpdl120\WR_IT" 

ELSE 
END IF 

IF ComputerName(CPWRIT_B) THEN 

Set objNetwork = CreateObject("WScript.Network") 
objNetwork.AddWindowsPrinterConnection "\\hpdl120\WR_IT" 
objNetwork.AddWindowsPrinterConnection "\\hpdl120\Savin_Admin" 
objNetwork.AddWindowsPrinterConnection "\\cpsysop01\SAVIN_AdminTray4" 
objNetwork.SetDefaultPrinter "\\hpdl120\Savin_Admin" 

ELSE 
END IF 

'******************************************************************************* 

wscript.quit 

'*****************_Returns the data in the registry value_********************** 

FUNCTION ReadRegValue(ByVal RegValue) 
DIM WSHShell 
SET WSHShell = WScript.CREATEOBJECT("WScript.Shell") 
ReadRegValue = "" 
ON ERROR RESUME NEXT 
ReadRegValue = WSHShell.RegRead(RegValue) 
END FUNCTION 
+0

您应该使用Windows网络安全策略和专门的管理工具,据我所知您在域控网络。不要试图制作自制脚本,因为这样只会损害您的网络安全。 –

回答

0
if instr(CPWRIT_A,computername) > 0 then 

删除所有的别人的。或者将它们改为elseif(这会更快,但是一旦找到匹配就停止检查)。

if instr(CPWRIT_A,computername) > 0 then 
    'dah dah dah 
elseif instr(CPWRNH_A,computername) > 0 then 
    'dah dah dah 
elseif instr(CPWRIT_B,computername) > 0 then 
    'dah dah dah 
endif 
+0

PS:它添加所有打印机的原因是它对每个非法if语句都有错误。但是,您使用On Error Resume Next隐藏错误并且没有看到它。所以它只是进入下一行。 –