如何通过ADSI为用户获取这些属性,这些属性来自Get-ADUser,我需要与ADSI等效的属性。
我的目标是查询所有用户的整个域并获取这些属性。
我尝试使用Get-ADUser cmdlet,它在查询用户时超时。
Get-ADUser -Filter * -Properties enabled,PasswordNeverExpires,passwordexpired,Name,SamAccountName,mail,passwordlastset -server sc |
where {$_.Enabled -eq "True"} |
where { $_.PasswordNeverExpires -eq $false } |
where { $_.passwordexpired -eq $false } |
Select Name,SamAccountName,mail,
@{l='PasswordExpires';e={$_.passwordlastset+(Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge}},
@{l='DaystoExpire';e={(New-TimeSpan -Start (get-date) -end ($_.passwordlastset+(Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge)).days}}上面的命令适用于几个用户,但是如果我查询一大组用户,它就会提供无效的枚举上下文。
发布于 2015-06-08 19:13:07
属性SamAccountName、Name和Mail对应于同名AD属性。PasswordLastSet是从属性pwdLastSet派生的。其他3个属性(Enabled、PasswordNeverExpires和PasswordExpired)是userAccountControl属性中的标志。
使用带有LDAP查询的adsisearcher对象搜索AD以查找用户对象,然后使用所需的属性构建自定义对象:
$ACCOUNTDISABLE = 0x000002
$DONT_EXPIRE_PASSWORD = 0x010000
$PASSWORD_EXPIRED = 0x800000
$searcher = [adsisearcher]"(&(objectClass=user)(objectCategory=person))"
$searcher.FindAll() | % {
$user = [adsi]$_.Properties.adspath[0]
New-Object -Type PSCustomObject -Property @{
SamAccountName = $user.sAMAccountName[0]
Name = $user.name[0]
Mail = $user.mail[0]
PasswordLastSet = [DateTime]::FromFileTime($_.Properties.pwdlastset[0])
Enabled = -not [bool]($user.userAccountControl[0] -band
$ACCOUNTDISABLE)
PasswordNeverExpires = [bool]($user.userAccountControl[0] -band
$DONT_EXPIRE_PASSWORD)
PasswordExpired = [bool]($user.userAccountControl[0] -band
$PASSWORD_EXPIRED)
}
}尽管如此,为什么您想要解决所有这些麻烦,而不是简单地使用Get-ADUser来达到相同的目的呢?
Import-Module ActiveDirectory
$attributes = 'SamAccountName', 'Name', 'Mail', 'PasswordLastSet', 'Enabled',
'PasswordNeverExpires', 'PasswordExpired'
Get-ADUser -Filter * -Properties $attributes | select $attributes发布于 2015-06-08 13:50:04
您可以在Get-Item Powershell驱动器上使用AD:\,此cmdlet接受-properties参数来检索指定的属性列表。使用星号将使cmdlet检索所有属性。举个例子:
get-aduser -filter "sAMAccountName -like '*'" | % { get-item "AD:\$($_.distinguishedName)" -properties enabled,PasswordNeverExpires,passwordexpired,Name,SamAccountName,mail,passwordlastset }编辑:对于计算出的属性,包括“已启用”、“密码永不过期”等,Get-ADUser也可以接受-properties参数,因此代码如下:
get-aduser -filter "sAMAccountName -like '*'" -properties enabled,PasswordNeverExpires,passwordexpired,Name,SamAccountName,mail,passwordlastset星号也很好。
https://stackoverflow.com/questions/30710755
复制相似问题