私の仕事では、ネストされたグループを含む、ユーザーが属するすべてのグループのリストを取得するスクリプトを VBScript で記述し、リスト全体で繰り返されるネストされたグループを取り出す必要があります (ネストされたグループをインデントし、さらにインデントします)。ネストされたグループのネストされたグループなど)
ユーザーが属しているグループのリスト全体を取得するスクリプトを、monimoy Sanyal が gallery.technet.microsoft.com で見つけて、自分のニーズに合わせようとしました。私が編集したスクリプトは次のとおりです。
Option Explicit
Const ForReading = 1, ForWriting = 2, ForAppend = 8
Dim ObjUser, ObjRootDSE, ObjConn, ObjRS
Dim GroupCollection, ObjGroup
Dim StrUserName, StrDomName, StrSQL
Dim GroupsList
Dim WriteFile
GroupsList = ""
Set ObjRootDSE = GetObject("LDAP://RootDSE")
StrDomName = Trim(ObjRootDSE.Get("DefaultNamingContext"))
Set ObjRootDSE = Nothing
StrUserName = InputBox("Enter user login", "Info needed", "")
StrSQL = "Select ADsPath From 'LDAP://" & StrDomName & "' Where ObjectCategory = 'User' AND SAMAccountName = '" & StrUserName & "'"
Set ObjConn = CreateObject("ADODB.Connection")
ObjConn.Provider = "ADsDSOObject": ObjConn.Open "Active Directory Provider"
Set ObjRS = CreateObject("ADODB.Recordset")
ObjRS.Open StrSQL, ObjConn
If Not ObjRS.EOF Then
ObjRS.MoveLast: ObjRS.MoveFirst
Set ObjUser = GetObject (Trim(ObjRS.Fields("ADsPath").Value))
Set GroupCollection = ObjUser.Groups
WScript.Echo "Looking for groups " & StrUserName & " is member of. This may take some time..."
'Groups with direct membership, and calling recursive function for nested groups
For Each ObjGroup In GroupCollection
GroupsList = GroupsList + ObjGroup.CN + VbCrLf
CheckForNestedGroup ObjGroup
Next
Set ObjGroup = Nothing: Set GroupCollection = Nothing: Set ObjUser = Nothing
'Writing list in a file named Groups <username>.txt
Set WriteFile = WScript.CreateObject("WScript.Shell")
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("Groups " & StrUserName & ".txt", ForWriting,true)
f.write(GroupsList)
f.Close
WScript.Echo "You can find the list in the Groups " &StrUserName & ".txt file that has just been created."
Else
WScript.Echo "Couldn't find user " & StrUserName & " in AD."
End If
ObjRS.Close: Set ObjRS = Nothing
ObjConn.Close: Set ObjConn = Nothing
'Recursive fucntion
Private Sub CheckForNestedGroup(ObjThisGroupNestingCheck)
On Error Resume Next
Dim AllMembersCollection, StrMember, StrADsPath, ObjThisIsNestedGroup
AllMembersCollection = ObjThisGroupNestingCheck.GetEx("MemberOf")
For Each StrMember in AllMembersCollection
StrADsPath = "LDAP://" & StrMember
Set ObjThisIsNestedGroup = GetObject(StrADsPath)
'Not include a group in the list if it is already in the list (does not work for some reason?)
If InStr(GroupsList, ObjThisIsNestedGroup.CN) = 0 Then
GroupsList = GroupsList + vbTab + ObjThisIsNestedGroup.CN + VbCrLf
End If
'Recursion to look for nested groups and nested groups of nested groups and nested groups of nested groups of nested groups and...
CheckForNestedGroup ObjThisIsNestedGroup
Next
Set ObjThisIsNestedGroup = Nothing: Set StrMember = Nothing: Set AllMembersCollection = Nothing
End Sub
元のスクリプトのように見つかった各グループのポップアップを表示するのではなく、リスト全体を文字列 (GroupsList = GroupsList + ObjGroup.CN + VbCrLf
直接グループの場合GroupsList = GroupsList + vbTab + ObjThisIsNestedGroup.CN + VbCrLf
、再帰関数でネストされたグループの場合) に保存し、スクリプトがグループの検索を完了すると、ファイル内の文字列。( f.write(GroupsList)
)
私の問題は、If "InStr(GroupsList, ObjThisIsNestedGroup.CN) = 0
再帰関数にもかかわらず、結果全体でまだ大量の繰り返しがあることに気付きます (私たちの AD はグループで肥大化しており、多くのネストされたグループとネストされたグループが他のネストされたグループにあるなどの巨大な構造です)。 .) そして、ObjThisIsNestedGroup.CN が既に GroupsList にあることにチェックが気付かないようです。そして、インデントを適切に実装する方法がわかりません。
何か案は?私はスクリプトを書くのはかなり新しいので、答えが明らかな場合はご容赦ください。