PaulRKrueger
asked on
VB Script for Mapping Printers
Hello! I am working on a VB script to use during logon to map printers. I've found everything I need except 1 line. I need to evaluate whether the current user is a member of an Active Directory security group.
For example:
IF CurrentUser is a member of "6thFloor Users"
then <DO THIS STUFF>
I have all the "Stuff" I just need the IF statement.
Thanks!
For example:
IF CurrentUser is a member of "6thFloor Users"
then <DO THIS STUFF>
I have all the "Stuff" I just need the IF statement.
Thanks!
http://www.computerperformance.co.uk/vbscript/vbscript_group_user_memberof.htm
ASKER
That looks like it would help if I actually knew this stuff. Unfortunately, I don't. From what I can see that link has information on enumerating the groups of which the user is a member. I want to ask "if the current user is a member of a specific group."
http://cwashington.netreach.net/depo/view.asp?Index=1028&ScriptType=vbscript
There is a function in there called IsMember
There is a function in there called IsMember
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
this is the script needed to map printers using vb instaed of giving you part i rather put the whole thing
If InGroup("groupnamehere") Then
Call AddPrinter("hcgl","servern ame","prin tername")
objWshNetwork.setDefaultPr inter "\\servername\printername"
End If
Set WshNetwork = CreateObject("WScript.Netw ork")
WshNetwork.AddWindowsPrint erConnecti on "\\servername\printer"
WshNetwork.SetDefaultPrint er "\\servername\printer"
If InGroup("groupnamehere") Then
Call AddPrinter("hcgl","servern
objWshNetwork.setDefaultPr
End If
Set WshNetwork = CreateObject("WScript.Netw
WshNetwork.AddWindowsPrint
WshNetwork.SetDefaultPrint
ASKER
Will give it a shot this weekend and let you know. Thanks!
ASKER
I had lots of trouble working with inGroup. As I looked in other places (following your link) I ended up with the script below. The functions are WAY out of my league, but it works none the less!
-------------------------- ---------- ---------- ---------- ---------- ----------
Option Explicit
Dim objADObject, strGroup, objGroupList, objSysInfo, strUser, objUser, wshNetwork
' Get the user info
Set objSysInfo = CreateObject("ADSystemInfo ")
strUser = objSysInfo.UserName
Set objUser = GetObject("LDAP://" & strUser)
Set objADObject = GetObject(objUser.AdsPath)
Set wshNetwork = CreateObject("WScript.Netw ork")
' Do this stuff
strGroup = "COP_Corporate"
If IsMember(strGroup) Then
wshNetwork.AddWindowsPrint erConnecti on "\\tbcws121\Corp_Copier_b& W"
wshNetwork.AddWindowsPrint erConnecti on "\\tbcws121\Corp_Copier_Co lor"
End If
' Functions
Set objGroupList = Nothing
Set objADObject = Nothing
Function IsMember(strGroup)
' Function to test for group membership.
' strGroup is the NT name (sAMAccountName) of the group to test.
' objGroupList is a dictionary object, with global scope.
' Returns True if the user or computer is a member of the group.
If IsEmpty(objGroupList) Then
Call LoadGroups
End If
IsMember = objGroupList.Exists(strGro up)
End Function
Sub LoadGroups
' Subroutine to populate dictionary object with group memberships.
' objADObject is the user or computer object, with global scope.
' objGroupList is a dictionary object, with global scope.
Dim arrbytGroups, j
Dim arrstrGroupSids(), objGroup
Set objGroupList = CreateObject("Scripting.Di ctionary")
objGroupList.CompareMode = vbTextCompare
objADObject.GetInfoEx Array("tokenGroups"), 0
arrbytGroups = objADObject.Get("tokenGrou ps")
If TypeName(arrbytGroups) = "Byte()" Then
ReDim arrstrGroupSids(0)
arrstrGroupSids(0) = OctetToHexStr(arrbytGroups )
Set objGroup = GetObject("LDAP://<SID=" & arrstrGroupSids(0) _
& ">")
objGroupList(objGroup.sAMA ccountName ) = True
Set objGroup = Nothing
Exit Sub
End If
If UBound(arrbytGroups) = -1 Then
Exit Sub
End If
ReDim arrstrGroupSids(UBound(arr bytGroups) )
For j = 0 To UBound(arrbytGroups)
arrstrGroupSids(j) = OctetToHexStr(arrbytGroups (j))
Set objGroup = GetObject("LDAP://<SID=" & arrstrGroupSids(j) _
& ">")
objGroupList(objGroup.sAMA ccountName ) = True
Next
Set objGroup = Nothing
End Sub
Function OctetToHexStr(arrbytOctet)
' Function to convert OctetString (byte array) to Hex string.
Dim k
OctetToHexStr = ""
For k = 1 To Lenb(arrbytOctet)
OctetToHexStr = OctetToHexStr _
& Right("0" & Hex(Ascb(Midb(arrbytOctet, k, 1))), 2)
Next
End Function
--------------------------
Option Explicit
Dim objADObject, strGroup, objGroupList, objSysInfo, strUser, objUser, wshNetwork
' Get the user info
Set objSysInfo = CreateObject("ADSystemInfo
strUser = objSysInfo.UserName
Set objUser = GetObject("LDAP://" & strUser)
Set objADObject = GetObject(objUser.AdsPath)
Set wshNetwork = CreateObject("WScript.Netw
' Do this stuff
strGroup = "COP_Corporate"
If IsMember(strGroup) Then
wshNetwork.AddWindowsPrint
wshNetwork.AddWindowsPrint
End If
' Functions
Set objGroupList = Nothing
Set objADObject = Nothing
Function IsMember(strGroup)
' Function to test for group membership.
' strGroup is the NT name (sAMAccountName) of the group to test.
' objGroupList is a dictionary object, with global scope.
' Returns True if the user or computer is a member of the group.
If IsEmpty(objGroupList) Then
Call LoadGroups
End If
IsMember = objGroupList.Exists(strGro
End Function
Sub LoadGroups
' Subroutine to populate dictionary object with group memberships.
' objADObject is the user or computer object, with global scope.
' objGroupList is a dictionary object, with global scope.
Dim arrbytGroups, j
Dim arrstrGroupSids(), objGroup
Set objGroupList = CreateObject("Scripting.Di
objGroupList.CompareMode = vbTextCompare
objADObject.GetInfoEx Array("tokenGroups"), 0
arrbytGroups = objADObject.Get("tokenGrou
If TypeName(arrbytGroups) = "Byte()" Then
ReDim arrstrGroupSids(0)
arrstrGroupSids(0) = OctetToHexStr(arrbytGroups
Set objGroup = GetObject("LDAP://<SID=" & arrstrGroupSids(0) _
& ">")
objGroupList(objGroup.sAMA
Set objGroup = Nothing
Exit Sub
End If
If UBound(arrbytGroups) = -1 Then
Exit Sub
End If
ReDim arrstrGroupSids(UBound(arr
For j = 0 To UBound(arrbytGroups)
arrstrGroupSids(j) = OctetToHexStr(arrbytGroups
Set objGroup = GetObject("LDAP://<SID=" & arrstrGroupSids(j) _
& ">")
objGroupList(objGroup.sAMA
Next
Set objGroup = Nothing
End Sub
Function OctetToHexStr(arrbytOctet)
' Function to convert OctetString (byte array) to Hex string.
Dim k
OctetToHexStr = ""
For k = 1 To Lenb(arrbytOctet)
OctetToHexStr = OctetToHexStr _
& Right("0" & Hex(Ascb(Midb(arrbytOctet,
Next
End Function