根据 AD 组成员身份设置打印机

根据 AD 组成员身份设置打印机

我正在尝试为 Active Directory 创建一个登录脚本,用于在客户端计算机上安装打印机。我有一个我们现在正在使用的基本脚本,如下所示,该脚本从我们的打印服务器将一系列打印机安装到本地计算机。我参考了后续帖子一些想法,但它无助于解决我所遇到的一些挑战。

我面临以下挑战,似乎无法在网上找到答案:

  • 防止计算机每次运行脚本时强制安装打印机。理想情况下,它会先检查打印机是否已安装,然后再尝试再次安装。如果没有这个,机器启动速度会非常慢。
  • 查询机器所属的计算机组成员身份,然后根据返回的成员身份/案例分配默认打印机。

在此先感谢您的帮助。

=======================================
下面列出的代码
=======================================

On Error Resume Next

PrintServer = "\\PrintServer\HP 4000 - Area1"
set objNetwork = createobject("Wscript.Network")
objNetwork.AddWindowsPrinterConnection(PrintServer)
objNetwork.SetDefaultPrinter PrintServer

PrintServer = "\\PrintServer\HP 4250 - Area2"
set objNetwork = createobject("Wscript.Network")
objNetwork.AddWindowsPrinterConnection(PrintServer)

PrintServer = "\\PrintServer\HP 4350 - Area3"
set objNetwork = createobject("Wscript.Network")
objNetwork.AddWindowsPrinterConnection(PrintServer)

PrintServer = "\\PrintServer\Dell 5200 - Area4"
set objNetwork = createobject("Wscript.Network")
objNetwork.AddWindowsPrinterConnection(PrintServer)

PrintServer = "\\PrintServer\HP 4240 - Area5"
set objNetwork = createobject("Wscript.Network")
objNetwork.AddWindowsPrinterConnection(PrintServer)

PrintServer = "\\PrintServer\HP 4240 - Area6"
set objNetwork = createobject("Wscript.Network")
objNetwork.AddWindowsPrinterConnection(PrintServer)

PrintServer = "\\PrintServer\HP 4240 - Area7"
set objNetwork = createobject("Wscript.Network")
objNetwork.AddWindowsPrinterConnection(PrintServer)

PrintServer = "\\PrintServer\HP 4240 - Area8"
set objNetwork = createobject("Wscript.Network")
objNetwork.AddWindowsPrinterConnection(PrintServer)

PrintServer = "\\PrintServer\HP 4240 - Area9"
set objNetwork = createobject("Wscript.Network")
objNetwork.AddWindowsPrinterConnection(PrintServer)

答案1

有很多方法可以实现这一点。对于第 2 部分,我有一个 vbscript,它查询组成员身份并返回一个错误级别,您可以在批处理中使用。使用 powershell 也可以做到这一点。

以下是 vbscript:

 'On Error Resume Next
' GroupCheck - GjM - returns errorlevel 1 if user is member of group, else returns 0
' EX: groupcheck.vbs <groupname>
' 
'
option explicit
Dim objADSysInfo, strUser, objGroup, objNetwork, strGroup, objUser, group, bMatched
Dim strGroupToTest, objArgs

set objArgs = wscript.arguments
strGroupToTest = objargs(0)
bMatched = False

'************************
'Make no changes below this point (unless you know why!)
'************************

Set objADSysInfo = CreateObject("ADSystemInfo")
strUser = objADSysInfo.UserName
Set objUser = GetObject("LDAP://" & strUser)

For Each group in objUser.memberOf
    Set objGroup = GetObject("LDAP://" & group)
    If trim(objGroup.CN) = trim(strGroupToTest) Then 
          bMatched = True
      'wscript.echo "Group match"
        Exit For
    End If
Next

If bMatched then 
    'wscript.echo "User in group"
    wscript.quit 1
else
    'wscript.echo "User not in group"
    wscript.quit 0
End If

下面是调用它的批处理程序:

:: Test to see if we should run this script
cscript /nologo Groupcheck.vbs "groupname"
if %errorlevel% EQU 0 (
   echo Failed groupcheck, exiting...
   Goto :EOF
) Else (
   echo Passed group check....
   'map your printer here
)

对于第 1 部分,我找到了一些用于枚举打印机的 vbscript这里。查找 showprn 文件。还发现了有关安装打印机的以下信息从命令行这可能比您拥有的 vbscript 更简单。

答案2

当在 Windows 上似乎有些困难时,你应该问自己的第一个问题是“为什么这么难,也许还有其他方法”。在这种情况下,当你在登录脚本中映射打印机,您可能不应该这样做。您可以使用组策略首选项将初始设置应用于您想要的组,而无需编写脚本。

看一眼:

GP 首选项:添加新打印机,设置为默认打印机

您应该使用组策略首选项的一个原因

如何使用组策略首选项通过漫游配置文件动态映射打印机

答案3

如果可能的话,我会使用组策略来添加打印机。如果您必须使用 vbs,以下是我们用来对某些组执行各种操作(添加打印机、添加映射驱动器等)的 vbscript。我们将此脚本设置为通过组策略为所有用户运行。

Dim strComputerName
Dim strUserName
Dim strDomainName
Dim wshShell
Dim bForce
Dim bUpdateProfile
Dim adsobj

bForce = true
bUpdateProfile = true

on error resume next

Set wshNetwork = WScript.CreateObject( "WScript.Network" )
Set wshShell = WScript.CreateObject("WScript.Shell" )

do while wshNetwork.username = ""
    WScript.Sleep 250
loop

strComputerName = wshNetwork.computerName
strUserName = wshNetwork.userName
strDomainName = wshNetwork.userDomain

adspath = "WinNT://" & strDomainName & "/" & strUserName
set adsobj = getobject(adspath)

'========= MAKE CHANGES BELOW =========     

cRemoveExistingDrives()

'=== Map company-wide drives and printers
cMapNetworkDrive "S:", "\\server\Shared$"

cAddNetworkPrinter "\\server\HP3050"    


'=== Map Group Specific Drives and printers
for each prop in adsobj.groups
    select case UCASE(prop.name)
        case "ACCOUNTING USERS"
                    cAddNetworkPrinter "\\server\AcctngPrinter"
                    wshNetwork.SetDefaultPrinter "AcctngPrinter"
            cMapNetworkDrive "X:", "\\server\accounting$"
        case "HR USERS"
                    cAddNetworkPrinter "\\server\HRPrinter"
                    wshNetwork.SetDefaultPrinter "HRPrinter"
            cMapNetworkDrive "Y:", "\\server\hr$"
    end select
next


'========= NO CHANGES BELOW THIS LINE ===========

Sub cRemoveExistingDrives()
    on error resume next
    Dim colNetDrives
    Set colNetDrives = wshNetwork.EnumNetworkDrives

    If colNetDrives.Count = 0 then
        'No drives
    Else
        For x = 0 to colNetDrives.count-1 Step 2
            wshNetwork.RemoveNetworkDrive colNetDrives.Item(x), bForce, bUpdateProfile
        Next
    End If  
End Sub

Sub cMapNetworkDrive(strDriveLetter, strPath)
    on error resume next
    wshNetwork.RemoveNetworkDrive strDriveLetter, bForce, bUpdateProfile
    wshNetwork.MapNetworkDrive strDriveLetter, strPath
End Sub

Sub cAddNetworkPrinter(strPath)
    on error resume next
    wshNetwork.AddWindowsPrinterConnection strPath 
End Sub

答案4

我真的很喜欢使用基克斯塔特。它具有类似 C 的语法,您可以根据 AD 成员资格自定义登录脚本。

使用 Kixtart 您可以做到;

If InGroup("group1")
    $rc = AddPrinterConnection("\\server\printer_a")
    $rc = AddPrinterConnection("\\server\printer_b")
    $rc = AddPrinterConnection("\\server\printer_c")
EndIf

If InGroup("group2")
    $rc = AddPrinterConnection("\\server\printer_d")
    $rc = AddPrinterConnection("\\server\printer_e")
    $rc = AddPrinterConnection("\\server\printer_f")
EndIf

相关内容