Skip to content

Instantly share code, notes, and snippets.

@Rusketh
Last active February 16, 2024 14:44
Show Gist options
  • Save Rusketh/1539e9f5c4f41cae659303e21c98584e to your computer and use it in GitHub Desktop.
Save Rusketh/1539e9f5c4f41cae659303e21c98584e to your computer and use it in GitHub Desktop.
'------------------------------------------------------------------------------------
' Over Complicated Printer Logon script
' By Christopher Goluch aka Rusketh (2019)
'
' USAGE:
' Set this as a user login script by group policy.
' Change the settings section to match your sites set up.
' For every printer that you have on the print server, create a new security group with the same name as the printer.
' Add users & computers to the printer groups on active directory.
' After login, network printers will be deleted and all relevant printers will be mapped.
' The default printer will default to the last known default printer.
'
' Additonaly you have the option of providing printers to all the computers with in a set ou group.
' To do this, create a security group, named with a set prefix, inside the ou.
' Add the printer groups to this new security group.
'
' This script will also inherit printers from sub group memberships.
'
' VER: 0.8
'------------------------------------------------------------------------------------
option Explicit
on error resume next
'------------------------------------------------------------------------------------
' Settings / Config Section
'------------------------------------------------------------------------------------
Dim arrPrintServers
arrPrintServers = array("PrintServer1", "PrintServer2")
'^ Add the names or ips of your Print Servers.
Dim strAntiPrintGroup
strAntiPrintGroup = "PRN-NO-PRINTERS"
'^ If a machine or user is a member of this group, this print script will exit before making any changes.
Dim strPrintGroupPrefix
strPrintGroupPrefix = "PRN-"
'^ Set to empty string to disable,
' The Name Prefix of all the printer groups.
Dim bDeleteLocalPrinters
bDeleteLocalPrinters = FALSE
'^ Set to true to delete local printers.
Dim strDeleteLocalPrintersGroup
strDeleteLocalPrintersGroup = "PRN-DELETE-LOCAL"
'^ Set to empty string to disable
' Set the name of a group that will delete local printers.
Dim bEnableOUPrinters
bEnableOUPrinters = TRUE
'^ Set to true to enable ou printer groups.
Dim strPrintOUGroupPrefix
strPrintOUGroupPrefix = "OU-PRINTERS-"
'^ Set to empty string to disable,
' The Name Prefix of the printer groups in each ou.
Dim bNoDefaultLocal
bNoDefaultLocal = FALSE
'^ Set to true to prevent local printers from being the default printer.
Dim strNoDefaultLocalGroup
strNoDefaultLocalGroup = "PRN-DEFAULT-NONE-LOCAL"
'^ Set to empty string to disable
' Set the name of a group that will prevent local printers from being the default printer.
Dim strLogFilePath
strLogFilePath = "C:\users\%USER%\print-log.txt"
'^ Set to empty string to disable,
' Set the log directory for the script.
' Supports: %COMPUTER% and %USER%
' Recomended to be disabled.
'------------------------------------------------------------------------------------
' Set up script enviroment
'------------------------------------------------------------------------------------
Dim objShell
Set objShell = CreateObject( "WScript.Shell" )
Dim objNetwork
Set objNetwork = WScript.CreateObject("WScript.Network")
Dim strComputerName
strComputerName = objShell.ExpandEnvironmentStrings("%ComputerName%")
Dim objWMIService
Set objWMIService = GetObject("winmgmts:\\" & strComputerName & "\root\cimv2")
Dim objFileSystem
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Dim objSysInfo
Set objSysInfo = CreateObject("ADSystemInfo")
Dim strUserName
strUserName = objShell.ExpandEnvironmentStrings("%UserName%")
'------------------------------------------------------------------------------------
' Create and open a log file.
'------------------------------------------------------------------------------------
Dim objLogFile
Dim bLoggin
bLoggin = NOT (strLogFilePath = "")
IF bLoggin THEN
strLogFilePath = Replace(strLogFilePath, "%USER%", strUserName)
strLogFilePath = Replace(strLogFilePath, "%COMPUTER%", strComputerName)
Set objLogFile = objFileSystem.OpenTextFile(strLogFilePath, 2, true)
END IF
Function log(str)
IF bLoggin THEN
'objShell.run("cmd.exe /C echo " & str & ">>" & strLogFilePath)
objLogFile.WriteLine(str)
END IF
END Function
log("Running on " & strComputerName & " for " & strUserName)
'------------------------------------------------------------------------------------
' Util function for prefi checking
'------------------------------------------------------------------------------------
Function hasPrefix(str, prefix)
hasPrefix = FALSE
IF prefix = "" THEN
hasPrefix = TRUE
ELSE
Dim pref
pref = Mid(str, 1, Len(prefix))
hasPrefix = pref = prefix
END IF
END Function
'------------------------------------------------------------------------------------
' Create an array of assigned printers.
'------------------------------------------------------------------------------------
Dim boolNoPrinters
boolNoPrinters = FALSE
Dim arrMemberOf()
Dim objGroup
Dim intSize
intSize = 0
Function assignPrinter(strPrinter)
IF strPrinter = strAntiPrintGroup THEN
boolNoPrinters = TRUE
log("WARNING: Found membership for " & strAntiPrintGroup & " print script will exit.")
ELSEIF strPrinter = strDeleteLocalPrintersGroup THEN
bDeleteLocalPrinters = TRUE
log("WARNING: Found membership for " & strDeleteLocalPrintersGroup & " setting 'bDeleteLocalPrinters' to true.")
ELSEIF strPrinter = strNoDefaultLocalGroup THEN
bNoDefaultLocal = TRUE
log("WARNING: Found membership for " & strNoDefaultLocalGroup & " setting 'bNoDefaultLocal' to true.")
ELSEIF hasPrefix(strPrinter, strPrintGroupPrefix) THEN
ReDim Preserve arrMemberOf(intSize)
arrMemberOf(intSize) = strPrinter
intSize = intSize + 1
log("Found printer group membership: " & strPrinter)
ELSE
log("Skipped none printer group: " & strPrinter)
END IF
END Function
'------------------------------------------------------------------------------------
' Assign all the Computers assigned printer groups.
'------------------------------------------------------------------------------------
Dim j
j = 0
Dim strTestedGroup
Dim arrTestedGroups()
Function shouldCheckGroup(strGroupName)
FOR EACH strTestedGroup in arrTestedGroups
IF strTestedGroup = strGroupName THEN
shouldCheckGroup = FALSE
EXIT Function
END IF
NEXT
ReDim Preserve arrTestedGroups(j)
arrTestedGroups(j) = strGroupName
j = j + 1
shouldCheckGroup = TRUE
END Function
Function assignFromMembership(objGroupInfo)
IF shouldCheckGroup(objGroupInfo.cn) = TRUE THEN
log("Searching group for printers: " & objGroupInfo.cn)
Dim objGroup
Dim objPrinterGroup
FOR EACH objPrinterGroup IN objGroupInfo.getex("memberof")
On Error Resume Next
Set objGroup = GetObject("LDAP://" & objPrinterGroup)
assignPrinter(objGroup.cn)
assignFromMembership(objGroup)
NEXT
ELSE
log("Skipped searching group: " & objGroupInfo.cn)
log("Group allready applied")
END IF
END Function
Function assignFromGroup(strGroupName)
assignFromMembership(GetObject("LDAP://" & strGroupName))
END Function
assignFromGroup(objSysInfo.ComputerName)
assignFromGroup(objSysInfo.UserName)
'------------------------------------------------------------------------------------
' If we have enabled ou printers, find the current ou group
'------------------------------------------------------------------------------------
IF bEnableOUPrinters = TRUE THEN
Function getOU(objInfo)
getOU = Mid(objInfo.DistinguishedName, Len(objInfo.Name) + 2)
END Function
Dim objComputerInfo
Set objComputerInfo = GetObject("LDAP://" & objSysInfo.ComputerName)
Dim strComputerOU
strComputerOU = getOU(objComputerInfo)
'------------------------------------------------------------------------------------
' Find all Printer groups inside the ou
'------------------------------------------------------------------------------------
Function assignFromOU(strOU)
log("Searching for OU Printer Groups in: '" & strOU & "' ")
Dim objOUInfo
Set objOUInfo = GetObject("LDAP://" & strOU)
objOUInfo.Filter = Array("group")
Dim pref
Dim objOUGroup
FOR EACH objOUGroup in objOUInfo
pref = Mid(objOUGroup.cn, 1, Len(strPrintOUGroupPrefix))
IF pref = strPrintOUGroupPrefix THEN
log("Found Printer Group: " & objOUGroup.cn)
assignFromMembership(objOUGroup)
END IF
NEXT
END Function
assignFromOU(strComputerOU)
END IF
'------------------------------------------------------------------------------------
' If we are a member of the antio print group, then exit
'------------------------------------------------------------------------------------
IF boolNoPrinters = TRUE THEN
log("Print script disabled via group, exiting.")
IF bLoggin THEN
objLogFile.Close
END IF
WScript.Quit
END IF
'------------------------------------------------------------------------------------
' Grab all the existing printers.
'------------------------------------------------------------------------------------
Dim arrInstalledPrinters
Set arrInstalledPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
'------------------------------------------------------------------------------------
' Delete all existing printers and record witch one is default
'------------------------------------------------------------------------------------
Dim strDefaultPrinter
Dim objInstalledPrinter
log("Removing pre existing printers.")
FOR Each objInstalledPrinter IN arrInstalledPrinters
on error resume next
IF objInstalledPrinter.Default = -1 THEN
IF bNoDefaultLocal AND NOT objInstalledPrinter.Network THEN
log("Skipping current default local printer: " & objInstalledPrinter.Name)
ELSE
log("Current default printer: " & objInstalledPrinter.Name)
strDefaultPrinter = objInstalledPrinter.ShareName
END IF
END IF
IF objInstalledPrinter.Network OR bDeleteLocalPrinters THEN
log("Deleted printer: " & objInstalledPrinter.Name)
objInstalledPrinter.Delete_
END IF
NEXT
'------------------------------------------------------------------------------------
' Add all the printers assigned to this machine.
'------------------------------------------------------------------------------------
IF strDefaultPrinter = NULL OR strDefaultPrinter = "" THEN
strDefaultPrinter = arrMemberOf(0)
log("Setting first network printer '" & strDefaultPrinter & "' as default.")
ELSE
log("Current Default '" & strDefaultPrinter & ".")
END IF
Dim strPrintServer
Dim strPrinterGroup
log("Adding network printers from shares.")
FOR EACH strPrinterGroup IN arrMemberOf
FOR EACH strPrintServer IN arrPrintServers
on error resume next
objNetwork.AddWindowsPrinterConnection "\\" & strPrintServer & "\" & strPrinterGroup
log("Adding Printer: \\" & strPrintServer & "\" & strPrinterGroup)
IF Err.Number = 0 THEN
IF strPrinterGroup = strDefaultPrinter THEN
on error resume next
objNetwork.SetDefaultPrinter "\\" & strPrintServer & "\" & strDefaultPrinter
log("Set Default Printer: " & strDefaultPrinter)
END IF
EXIT FOR
ELSE
IF Err.Description <> "" THEN
log("Failed to add printer: \\" & strPrintServer & "\" & strPrinterGroup)
log("Error: " & Err.Description)
END IF
END IF
NEXT
NEXT
'------------------------------------------------------------------------------------
' Close then log file.
'------------------------------------------------------------------------------------
IF bLoggin THEN
objLogFile.Close
END IF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment