Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printers Script based on group membership question

Status
Not open for further replies.

teddysherri

Technical User
Nov 18, 2003
137
GB
Hello all,

I know very little about vbscripting but have been tasked with creating a vbscript to map all our network printers. This i found simple enough, but we would like to map printers based on group membership and this is where i have the problem. The problem occurs if i have a security group within a security group, for instance i tell the script to map printer X if in group IT, but within group IT there is another security group with a list of users which the script has no knowledge of. I am sure it's something simple i need to add in, so any information would be great.

Thanks in advance.
 
I used Richard Mueller's sample scripts to build my login script (which I'd be happy to share if you need me to), but I always think that disecting someone else's script is a good way to learn and that's how I learned :).

Let me know if you want my script. Richard's samples are at
Good luck!

h
 
Hi Withanh,

Thanks for that, i took a look at "Logon script 2" and ammended it for what i need. I run the script and get no errors but no printers either. I can post up the script if that would help?

Thanks
 
Option Explicit

Dim objNetwork, objSysInfo, strUserDN
Dim objGroupList, objUser, objFSO
Dim strComputerDN, objComputer

Set objNetwork = CreateObject("Wscript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objSysInfo = CreateObject("ADSystemInfo")
strUserDN = objSysInfo.userName
strComputerDN = objSysInfo.computerName

' Escape any forward slash characters, "/", with the backslash
' escape character. All other characters that should be escaped are.
strUserDN = Replace(strUserDN, "/", "\/")
strComputerDN = Replace(strComputerDN, "/", "\/")

' Bind to the user and computer objects with the LDAP provider.
Set objUser = GetObject("LDAP://" & strUserDN)
Set objComputer = GetObject("LDAP://" & strComputerDN)

On Error Resume Next

'Add a network printer if the computer is a member of the group.
' Make this printer the default.
If IsMember(objComputer, "ISL-UK-BAS-IT-HPLJ2100") = True Then
objNetwork.AddWindowsPrinterConnection "\\ISLUKBASFAP0003\ISL-UK-BAS-IT-LJ2100"
objNetwork.SetDefaultPrinter "\\ISLUKBASFAP0003\ISL-UK-BAS-IT-LJ2100"

Else

If IsMember(objcomputer, "RCS Domain Users") = True Then
objNetwork.AddWindowsPrinterConnection "ISLUKBASFAP0003\ISL-UK-BAS-HUB1-RICOHAFICIOMP6500"
End If

' Clean up.
Set objNetwork = Nothing
Set objFSO = Nothing
Set objSysInfo = Nothing
Set objGroupList = Nothing
Set objUser = Nothing
Set objComputer = Nothing

Function IsMember(ByVal objADObject, ByVal strGroup)
' Function to test for group membership.
' objGroupList is a dictionary object with global scope.

If (IsEmpty(objGroupList) = True) Then
Set objGroupList = CreateObject("Scripting.Dictionary")
End If
If (objGroupList.Exists(objADObject.sAMAccountName & "\") = False) Then
Call LoadGroups(objADObject, objADObject)
objGroupList.Add objADObject.sAMAccountName & "\", True
End If
IsMember = objGroupList.Exists(objADObject.sAMAccountName & "\" _
& strGroup)
End Function

Sub LoadGroups(ByVal objPriObject, ByVal objADSubObject)
' Recursive subroutine to populate dictionary object objGroupList.

Dim colstrGroups, objGroup, j

objGroupList.CompareMode = vbTextCompare
colstrGroups = objADSubObject.memberOf
If (IsEmpty(colstrGroups) = True) Then
Exit Sub
End If
If (TypeName(colstrGroups) = "String") Then
' Escape any forward slash characters, "/", with the backslash
' escape character. All other characters that should be escaped are.
colstrGroups = Replace(colstrGroups, "/", "\/")
Set objGroup = GetObject("LDAP://" & colstrGroups)
If (objGroupList.Exists(objPriObject.sAMAccountName & "\" _
& objGroup.sAMAccountName) = False) Then
objGroupList.Add objPriObject.sAMAccountName & "\" _
& objGroup.sAMAccountName, True
Call LoadGroups(objPriObject, objGroup)
End If
Set objGroup = Nothing
Exit Sub
End If
For j = 0 To UBound(colstrGroups)
' Escape any forward slash characters, "/", with the backslash
' escape character. All other characters that should be escaped are.
colstrGroups(j) = Replace(colstrGroups(j), "/", "\/")
Set objGroup = GetObject("LDAP://" & colstrGroups(j))
If (objGroupList.Exists(objPriObject.sAMAccountName & "\" _
& objGroup.sAMAccountName) = False) Then
objGroupList.Add objPriObject.sAMAccountName & "\" _
& objGroup.sAMAccountName, True
Call LoadGroups(objPriObject, objGroup)
End If
Next
Set objGroup = Nothing
End Sub

Function MapDrive(ByVal strDrive, ByVal strShare)
' Function to map network share to a drive letter.
' If the drive letter specified is already in use, the function
' attempts to remove the network connection.
' objFSO is the File System Object, with global scope.
' objNetwork is the Network object, with global scope.
' Returns True if drive mapped, False otherwise.

Dim objDrive

On Error Resume Next
If (objFSO.DriveExists(strDrive) = True) Then
Set objDrive = objFSO.GetDrive(strDrive)
If (Err.Number <> 0) Then
On Error GoTo 0
MapDrive = False
Exit Function
End If
If (objDrive.DriveType = 3) Then
objNetwork.RemoveNetworkDrive strDrive, True, True
Else
MapDrive = False
Exit Function
End If
Set objDrive = Nothing
End If
objNetwork.MapNetworkDrive strDrive, strShare
If (Err.Number = 0) Then
MapDrive = True
Else
Err.Clear
MapDrive = False
End If
On Error GoTo 0
End Function
End If
WScript.Quit

'End


 
You have all your subs/functions inside your if/then statement. Try this:

Code:
Option Explicit

Dim objNetwork, objSysInfo, strUserDN
Dim objGroupList, objUser, objFSO
Dim strComputerDN, objComputer

Set objNetwork = CreateObject("Wscript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objSysInfo = CreateObject("ADSystemInfo")
strUserDN = objSysInfo.userName
strComputerDN = objSysInfo.computerName

' Escape any forward slash characters, "/", with the backslash
' escape character. All other characters that should be escaped are.
strUserDN = Replace(strUserDN, "/", "\/")
strComputerDN = Replace(strComputerDN, "/", "\/")

' Bind to the user and computer objects with the LDAP provider.
Set objUser = GetObject("LDAP://" & strUserDN)
Set objComputer = GetObject("LDAP://" & strComputerDN)

On Error Resume Next

'Add a network printer if the computer is a member of the group.
' Make this printer the default.
If IsMember(objComputer, "ISL-UK-BAS-IT-HPLJ2100") = True Then
    objNetwork.AddWindowsPrinterConnection "\\ISLUKBASFAP0003\ISL-UK-BAS-IT-LJ2100"
    objNetwork.SetDefaultPrinter "\\ISLUKBASFAP0003\ISL-UK-BAS-IT-LJ2100"

End If

If IsMember(objcomputer, "RCS Domain Users") = True Then
    objNetwork.AddWindowsPrinterConnection "ISLUKBASFAP0003\ISL-UK-BAS-HUB1-RICOHAFICIOMP6500"
End If


' Clean up.
Set objNetwork = Nothing
Set objFSO = Nothing
Set objSysInfo = Nothing
Set objGroupList = Nothing
Set objUser = Nothing
Set objComputer = Nothing

Function IsMember(ByVal objADObject, ByVal strGroup)
    ' Function to test for group membership.
    ' objGroupList is a dictionary object with global scope.

    If (IsEmpty(objGroupList) = True) Then
        Set objGroupList = CreateObject("Scripting.Dictionary")
    End If
    If (objGroupList.Exists(objADObject.sAMAccountName & "\") = False) Then
        Call LoadGroups(objADObject, objADObject)
        objGroupList.Add objADObject.sAMAccountName & "\", True
    End If
    IsMember = objGroupList.Exists(objADObject.sAMAccountName & "\" _
        & strGroup)
End Function

Sub LoadGroups(ByVal objPriObject, ByVal objADSubObject)
    ' Recursive subroutine to populate dictionary object objGroupList.

    Dim colstrGroups, objGroup, j

    objGroupList.CompareMode = vbTextCompare
    colstrGroups = objADSubObject.memberOf
    If (IsEmpty(colstrGroups) = True) Then
        Exit Sub
    End If
    If (TypeName(colstrGroups) = "String") Then
        ' Escape any forward slash characters, "/", with the backslash
        ' escape character. All other characters that should be escaped are.
        colstrGroups = Replace(colstrGroups, "/", "\/")
        Set objGroup = GetObject("LDAP://" & colstrGroups)
        If (objGroupList.Exists(objPriObject.sAMAccountName & "\" _
                & objGroup.sAMAccountName) = False) Then
            objGroupList.Add objPriObject.sAMAccountName & "\" _
                & objGroup.sAMAccountName, True
            Call LoadGroups(objPriObject, objGroup)
        End If
        Set objGroup = Nothing
        Exit Sub
    End If
    For j = 0 To UBound(colstrGroups)
        ' Escape any forward slash characters, "/", with the backslash
        ' escape character. All other characters that should be escaped are.
        colstrGroups(j) = Replace(colstrGroups(j), "/", "\/")
        Set objGroup = GetObject("LDAP://" & colstrGroups(j))
        If (objGroupList.Exists(objPriObject.sAMAccountName & "\" _
                & objGroup.sAMAccountName) = False) Then
            objGroupList.Add objPriObject.sAMAccountName & "\" _
                & objGroup.sAMAccountName, True
            Call LoadGroups(objPriObject, objGroup)
        End If
    Next
    Set objGroup = Nothing
End Sub

Function MapDrive(ByVal strDrive, ByVal strShare)
    ' Function to map network share to a drive letter.
    ' If the drive letter specified is already in use, the function
    ' attempts to remove the network connection.
    ' objFSO is the File System Object, with global scope.
    ' objNetwork is the Network object, with global scope.
    ' Returns True if drive mapped, False otherwise.

    Dim objDrive

    On Error Resume Next
    If (objFSO.DriveExists(strDrive) = True) Then
        Set objDrive = objFSO.GetDrive(strDrive)
        If (Err.Number <> 0) Then
            On Error GoTo 0
            MapDrive = False
            Exit Function
        End If
        If (objDrive.DriveType = 3) Then
            objNetwork.RemoveNetworkDrive strDrive, True, True
        Else
            MapDrive = False
            Exit Function
        End If
        Set objDrive = Nothing
    End If
    objNetwork.MapNetworkDrive strDrive, strShare
    If (Err.Number = 0) Then
        MapDrive = True
    Else
        Err.Clear
        MapDrive = False
    End If
    On Error GoTo 0
End Function
WScript.Quit

'End
 
Also, I'd recommend removing the "on error resume next" When you're troubleshooting a script, this line makes it almost impossible to troubleshoot. If you comment the line out if there is an error, it will pop up and tell you where to look for the error.
 
Hi,

Thank you for your script and tip. However i still have the same problem, i recieve no errors and no printers, any ideas?

Thanks
 
Change this line
Code:
If IsMember(objComputer, "ISL-UK-BAS-IT-HPLJ2100") = True Then
to
Code:
If IsMember(objUser, "ISL-UK-BAS-IT-HPLJ2100") = True Then
and this line
Code:
If IsMember(objcomputer, "RCS Domain Users") = True Then
to
Code:
If IsMember(objUser, "RCS Domain Users") = True Then
I think the problem is you are checking to see if a computer is a member of that group and you should be checking to see if the user is a member of that group.

For troubleshooting, inside the if/then group, put
Code:
wscript.echo "Mapped printer"
if it does not give you this pop-up, you know you failed the if/then test.
 
Hi,

I decided to totally re-write my script, i can't work out how to do nested groups still so have just changed my security groups around. Thanks withannh
 
The problem may be because of your group names/types as well. I'm sorry you weren't able to work it out. Feel free to post back if you have more questions.

 
I've been thinking more about this and created a few nested security groups and a printer with your names and the script does work as you want. My best suggestion is to check your group names and make sure there are no typing errors in the script. Also, all my groups are Security Group - Global. Don't know if this makes a difference as well, but may be worth looking at.

There really shouldn't be any reason to change up your whole network structure to make the script work, it's much easier to change the script to work with your network.

h
 
Yeah, they are all security/global groups. I have copied and pasted the names of the groups into the script so i know it's not my spelling (this time) :)
 
Interesting for sure. I've posted my script here Feel free to take a look at it, see how it compares to yours. It's pretty functional, including logging all action to the screen and to the Event Log when the script is complete.

h
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top