'==========================================================================
'
' NAME: CheckGroupmembersAgainstList.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.thespidersparlor.com[/URL]
' DATE : 12/9/2007
' COPYRIGHT (c) 2007 All Rights Reserved
'
' COMMENT: This script an many more can be found in
' The Admin Script Pack by The Spider's Parlor
' Work smarter, not harder!
'
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
' ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
' THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
' PARTICULAR PURPOSE.
'
' IN NO EVENT SHALL THE SPIDER'S PARLOR AND/OR ITS RESPECTIVE SUPPLIERS
' BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
' DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
' WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
' ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
' OF THIS CODE OR INFORMATION.
'
'==========================================================================
On Error Resume Next
Dim oShell,oDic, oFSO, oTextStream, objGroup, desiredGroupList, wantedUser, userDN
Const ADS_PROPERTY_APPEND = 3
Set oDic = CreateObject("Scripting.Dictionary")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("Wscript.Shell")
forceUseCScript
Sub forceUseCScript()
If Not WScript.FullName = WScript.Path & "\cscript.exe" Then
oShell.Popup "Launched using wscript. Relaunching...",3,"WSCRIPT"
oShell.Run "cmd.exe /k " & WScript.Path & "\cscript.exe //NOLOGO " & Chr(34) & WScript.scriptFullName & Chr(34),1,False
WScript.Quit 0
End If
End Sub
'Bind to the group
Set objGroup = GetObject("LDAP://CN=TSPAdmins,OU=TSP Users,DC=thespidersparlor,DC=local")
'Enumerate members into a dictionary object
For Each member In objGroup.Members
oDic.Add member.samAccountName, "member"
Next
'Open the text file containing the ;ist of desired users
Set oTextStream = oFSO.OpenTextFile("userlist.txt")
'make an array from the data file
desiredGroupList = Split(oTextStream.ReadAll, vbNewLine)
'close the data file
oTextStream.Close
For Each wantedUser In desiredGroupList
If Len(wantedUser > 0) Then
'Check if the user is in the group or not
If oDic.Exists(wantedUser) Then
WScript.Echo "Member found " & wantedUser
Else
'not a member so add them
WScript.Echo "Not a member, joining " & wantedUser & " to group."
userDN = GetUserDN(wantedUser)
If Err.Number = 0 Then
objGroup.PutEx ADS_PROPERTY_APPEND, "member", Array(userDN)
objGroup.SetInfo
End If
Err.Clear
End If
End If
Next
Public Function GetUserDN(ByVal vSAN)
'This function courtesty of K0b3 and FAQ faq329-5688
Dim oRootDSE, oConnection, oCommand, oRecordSet
Set oRootDSE = GetObject("LDAP://rootDSE")
Set oConnection = CreateObject("ADODB.Connection")
oConnection.Open "Provider=ADsDSOObject;"
Set oCommand = CreateObject("ADODB.Command")
oCommand.ActiveConnection = oConnection
oCommand.CommandText = "<LDAP://" & oRootDSE.get("defaultNamingContext") & _
">;(&(objectCategory=User)(samAccountName=" & vSAN & "));distinguishedName;subtree"
Set oRecordSet = oCommand.Execute
On Error Resume Next
GetUserDN = oRecordSet.Fields("DistinguishedName")
On Error GoTo 0
oConnection.Close
Set oRecordSet = Nothing
Set oCommand = Nothing
Set oConnection = Nothing
Set oRootDSE = Nothing
End Function