我正在帮助用户解决这个问题,链接到我的答案:Powershell script to add users to A/D group from .csv only using email address?
最初我编写脚本如下,使用基于括号的过滤器,Get-AdUser
如下所示:
Import-CSV "C:\users\Balbahagw\desktop\test1.csv" |
Foreach-Object {
# Here, $_.EmailAddress refused to resolve
$aduser = Get-ADUser -Filter { EmailAddress -eq $_.EmailAddress }
if( $aduser ) {
Write-Output "Adding user $($aduser.SamAccountName) to groupname"
Add-ADGroupMember -Identity groupname -Members $aduser
} else {
Write-Warning "Could not find user in AD with email address $($_.EmailAddress)"
}
}
但是,$_.EmailAddress
未能填充值。但是,将Get-ADUser
过滤器更改为基于字符串的过滤器按预期工作:
$aduser = Get-ADUser -Filter "EmailAddress -eq '$($_.EmailAddress)'"
我正在经历什么奇怪的事情,为什么?是因为当我使用括号时,它被视为一个新范围并且$PSItem
不会跟随?