Printing 打印机脚本-尝试创建组

Printing 打印机脚本-尝试创建组,printing,vbscript,Printing,Vbscript,我想把使用相同打印机的计算机放在一个组中,而不是为每台计算机写,如果有任何计算机名称是该组的一部分,那么它就会被称为打印机。我已经尝试了很多不同的方法,但无法将其单独发送给指定的组。它最终添加了所有打印机 on error resume next DIM ComputerName, RegEntry, CPWRIT_A, CPWRIT_B, CPWRNH_A, CPWRNH_B, CPWRNH_C `'***************_Get Computername_*************

我想把使用相同打印机的计算机放在一个组中,而不是为每台计算机写,如果有任何计算机名称是该组的一部分,那么它就会被称为打印机。我已经尝试了很多不同的方法,但无法将其单独发送给指定的组。它最终添加了所有打印机

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
移除所有的ELSE。或者将它们更改为elseif(速度更快,但一旦找到匹配项,它就会停止检查)


您应该使用Windows网络安全策略和专门的管理工具,因为我知道您在域控制网络上。不要试图制作自制脚本,因为这样只会损害网络安全。PS:它添加所有打印机的原因是它会在每个非法if语句上出错。但是,您正在使用On Error Resume Next隐藏错误,而没有看到它。所以它就转到下一行。
if instr(CPWRIT_A,computername) > 0 then
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