我的目標是列出屬性 proxyAddresses不包含郵件屬性值的所有 ADuser 。
我的第一步是讓所有具有這兩個值的用戶都填充:
$ADUser = Get-ADUser -Properties Name,Mail,proxyAddresses -Filter {proxyAddresses -like '*' -and mail -like '*'}
然后我嘗試通過帶有集成 if 陳述句的 foreach 回圈運行它
$result = foreach ($User in $ADUser){
$proxystring = $User.proxyAddresses
$Mailstring = $User.Mail
$Mailstring = $Mailstring.ToString()
if ($proxystring -contains '*$Mailstring*'){
Write-Host 'l?uft'
}
else{
Write-Output($User).Name
}
}
在我試過的 if 陳述句中
if ($proxystring -contains '*$Mailstring*')
if ($proxystring -contains $Mailstring)
if ($proxystring -like $Mailstring)
if (($proxystring).contains($Mailstring))
正如在代碼的主要部分中看到的那樣,我也嘗試將它傳遞給一個字串,因為我認為格式可能有問題。
我到處都看到一個變數只與一個字串匹配,而不是與其他變數匹配。
如果有人碰巧知道我的錯誤是什么,我將不勝感激。
uj5u.com熱心網友回復:
您需要從每個地址中洗掉前面的SMTP:/才能正常作業:smtp:proxyAddresses
$result = :outer foreach ($User in $ADUser){
foreach($address in $user.proxyAddresses) {
# remove the leading `smtp:` from each address
$mail = $address -replace '^smtp:'
# and compare, if the user's mail was in the `proxyAddresses` array
if($mail -eq $User.mail) {
# there is no need to keep checking, we can skip this user
# and go next
continue outer
}
}
# if the user's `mail` wasn't found in the `proxyAddresses` array
# output this user
$user
}
您也可以使用-notcontains來大大簡化上面的代碼,但這需要smtp:在用戶mail屬性之前添加:
$result = foreach ($User in $ADUser){
if($user.proxyAddresses -notcontains ('smtp:' $user.mail)) {
$User
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/534706.html
標籤:电源外壳活动目录相比
