用于获取 AD 用户的脚本停止工作

Script for fetching AD users stopped working

我使用以下代码超过 16 个月,突然 $ldapresult 为空。

Import-module ActiveDirectory
if((Get-PSSnapin | Where {$_.Name -eq “Microsoft.SharePoint.PowerShell”}) -eq $null) {

}

$SearchBase = "OU=Active-Users,DC=Rubber,DC=Maid"
$LdapServer = "YYYDC12XYZ.Rubber.Maid"
$ADAccount = "sjones"
Get-ADDomainController -server $ldapserver          

$LDAPResult = Get-ADUser -SearchBase $searchbase -SearchScope 'subtree' -Server $ldapserver -filter "Account -like '*$ADAccount*'" -Properties * | Select -Property AccountExpirationDate, userAccountControl
$pair1, $pair2 = $LDAPResult -replace '^@{|}$' -split '; '
$null, $AccountExpirationDate = $pair1 -split '=', 2
$null, $userAccountControl = $pair2 -split '=', 2    

write-host $ADAccount $AccountExpirationDate $userAccountControl -foregroundcolor "green"   
write-host $LDAPResult          

所以我 运行 执行了以下命令,果然它为 sjones 带来了所有数据。知道为什么上面的代码不再起作用了吗?

Get-ADUser $ADAccount -Properties * | Select *

您过滤器中的属性不正确-filter "Account -like '*$ADAccount*'" 将过滤器中的 "Account" 替换为 "SamAccountName",如下所示:

-Filter "SamAccountName -like '*$ADAccount*'"