我有一个txt文件,其中包含格式为
的名称列表firstname lastname
如何通过仅从此列表中获取内容来检查AD中是否存在用户。我在AD中最接近的属性是displayName,其格式为lastname,firstname。以下不起作用。感谢
get-content "c:\temp\file.txt" |
foreach {
if (Get-ADUser -Filter {displayName -like $_}){
Write-host "$_ does exist in AD"}
else {
Write-host "$_ does not exist in AD"
}
}
答案 0 :(得分:1)
怎么样
get-content "temp.txt" |
foreach {
$givenname,$surname = $_ -split ' '
if (Get-ADUser -Filter "surname -eq '$surname' -and givenname -eq '$givenname'"){
Write-host "$_ does exist in AD"}
else {
Write-host "$_ does not exist in AD"
}
}
答案 1 :(得分:0)
这会更好吗?
get-content "c:\temp\file.txt" |
foreach {
$filter = [scriptblock]::create("DisplayName -like '*$_*'")
if (Get-ADUser -Filter $filter){
Write-host "$_ does exist in AD"}
else {
Write-host "$_ does not exist in AD"
}
}
答案 2 :(得分:0)
我会这样做:
$users = Get-Content "C:\temp\file.txt"
Get-ADUsers -Filter * -Properties givenName,sn | % {
if ( $users -contains ($_.givenName + " " + $_.sn) ) {
$_.Name + " exists in AD."
} else {
$_.Name + " does not exist in AD."
}
}