如何从Active Directory查询当前用户的信息?
我知道我可以从Windows访问大量关于当前用户的信息,例如环境variables等。 我可以得到当前用户的用户名和域名,但是我需要在AD中查找相应的logging?
我特别想确定的是,我可以唯一地识别 AD中的当前用户,因为这个脚本必须处理大量的用户,并且任何潜在的歧义可能难以以编程方式解决。 做这个的最好方式是什么?
如果你打算在PowerShell中做,假设你已经加载了ActiveDirectory模块( ipmo ActiveDirectory ),你可以做
Get-ADUser ($env:UserName)
login用户名在域上应该是唯一的, Get-ADUser默认使用当前域
经过了一段时间,我从.NET API中偶然发现了这个小小的美丽 :
public static UserPrincipal Current { get; }
自从.NET 3.5以来,这一点已经得到了支持,所以它也适用于较老的Windows操作系统。 在PowerShell中,它可以像这样使用:
# Load in the Assembly for this stuff [System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices.AccountManagement") | Out-Null Function Get-CurrentUser { [System.DirectoryServices.AccountManagement.UserPrincipal]::Current }