VBS Script for mapping printer: How to do without .NET 3.5 framework?

-1

Our printers are assigned by computergroups that have (almost) the same name as the printers (e.g. "prn-accounting" etc.). The script looks up the ad printer groups the computer is in, removes all network printers and only assigns the ones that the groups the computer is in suggest.

It is working, but the problem is it needs the .NET 3.5 framework to function, because of the ArrayList it uses I guess.

Error Message: "Windows Script Host Error 0x80131700 in Line 2" and .NET 3.5 installation process starts...

My question is: How can it work without .NET 3.5? Is there a way to make it use .NET 4.8 instead? Or use arrays instead of arraylists? But how?...

Dim WSHShell, objNET, objSysInfo, objUser, strUserDN, strGroups, Group, GroupName, DruckerListe, i
Set DruckerListe = CreateObject("System.Collections.ArrayList")
Set WSHShell = WScript.CreateObject("WScript.Shell")
Set objNET = WScript.CreateObject("WScript.Network")
Set objSysInfo = WScript.CreateObject("ADSystemInfo")
strComputerDN = objSysInfo.ComputerName
Set objComputer = GetObject("LDAP://" & strComputerDN)
strGroups = objComputer.GetEx("memberOf")

For Each gruppe in strGroups
Set ThisGroup = GetObject("LDAP://" & gruppe)
GroupName = ThisGroup.CN
If Left(GroupName,3) = "prn" Or Left(GroupName,3) = "mfc" Or Left(GroupName,3) = "cop" Or Right(GroupName,7) = "armband" Or Left(GroupName,3) = "pdf" Then
DruckerListe.add "\\srv-print01\" & GroupName
End If
Next

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set networkPrinters = objWMIService.ExecQuery("Select * From Win32_Printer Where Network = TRUE")
For Each networkPrinter in networkPrinters 
    networkPrinter.Delete_
Next

Dim objNetwork
Set objNetwork = CreateObject("WScript.Network")
For Each drucker in DruckerListe
    If Right(drucker, 10) = "-nodefault" Then
        objNetwork.AddWindowsPrinterConnection Left(drucker,Len(drucker)-10)
    ElseIF Left(drucker, 7) = "armband" Then
        objNetwork.AddWindowsPrinterConnection drucker
    Else
        objNetwork.AddWindowsPrinterConnection drucker
        objNetwork.SetDefaultPrinter drucker
    End If
Next 
Set objNetwork = Nothing
vb.net
vbscript
asked on Stack Overflow Apr 8, 2021 by readyrockc

1 Answer

0

The problem is the ArrayList object that is defined in .Net 3.5

Set DruckerListe = CreateObject("System.Collections.ArrayList")

If you replace the ArrayList with a regular array, or another type of collection, you won't need .Net 3.5 (unless one of the other Classes are defined in .Net 3.5 as well)

answered on Stack Overflow Apr 8, 2021 by Geert Bellekens

User contributions licensed under CC BY-SA 3.0