View RSS Feed


VB Script for network printer deployment

Rate this Entry
by , 20th August 2012 at 02:48 PM (12603 Views)
VB script may be showing its age, but it's still incredibly useful. Printer deployment at my district is handled by means of a VB script that collects various bits of information that is then used to determine who gets what printers; said bits of information include: username, user group membership, user LDAP path, AD site name, computer name, computer group membership, and computer LDAP path. Obviously you don't need to use all of this, but this script will still pull it out for you and you can customize to your needs.

The sample script below I left in debug mode. You can run as is and it'll bring up the collected information in message boxes. In addition, the sub that pulls group membership calls out to a function that will in turn call back on itself for every nested group it finds. This will add nested group membership to the final results. I hope this helps...

Edit: The strExpectedPrtName string is derived from the computer name. Most machines in the district are named by building, room, and then number. For instance, a teacher computer in room 12 in building A would be: BDA-RM12-TW01. Classroom printers are named the same way, just minus the workstation designation, so it would be: BDA-RM12. So the at the end of the printer sub it'll attempt to add a printer with the name it derived from the classroom workstation name. If it doesn't exist, it'll simply skip over it an finish normally due to the error handling.

 Dim strUserName
Dim strUserPath
Dim strADSite
Dim strCompName
Dim strCompPath
Dim strGroups
Dim strUserGroups
Dim strCompGroups

Call InitializeScript()
Call GetGroupMemberships
Call Debug
'Call Printers

Sub InitializeScript()
	Set objNetwork = CreateObject("WScript.Network")
	Set objADSysInfo = CreateObject("ADSystemInfo")
	strUserName = objNetwork.UserName
	strUserPath = "LDAP://" & objADSysInfo.UserName
	strADSite =  objADSysInfo.SiteName
	strCompName = objNetwork.ComputerName
	strCompPath = "LDAP://" & objADSysInfo.ComputerName	    
End Sub

Sub Debug
	Msgbox "Username: " & strUserName
	Msgbox "User LDAP Path: " & strUserPath
	Msgbox "AD Site: " & strADSite
	Msgbox "Computer Name: " & strCompName
	Msgbox "LDAP Computer Path: " & strCompPath
	Msgbox "User is a member of the following groups: " & strUserGroups
	MsgBox "Computer is a member of the following groups:" & strCompGroups
End Sub

Sub GetGroupMemberships
	Set objUser = GetObject(strUserPath)
    Set colUserGroups = objUser.Groups
	Set objComp = GetObject(strCompPath)
    Set colCompGroups = objComp.Groups

    For Each objUserGroup in colUserGroups
        strGroups = strGroups & objUserGroup.CN & "|"
		strUserGroups = strGroups
		strGroups = ""
	For Each objCompGroup in colCompGroups
        strGroups = strGroups & objCompGroup.CN & "|"
		strCompGroups = strGroups
		strGroups = ""
End Sub

Function GetNested(objGroup)
    On Error Resume Next

    colMembers = objGroup.GetEx("memberOf")

    For Each strMember in colMembers
        strPath = "LDAP://" & strMember
        Set objNestedGroup = GetObject(strPath)
        strGroups = strGroups & objNestedGroup.CN & "|"
End Function	

Sub Printers
	Set objNetwork = CreateObject("WScript.Network")

	If Len(strCompName) < 5 Then
		strExpectedPrtName = ""
		Position = InStr(5, StrCompName, "-") - 1
		If Position < 0 Then Position = 5
		strExpectedPrtName = LCase(Left(strCompName, Position))
		intCompNumber = Right(strCompName,2)
	End If

	If InStr(LCase(strCompPath), "BuildingA") Then

		strPrintServer = "\\BldA-PrtSrv01\"
		objNetwork.AddWindowsPrinterConnection strPrintServer & "BldA-Copier"
		objNetwork.SetDefaultPrinter strPrintServer & "BldA-Copier"
	ElseIf InStr(LCase(strCompPath), "BuildingB") Then

		strPrintServer = "\\BldB-PrtSrv01\"

		objNetwork.AddWindowsPrinterConnection strPrintServer & "BldB-Copier"
		If InStr(LCase(strUserGroups), "staff") Then
			objNetwork.AddWindowsPrinterConnection strPrintServer & "BldB-OfficeCopier"
			objNetwork.AddWindowsPrinterConnection strPrintServer & "BldB-MediaCopy"
		End If

		If InStr(LCase(strUserGroups), "student") Then
			objNetwork.AddWindowsPrinterConnection strPrintServer & "BldB-LibPrt01"
			objNetwork.AddWindowsPrinterConnection strPrintServer & "BldB-LibPrt02"
		End If
	End If
	On Error Resume Next
	objNetwork.AddWindowsPrinterConnection strPrintServer & strExpectedPrtName

	If Err.Number <> 0 Then
		'Nothing to see here folks, move along.
		Exit Sub
		objNetwork.SetDefaultPrinter strPrintServer & strExpectedPrtName
	End If
End Sub

Updated 4th September 2012 at 10:16 PM by Duke5A

VB Script



Total Trackbacks 0
Trackback URL: