lakshjoshi Ответов: 1

Создание vbscript для чтения пользователь принадлежит к какой группе включая вложенную группу но не может получить список вложенных групп


я использую vbscript для чтения пользователь принадлежит к какой группе:

strUsers = inputbox("Enter user name")

'**************************************************************************
'*****                DO NOT EDIT BEYOND THIS POINT                   *****
'**************************************************************************


arrUsers = Split(strUsers, ",")
Set fs = CreateObject ("Scripting.FileSystemObject")
Set outFile = fs.CreateTextFile (".\User Group Membership Report.txt")

outFile.WriteLine
outFile.WriteLine "                       User Group Membership Report"
outFile.WriteLine "                       ----------------------------"
outFile.WriteLine
outFile.WriteLine "Run on: " & Now()
For x = 0 to UBound(arrUsers)
	arrUsers(x) = Trim(arrUsers(x))
	If x = 0 Then
		outFile.WriteLine " Users: " & arrUsers(x)
	Else
		outFile.WriteLine "        " & arrUsers(x)
	End If
Next 
outFile.WriteLine "=================================================================="
outFile.WriteLine
For x = 0 to UBound(arrUsers)
	Call getGroups(arrUsers(x))
	outFile.WriteLine "------------------------------------------------------------------"
Next

Wscript.echo "Done!"


Sub getGroups(strUser)
	Set oNetwork = CreateObject("WScript.Network")
	strDomain = oNetwork.UserDomain
	On Error Resume Next
	Set oUser = GetObject("WinNT://" & strDomain & "/" & strUser & ",user")
	If Err.Number <> 0 Then
		outFile.WriteLine strUser & " not found..."
		Exit Sub
	End If
	Err.Clear
	On Error Goto 0
	
	outFile.WriteLine strUser & " - " & oUser.FullName

	For Each strGroup In oUser.Groups
		outFile.WriteLine "     " & strGroup.Name
            
	Next
Set GroupCollection = oUser.Groups
	WScript.Echo "User " & StrUserName & " is a Member of all the following Groups:"
	For Each ObjGroup In GroupCollection
		outFile.WriteLine "  >> " & Trim(ObjGroup.Name)
		' -- Now List all Nested Groups of which the User is a Member
		GetNested ObjGroup.Name
	Next
End Sub
Function GetNested(objGroup)
    On Error Resume Next
outFile.WriteLine "  checking for nested group>> "
   Set objSysInfo = CreateObject("ADSystemInfo")
   strUser = objSysInfo.UserName
   Set objUser = GetObject("LDAP://" & strUser)
    For Each strMember in objUser
        strPath = "LDAP://" & strMember
        Set objNestedGroup = GetObject(strPath)
        strGroupList = strGroupList & "[" + objNestedGroup.Name & "] "
outFile.WriteLine "  >> " & strGroupList
        GetNested(objNestedGroup)
    Next
End Function


я получаю группы, но не вложенные группы..пожалуйста помочь.

Что я уже пробовал:

я попробовал с LDAP его работать,но он не может читать некоторые группы,поэтому я решил пойти с winnt// .так что я могу читать группы, но не вложенные группы

CHill60

Для начала вы ничего не возвращаете из функции GetNested. Попробуйте избавиться от "On Error Resume Next", и сообщение об ошибке может помочь вам выяснить, что случилось

lakshjoshi

хорошо ... спасибо за помощь, я проверю это

1 Ответов

Рейтинг:
0

lakshjoshi

наконец я получил решение читать вложенную группу.

'On Error Resume Next

'Set objADSysInfo = CreateObject("ADSystemInfo")

'strUser = objADSysInfo.UserName

'Set objUser = GetObject("LDAP://" & strUser)

'For Each strGroup in objUser.memberOf

'    Set objGroup = GetObject("LDAP://" & strGroup)
 '   Wscript.Echo objGroup
  '  Wscript.Echo objGroup.CN
   ' GetNested objGroup

'Next
dim temp
Set d = CreateObject("Scripting.Dictionary")
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_INITTYPE_GC = 3
  Const ADS_NAME_TYPE_1779 = 1
  Const ADS_NAME_INITTYPE_SERVER = 2
  Const ADS_NAME_INITTYPE_DOMAIN = 1
Wscript.Echo "==================================================="
Set objNetwork = WScript.CreateObject("WScript.Network")
'strUserName =objNetwork.Username
strUserName ="laxjoshi"
wscript.echo strUserName
strComputerName = objNetwork.ComputerName
strUserDomain = objNetwork.UserDomain
Set objUser = GetObject("WinNT://" & strUserDomain & "/" & strUserName & ",user")
strGroupList = ""
For Each objGroup In objUser.Groups
    'wscript.echo objGroup.Name
strGroupName=objGroup.Name
Set objTrans = CreateObject("NameTranslate")
objTrans.Init ADS_NAME_INITTYPE_DOMAIN, strUserDomain
	strNTName = strUserDomain & "\" & strGroupName
       objTrans.Set ADS_NAME_TYPE_NT4, strNTName
        strGroupDN = objTrans.Get(ADS_NAME_TYPE_1779)
  ' Escape any forward slash characters, "/", with the backslash
  ' escape character. All other characters that should be escaped are.
       strGroupDN = Replace(strGroupDN, "/", "\/")
'wscript.echo "strgroup="& strGroupDN 
 ' strGroupDN = "LDAP://" & strGroupDN
'wscript.echo "strdn="& strGroupDN 

Set objGroup = GetObject("LDAP://" & strGroupDN)
    'Wscript.Echo objGroup
    Wscript.Echo objGroup.CN
   strgroupname=objGroup.CN
'If IsMember(strUserDomain,strUserName,strgroupname) Then
 '   Wscript.echo "The user '"& strUserName &"' exists in the group '"&strgroupname&"'"
 'Else
  '  Wscript.echo "The user '"&strUserName&"' not exists in the group '"&strgroupname&"'"
'End If

   'd.add objGroup.CN
    GetNested objGroup




  'adsPathNT2DN = strGroupDN 
 'Set ngrp = GetObject("WinNT://" & strUserDomain & "/" & objGroup.Name & ",group")
'For Each member In ngrp.Members
'wscript.echo member.name
'If (StrComp(UCase(member.Class), "GROUP") = 0) Then
'wscript.echo "Group name="& member.name
'end if
'Next
'wscript.echo "=================================================="
next
wscript.echo "=================================================="

'If IsMember(strUserDomain,strUserName,State) Then
 '   Wscript.echo "The user '"& strUserName &"' exists in the group '"&State&"'"
 'Else
  '  Wscript.echo "The user '"&strUserName&"' not exists in the group '"&State&"'"
'End If
'*************************************************************************

  
' *****************************************************
'This function checks if the given AD user is member of the given group.
Function IsMember(domainName,userName,groupName)
   Set groupListD = CreateObject("Scripting.Dictionary")
   groupListD.CompareMode = 1
   ADSPath = domainName & "/" & userName
   Set objUser = GetObject("WinNT://" & ADSPath & ",user")
   For Each objGroup in objUser.Groups
      groupListD.Add objGroup.Name, "-"
   Next
   IsMember = CBool(groupListD.Exists(groupName))
End Function
'******************************************************************************
'******************************************
Function GetNested(objGroup)
    On Error Resume Next
    colMembers = objGroup.GetEx("memberOf")
    For Each strMember in colMembers

        strPath = "LDAP://" & strMember
        Set objNestedGroup = GetObject(strPath)
        strGroupList =  objNestedGroup.CN
      if  strGroupList <> "" and temp <> strGroupList then
        temp=strGroupList
	'strGroupList = Replace(strGroupList, "CN= ", "")
	strGroupList=trim(strGroupList)
        d.add strGroupList
	wscript.echo ">>>>>"& strGroupList 	
     End If
         
        GetNested(objNestedGroup)
    Next
End Function