如何清理 Powershell 脚本的结果
How to clean up results of a Powershell script
这里是 PowerShell 初学者。到目前为止很享受!
我有一个用户列表,需要与 AD 进行比较。我创建了一个脚本来为每个用户搜索 Get-ADUser Name 和 EmailAddress,使用 Division 数组作为过滤器。
如果找到AD用户,写主机用户找到,显示姓名,电子邮件地址!很简单。
如果未找到 AD 使用,则继续遍历所有分区,直到最后一个分区,如果在任何分区中均未找到,则仅打印一次“用户未在 AD 中找到”
我一直在尝试各种 Do-While、Do-Until 循环和 If-ElseIf 语句,但我想不出让它只报告 ONCE如果在任何部门都找不到。
$Divisions="1","2","3"
$lastd = $Divisions | Select-Object -Last 1
Foreach ($d in $Divisions){
$aduser = Get-ADUser -Filter "Surname -like '*$lastname' -and GivenName -like '$givenname*'`
-and Enabled -eq '$True' -and Division -eq '$d'"`
-SearchBase "OU" -Properties Name, Division, EmailAddress |Select-Object Name,Division,EmailAddress
if(!$aduser){
write-host "Usernotfound"
}
if($aduser -eq ""){
write-host "user not found in AD" `n
}
else{
write-host "User Found in $d :" $aduser.Name $aduser.EmailAddress -F Green `n
}
}
这是我 运行 脚本时的示例结果。
USER1 - account will be terminated in 35 days; Last signon activity 2 days ago
****Searching: Active Directory Information
Usernotfound
Usernotfound
Usernotfound
Usernotfound
Usernotfound
Usernotfound
User Found in Division1! Name EmailAddress
USER2 - account will be terminated in 43 days; Last signon activity 4 days ago
****Searching: Active Directory Information
Usernotfound
Usernotfound
Usernotfound
Usernotfound
Usernotfound
Usernotfound
User Found in Division2! Name EmailAddress
Usernotfound
Usernotfound
Usernotfound
如何才能做得更好?
由于您的 foreach
循环似乎只与 单个 用户相关(同时在多个部门中反复寻找该用户),您可以简单地移动您的不- 在 循环后找到对 的测试:
foreach ($d in $Divisions) {
$aduser =
Get-ADUser -Filter "Surname -like '*$lastname' -and GivenName -like '$givenname*' -and Enabled -eq 'True' -and Division -eq '$d'" -Properties Name, Division, EmailAddress |
Select-Object Name,Division,EmailAddress
if ($adUser) {
Write-Host "User Found in $d :" $aduser.Name $aduser.EmailAddress -F Green `n
break # Presumably you needn't keep searching.
}
}
if (-not $aduser) {
Write-Host "Usernotfound"
}
这里是 PowerShell 初学者。到目前为止很享受!
我有一个用户列表,需要与 AD 进行比较。我创建了一个脚本来为每个用户搜索 Get-ADUser Name 和 EmailAddress,使用 Division 数组作为过滤器。
如果找到AD用户,写主机用户找到,显示姓名,电子邮件地址!很简单。
如果未找到 AD 使用,则继续遍历所有分区,直到最后一个分区,如果在任何分区中均未找到,则仅打印一次“用户未在 AD 中找到”
我一直在尝试各种 Do-While、Do-Until 循环和 If-ElseIf 语句,但我想不出让它只报告 ONCE如果在任何部门都找不到。
$Divisions="1","2","3"
$lastd = $Divisions | Select-Object -Last 1
Foreach ($d in $Divisions){
$aduser = Get-ADUser -Filter "Surname -like '*$lastname' -and GivenName -like '$givenname*'`
-and Enabled -eq '$True' -and Division -eq '$d'"`
-SearchBase "OU" -Properties Name, Division, EmailAddress |Select-Object Name,Division,EmailAddress
if(!$aduser){
write-host "Usernotfound"
}
if($aduser -eq ""){
write-host "user not found in AD" `n
}
else{
write-host "User Found in $d :" $aduser.Name $aduser.EmailAddress -F Green `n
}
}
这是我 运行 脚本时的示例结果。
USER1 - account will be terminated in 35 days; Last signon activity 2 days ago
****Searching: Active Directory Information
Usernotfound
Usernotfound
Usernotfound
Usernotfound
Usernotfound
Usernotfound
User Found in Division1! Name EmailAddress
USER2 - account will be terminated in 43 days; Last signon activity 4 days ago
****Searching: Active Directory Information
Usernotfound
Usernotfound
Usernotfound
Usernotfound
Usernotfound
Usernotfound
User Found in Division2! Name EmailAddress
Usernotfound
Usernotfound
Usernotfound
如何才能做得更好?
由于您的 foreach
循环似乎只与 单个 用户相关(同时在多个部门中反复寻找该用户),您可以简单地移动您的不- 在 循环后找到对 的测试:
foreach ($d in $Divisions) {
$aduser =
Get-ADUser -Filter "Surname -like '*$lastname' -and GivenName -like '$givenname*' -and Enabled -eq 'True' -and Division -eq '$d'" -Properties Name, Division, EmailAddress |
Select-Object Name,Division,EmailAddress
if ($adUser) {
Write-Host "User Found in $d :" $aduser.Name $aduser.EmailAddress -F Green `n
break # Presumably you needn't keep searching.
}
}
if (-not $aduser) {
Write-Host "Usernotfound"
}