PowerShell新手,我无法弄清楚为什么输出文件中的SamAccountName列为空。 IsDisabled列具有帐户状态结果。
Import-Csv $filename | Foreach-Object{
$user = ([ADSISEARCHER]"(samaccountname=$($_.SamAccountName))").FindOne()
if($user)
{
New-Object -TypeName PSObject -Property @{
SamAccountName = $user.SamAccountName
IsDisabled = $user.GetDirectoryEntry().InvokeGet('AccountDisabled')
}
}
else
{
Write-Warning "Can't find user '$($_.SamAccountName)'"
}
} | Export-Csv $filename
答案 0 :(得分:0)
您必须将第6行更改为:
SamAccountName = $user.Properties.samaccountname
答案 1 :(得分:0)
您需要将sAMAccountName指定为要返回的属性之一。此外,您可以指定userAccountControl属性并测试ADS_UF_ACCOUNTDISABLE位以查看是否禁用了用户帐户。这将避免绑定到每个用户对象的开销,以便在禁用时进行测试。这是一个简短的示例脚本:
param(
[String[]] $sAMAccountName
)
$ADS_UF_ACCOUNTDISABLE = 2
$sAMAccountName | foreach-object {
$searcher = [ADSISearcher] "(sAMAccountName=$($_))"
$searcher.PropertiesToLoad.AddRange(@("samaccountname","useraccountcontrol"))
$result = $searcher.FindOne()
if ( $result ) {
$samName = $result.Properties["samaccountname"][0]
$disabled = ($result.Properties["useraccountcontrol"][0] -band
$ADS_UF_ACCOUNTDISABLE) -ne 0
new-object PSObject -property @{
"Name" = $samName
"Disabled" = $disabled
}
} else {
write-warning "Can't find user $_"
}
}
比尔
答案 2 :(得分:0)
IIRC这是一个已知问题(bug),区分大小写的ldap属性名称。为了避免这种情况,您可以使用哈希表语法而不用担心大小写:
$user.Properties['SAMACCOUNTNAME']