How about this then...

I asked the same question a few weeks ago and finally came up with this script...

Option Explicit ' Force explicit declarations
'
' Variables
'
Dim WSHNetwork
Dim FSO
Dim strUserName ' Current user
Dim strUserDomain ' Current User's domain name
Dim ObjGroupDict ' Dictionary of groups to which the user belongs
Dim strDrive ' Drive letter
Dim strShare ' Share path (UNC)
Dim colDrives ' Enumerate Drives
Dim i ' Another variable

Set WSHNetwork = WScript.CreateObject("WScript.Network")
Set FSO = CreateObject("Scripting.FileSystemObject")
'
' Wait until the user is really logged in...
'
strUserName = ""
While strUserName = ""
WScript.Sleep 100 ' 1/10 th of a second
strUserName = WSHNetwork.UserName
Wend
strUserDomain = WSHNetwork.UserDomain

' Read the user's account "Member Of" tab info across the network
' once into a dictionary object.

Set ObjGroupDict = CreateMemberOfObject(strUserDomain, strUserName)

'GROUP1
If MemberOf(ObjGroupDict, "GROUP1") Then
'Map network Drives
Call ConnectNetworkDrives("W:", "\\SERVER\SHARE1")
'Connect Printer(s)
WSHNetwork.AddWindowsPrinterConnection "\\SERVER\PRINTER1"
'Set Default printer
'WSHNetwork.SetDefaultPrinter "\\SERVER\PRINTER1"
End If


'GROUP2
If MemberOf(ObjGroupDict, "GROUP2") Then
'Map Network Drives
'Call ConnectNetworkDrives("<LETTER>:", "\\<SERVER>\<SHARE>")
'Connect Printer(s)
WSHNetwork.AddWindowsPrinterConnection "\\SERVER\PRINTER2"
'Set Default printer
'WSHNetwork.SetDefaultPrinter "\\SERVER\PRINTER2"
End If

Function MemberOf(ObjDict, strKey)
' Given a Dictionary object containing groups to which the user
' is a member of and a group name, then returns True if the group
' is in the Dictionary else return False.
'
' Inputs:
' strDict - Input, Name of a Dictionary object
' strKey - Input, Value being searched for in
' the Dictionary object
' Sample Usage:
'
' If MemberOf(ObjGroupDict, "DOMAIN ADMINS") Then
' wscript.echo "Is a member of Domain Admins."
' End If
'
'
MemberOf = CBool(ObjGroupDict.Exists(strKey))

End Function


Function CreateMemberOfObject(strDomain, strUserName)
' Given a domain name and username, returns a Dictionary
' object of groups to which the user is a member of.
'
' Inputs:
'
' strDomain - Input, NT Domain name
' strUserName - Input, NT username
'
Dim objUser, objGroup

Set CreateMemberOfObject = CreateObject("Scripting.Dictionary")
CreateMemberOfObject.CompareMode = vbTextCompare
Set objUser = GetObject("WinNT://" & strDomain & "/" & strUserName & ",user")
For Each objGroup In objUser.Groups
CreateMemberOfObject.Add objGroup.Name, "-"
Next
Set objUser = Nothing

End Function

Function ConnectNetworkDrives(strDrive, strShare)
' Input drive letter and share path
' This function connects a drive letter to a specified share share

' Inputs:

' strDrive = Drive letter e.g. "W:"
' strShare = Share path e.g. "\\Server\Share"

Set colDrives = wshNetwork.EnumNetworkDrives
For i = 0 To colDrives.Count - 1 Step 2
If (colDrives(i) = strDrive) Then
wshNetwork.RemoveNetworkDrive strDrive
End If
Next
wshNetwork.MapNetworkDrive strDrive, strShare
End Function







From: "Chris Levis" <[EMAIL PROTECTED]>
Reply-To: "NT 2000 Discussions" <[EMAIL PROTECTED]>
To: "NT 2000 Discussions" <[EMAIL PROTECTED]>
Subject: RE: VBScript, getting group membership?
Date: Mon, 18 Nov 2002 13:21:08 -0500

This is definitely a step in the right direction.  Thanks!

> -----Original Message-----
> From: Lich, Brian M. [mailto:[EMAIL PROTECTED]]
> Sent: Monday, November 18, 2002 1:06 PM
> To: NT 2000 Discussions
> Subject: RE: VBScript, getting group membership?
>
>
> Try this function.  I am not sure where I found it, but have
> had it for
> a while:
>
> Function IsMember(sGroup)
>     Dim sAdsPath, oUser, oGroup
>
>     If IsEmpty(oGroupDict) Then
>         Set oGroupDict         = CreateObject("Scripting.Dictionary")
>         oGroupDict.CompareMode = vbTextCompare
>
>         sAdsPath  = oNet.UserDomain & "/" & oNet.UserName
>         Set oUser = GetObject("WinNT://" & sAdsPath & ",user")
>
>         For Each oGroup In oUser.Groups
>             oGroupDict.Add oGroup.Name, "-"
>         Next
>
>         Set oUser = Nothing
>     End If
>
>     IsMember = CBool(oGroupDict.Exists(sGroup))
> End Function
>
> It works well for me.
>
> HTH...
> Brian
>
> -----Original Message-----
> From: Chris Levis [mailto:[EMAIL PROTECTED]]
> Sent: Monday, November 18, 2002 1:04 PM
> To: NT 2000 Discussions
> Subject: VBScript, getting group membership?
>
> After googling and msdn'ing, I've been unable to figure this out:
>
> How can I test a user's AD group membership using VBScript?  i.e., I
> want to do the vbscript equivalent of kixtart's "IF INGROUP".
>
> Any ideas/samples?
>
> Thanks a bunch!
>
>
>
>
>
> ___________________________
> Chris Levis
> Applied Geographics, Inc.
>
> There are 10 types of people in the world:  those who
> understand binary,
> and those who do not.
>
> ------
> You are subscribed as [EMAIL PROTECTED]
> Archives: http://www.swynk.com/sitesearch/search.asp
> To unsubscribe send a blank email to %%email.unsub%%
>
> ------
> You are subscribed as [EMAIL PROTECTED]
> Archives: http://www.swynk.com/sitesearch/search.asp
> To unsubscribe send a blank email to %%email.unsub%%
>

------
You are subscribed as [EMAIL PROTECTED]
Archives: http://www.swynk.com/sitesearch/search.asp
To unsubscribe send a blank email to %%email.unsub%%

_________________________________________________________________
MSN 8 helps eliminate e-mail viruses. Get 2 months FREE*. http://join.msn.com/?page=features/virus


------
You are subscribed as [email protected]
Archives: http://www.swynk.com/sitesearch/search.asp
To unsubscribe send a blank email to [EMAIL PROTECTED]

Reply via email to