我想Get-ADUser
然后将其与用户插入的内容进行比较。
如果有人能帮助我解决这个问题,我将非常感激。
$user1 = Read-Host "Enter the first username"
$GetUser = Get-ADUser -Filter {SamAccountName- eq $user1}
if ($user1 -ne $GetUser){
Write-Host "It does not match our records. Please try again later" -ForeGroundColor Red
}
elseif ($User1 -eq $GetUser){
Write-Host "It matches our records" -ForegroundColor Green
}
答案 0 :(得分:2)
您可以测试Get-ADUser
的回复,如果没有匹配则返回任何内容。
我还会使用Identity
(而不是Filter
)因为它接受SamAccountName和DN。
- 尊贵的名字
- GUID(objectGUID)
- 安全标识符(objectSid)
- SAM帐户名称(sAMAccountName)
这将您的代码简化为:
$user1 = Read-Host "Enter the first username"
if (Get-ADUser -Identity $user1){
Write-Host "It matches our records" -ForegroundColor Green
}
else {
Write-Host "It does not match our records. Please try again later" -ForeGroundColor Red
}