使用GroupPrincipal从Active Directory中的组中获取所有用户

时间:2017-05-25 08:35:08

标签: c# .net active-directory

我试图在AD中获取特定组的所有用户,但我收到异常:“指定的目录服务属性或值不存在。”

@echo off 
SetLocal
:EnterDate
set /p dt="Enter Date in mm-dd-yyyy: " 
if "%dt%" == "" goto :EnterDate

set "saved_date=%date%"
echo %dt%|DATE >NUL 2>&1 && (
  echo %saved_date%|DATE >NUL 2>&1
) || (
  echo %saved_date%|DATE >NUL 2>&1 
  echo %dt% is not a valid date
  goto :EnterDate
)

:CopyFile
echo %dt%
echo xcopy C:\Users\hkum29\Desktop\Source\*.* C:\Users\hkum29\Desktop\Destination\*.* /d:%dt%
EndLocal
Pause

exit/B

1 个答案:

答案 0 :(得分:0)

使用容器指定根

using (var principalContext = new PrincipalContext(ContextType.Domain, "TEST.COM", "DC=TEST,DC=COM"))
                {
                    //using (var group = GroupPrincipal.FindByIdentity(context, windowsGroup.TrimEnd('*')))
                    using (var groupPrincipal = GroupPrincipal.FindByIdentity(principalContext, IdentityType.SamAccountName, "groupName"))
                    {
                        if (groupPrincipal != null)
                        {
                            var users = groupPrincipal.GetMembers();
                            foreach (UserPrincipal userPrincipal in users)
                            {
                                //user variable has the details about the user 
                            }
                        }
                    }
                }