本文介绍了创建用于读取用户的vbscript属于包含嵌套组但无法获取嵌套组列表的组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在使用vbscript阅读用户属于哪个组: strUsers = inputbox(输入用户名) '******************************************** ****************************** '*****不要在这个点之外编辑**** * '******************************************** ****************************** arrUsers = Split(strUsers,, )设置fs = CreateObject(Scripting.FileSystemObject)设置outFile = fs.CreateTextFile(.\User Group Membership Report.txt) outFile。 WriteLine outFile.WriteLine用户组成员报告 outFile.WriteLine---------------------------- outFile.WriteLine outFile.WriteLine继续:& Now() For x = 0 to UBound(arrUsers) arrUsers(x)= Trim(arrUsers(x)) if x = 0 Then outFile.WriteLineUsers :& arrUsers(x) Else outFile.WriteLine& arrUsers(x)结束如果下一个 outFile.WriteLine=========================== ======================================= outFile.WriteLine 对于x = 0到UBound(arrUsers)调用getGroups(arrUsers(x)) outFile.WriteLine-------------------- ----------------------------------------------下一个 Wscript.echo完成! Sub getGroups(strUser)设置oNetwork = CreateObject(WScript.Network) strDomain = oNetwork.UserDomain On Error Resume Next 设置oUser = GetObject(WinNT://& strDomain&/& strUser&,user)如果Err.Number<> 0然后 outFile.WriteLine strUser& 找不到......退出Sub 结束如果 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.EchoUser& StrUserName& 是以下所有群组的成员: For Each ObjGroup In GroupCollection outFile.WriteLine>>&修剪(ObjGroup.Name)' - 现在列出用户是其成员的所有嵌套组 GetNested ObjGroup.Name 下一个结束Sub 函数GetNested (objGroup) On Error Resume Next outFile.WriteLine检查嵌套组>>设置objSysInfo = CreateObject(ADSystemInfo) strUser = objSysInfo.UserName 设置objUser = GetObject(LDAP://& strUser) For objUser中的每个strMember strPath =LDAP://& strMember 设置objNestedGroup = GetObject(strPath) strGroupList = strGroupList& [+ objNestedGroup.Name& ] outFile.WriteLine>>& strGroupList GetNested(objNestedGroup)下一个结束函数 i我正在获取组但不是嵌套组..请帮助。 我尝试了什么: i尝试过使用LDAP工作,但它无法读取一些组,所以我决定使用winnt //。所以我能够读取组但不能嵌套组解决方案 最后我得到了解读嵌套组的解决方案。 'On Error Resume Next 'Set objADSysInfo = CreateObject(ADSystemInfo) 'strUser = objADSysInfo.UserName '设置objUser = GetObject(LDAP://& strUser) '对于objUser.memberOf中的每个strGroup '设置objGroup = GetObject(LDAP://& strGroup)'Wscript.Echo objGroup 'Wscript。 Echo objGroup.CN 'GetNested objGroup 'Next dim temp Set d = CreateObject(Scrip ting.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=============================================== ====设置objNetwork = WScript.CreateObject(WScript.Network)'strUserName = objNetwork.Username strUserName =laxjoshi wscript.echo strUserName strComputerName = objNetwork.ComputerName strUserDomain = objNetwork.UserDomain 设置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)'使用反斜杠'转义字符转义任何正斜杠字符/。应该转义的所有其他字符是。 strGroupDN =替换(strGroupDN,/,\ /)'wscript.echostrgroup =& strGroupDN 'strGroupDN =LDAP://& strGroupDN 'wscript.echostrdn =& strGroupDN 设置objGroup = GetObject(LDAP://& strGroupDN)'Wscript.Echo objGroup Wscript.Echo objGroup.CN strgroupname = objGroup .CN '如果是IsMember(strUserDomain,strUserName,strgroupname)那么'Wscript.echo用户'& strUserName&'存在于组'& strgroupname&''Else 'Wscript.echo用户'& strUserName&'不存在于组' & strgroupname&''结束如果 'd.add objGroup.CN GetNested objGroup 'adsPathNT2DN = strGroupDN '设置ngrp = GetObject(WinNT://& strUserDomain&/& objGroup.Name&,group)'对于每个成员在ngrp.Members 'wscript.echo member.name 'If(StrComp(UCase(member.Class),GROUP)= 0)然后'wscript.echo 群组名称=& member.name '结束如果'Next 'wscript.echo========================= ========================= next wscript.echo=========== ======================================= '如果IsMember(strUserDomain,strUserName,State)然后'Wscript.echo用户'& strUserName&'存在于组'& State&''Else 'Wscript.echo用户'& strUserName&'不存在于组' & State&''结束如果'******************************* ****************************************** '********************************************* ******** '此函数检查给定的AD用户是否是给定组的成员。 函数IsMember(domainName,userName,groupName)设置groupListD = CreateObject(Scripting.Dictionary) groupListD.CompareMode = 1 ADSPath = domainName& /& userName 设置objUser = GetObject(WinNT://& ADSPath&,user)对于objUser.Groups中的每个objGroup groupListD.Add objGroup.Name, - 下一个 IsMember = CBool(groupListD.Exists(groupName))结束函数'****************** ************************************************** ********** '*********************************** ******* 函数GetNested(objGroup) On Error Resume Next colMembers = objGroup.GetEx(memberOf) For colMembers $ b $中的每个strMember b strPath =LDAP://& strMember 设置objNestedGroup = GetObject(strPath) strGroupList = objNestedGroup.CN if strGroupList<> 和temp<> strGroupList然后 temp = strGroupList 'strGroupList = Replace(strGroupList,CN =,) strGroupList = trim(strGroupList) d.add strGroupList wscript.echo>>>>>& strGroupList End if GetNested(objNestedGroup) Next End Function i am using vbscript to read user belongs to which group: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.WriteLineoutFile.WriteLine " User Group Membership Report"outFile.WriteLine " ----------------------------"outFile.WriteLineoutFile.WriteLine "Run on: " & Now()For x = 0 to UBound(arrUsers)arrUsers(x) = Trim(arrUsers(x))If x = 0 ThenoutFile.WriteLine " Users: " & arrUsers(x)ElseoutFile.WriteLine " " & arrUsers(x)End IfNext outFile.WriteLine "=================================================================="outFile.WriteLineFor x = 0 to UBound(arrUsers)Call getGroups(arrUsers(x))outFile.WriteLine "------------------------------------------------------------------"NextWscript.echo "Done!"Sub getGroups(strUser)Set oNetwork = CreateObject("WScript.Network")strDomain = oNetwork.UserDomainOn Error Resume NextSet oUser = GetObject("WinNT://" & strDomain & "/" & strUser & ",user")If Err.Number <> 0 ThenoutFile.WriteLine strUser & " not found..."Exit SubEnd IfErr.ClearOn Error Goto 0outFile.WriteLine strUser & " - " & oUser.FullNameFor Each strGroup In oUser.GroupsoutFile.WriteLine " " & strGroup.Name NextSet GroupCollection = oUser.GroupsWScript.Echo "User " & StrUserName & " is a Member of all the following Groups:"For Each ObjGroup In GroupCollectionoutFile.WriteLine " >> " & Trim(ObjGroup.Name)' -- Now List all Nested Groups of which the User is a MemberGetNested ObjGroup.NameNextEnd SubFunction GetNested(objGroup) On Error Resume NextoutFile.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) NextEnd Functioni am getting groups but not nested groups..please help.What I have tried:i tried with LDAP its worked,but its unable to read some of groups,so i decided to go with winnt// .so i am able to read groups but not nested groups 解决方案 finally i got solution to read nested group.'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'Nextdim tempSet d = CreateObject("Scripting.Dictionary")Const ADS_NAME_TYPE_NT4 = 3Const ADS_NAME_INITTYPE_GC = 3 Const ADS_NAME_TYPE_1779 = 1 Const ADS_NAME_INITTYPE_SERVER = 2 Const ADS_NAME_INITTYPE_DOMAIN = 1Wscript.Echo "==================================================="Set objNetwork = WScript.CreateObject("WScript.Network")'strUserName =objNetwork.UsernamestrUserName ="laxjoshi"wscript.echo strUserNamestrComputerName = objNetwork.ComputerNamestrUserDomain = objNetwork.UserDomainSet objUser = GetObject("WinNT://" & strUserDomain & "/" & strUserName & ",user")strGroupList = ""For Each objGroup In objUser.Groups 'wscript.echo objGroup.NamestrGroupName=objGroup.NameSet objTrans = CreateObject("NameTranslate")objTrans.Init ADS_NAME_INITTYPE_DOMAIN, strUserDomainstrNTName = 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 "=================================================="nextwscript.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 strGroupListwscript.echo ">>>>>"& strGroupList End If GetNested(objNestedGroup) NextEnd Function 这篇关于创建用于读取用户的vbscript属于包含嵌套组但无法获取嵌套组列表的组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-23 16:30