映射前删除本地打印机

时间:2016-03-15 14:49:28

标签: printing vbscript

我有以下脚本根据AD分组映射打印机:

'---------------------------------------------------------------------------------------------
'-------------------------------MAPEAMENTO DE IMPRESSORAS-------------------------------------

on error resume next

'determines the user who just logged on
Set objSysInfo = CreateObject("ADSystemInfo")
Set WSHNetwork = CreateObject("WScript.Network")
'As soon as we tack on LDAP:// and construct an ADsPath we then bind to the user account in
'Active Directory and report back the groups the user belongs to; this can be done simply 
'by enumerating the values in the MemberOf attribute.


strUserPath = "LDAP://" & Replace(objSysInfo.UserName, "/", "\/")
Set objUser = GetObject(strUserPath)


For Each objGroup in objUser.Groups
  strGroupName = objGroup.CN


    '---------------------------------------------------------------------------------------------
    '--------------MAPEAMENTO DE IMPRESSORA DO GRUPO UM-------------------------------------------

    'Mapeamento de impressoras por grupo definindo a impressora comom padrão para aquele grupo.
      Select Case strGroupName
         Case "GrupodoAD1"
        WshNetwork.AddWindowsPrinterConnection "\\servidor\impressora1"
        WshNetwork.SetDefaultPrinter "\\servidor\impressora1"
      End Select

    '---------------------------------------------------------------------------------------------


    '---------------------------------------------------------------------------------------------
    '--------------MAPEAMENTO DE IMPRESSORA DO GRUPO DOIS-----------------------------------------

    'Mapeamento de impressoras por grupo definindo a impressora comom padrão para aquele grupo.
      Select Case strGroupName
         Case "GrupodoAD2"
        WshNetwork.AddWindowsPrinterConnection "\\servidor\impressora2"
        WshNetwork.SetDefaultPrinter "\\servidor\impressora2"
      End Select

    '---------------------------------------------------------------------------------------------  



    '==========Adicione seções adicionais de case para cada grupo do AD que tiver.+===============

    next

现在我被要求删除用户计算机上的一些本地打印机。上面的脚本在登录时运行。该条件虽然规定必须删除某些打印机(一些用户将计算机带回家,我们不想删除他们的家用打印机)。

有没有办法可以使用上面的现有脚本完成此操作?我该怎么做?我在哪里插入信息?我可以使用一个脚本删除多个打印机吗?

例如我知道我必须删除:

  • Samsung SCX-6545X系列PCL 6
  • Samsung SCX-6545X系列PCL 5
  • Samsung SCX-6545X Series PS

如果我有类似上面的名字,我可以用它来删除任何与名称部分相匹配的打印机吗?

最好的问候,并提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

这是我从某个地方找到的脚本,稍微调整一下以按设备名称删除打印机。请谨慎行事,理解并仔细调整以满足您的需求:

On Error Resume Next

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer where DeviceID = 'HP LaserJet P1505'")

For Each objPrinter In colInstalledPrinters

    objPrinter.Delete_

Next

我认为您可以替换“SELECT”语句中的equals子句以使用“IN”,这样您就可以检查所有打印机并在一次点击中删除它们。例如:

("Select * from Win32_Printer where DeviceID In ('HP LaserJet P1505', 'Printer_2', 'Printer_3')")