【发布时间】:2018-10-12 07:03:23
【问题描述】:
我使用这个 PowerShell 从 Active Directory 获取过去 24 小时内创建的用户的信息:
$ous = 'OU=test,DC=test,DC=local' $When = ((Get-Date).AddDays(-1)).Date $ous | ForEach { Get-ADUser -Filter {whenCreated -ge $When} -Properties whenCreated,* -SearchBase $_ }";
如何使用 C# 获得相同的结果?感谢您的帮助。
这是我的 C# 代码:
static void Main(string[] args)
{
// LDAP string to define your OU
string ou = "OU=test,DC=test,DC=local";
// set up a "PrincipalContext" for that OU
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "test.local", ou))
{
// define the "query-by-example" user (or group, or computer) for your search
UserPrincipal qbeUser = new UserPrincipal(ctx);
// set whatever attributes you want to limit your search for, e.g. Name, etc.
qbeUser.Surname = "ilgezdi";
// define a searcher for that context and that query-by-example
using (PrincipalSearcher searcher = new PrincipalSearcher(qbeUser))
{
foreach (Principal p in searcher.FindAll())
{
// Convert the "generic" Principal to a UserPrincipal
UserPrincipal user = p as UserPrincipal;
if (user != null)
{
console.Write(user);
}
}
}
}
}
【问题讨论】:
-
你可以把
if (user != null)改成if(p is UserPrincipal)
标签: c# active-directory principal userprincipal