我无法让这个工作......任何想法?基本上,即使用户在芝加哥,索赔是用户不是,并且在else语句中。
我基本上想收集广告中的所有用户。
然后我想查看他们的Office位置,并根据他们所在的位置,设置他们的地址....
$Users = Get-ADGroupMember "Domain Users" -recursive | Select-Object sAMAccountName
foreach ($User in $Users)
{
if
(Get-ADUser -filter {saMAccountName -eq '$User' -and Office -eq "Chicago"})
{
Set-ADUser -StreetAddress "66 Chicago Rd" -City "Chicago" -PostalCode "60618" -State "IL" -Country "US" -Replace @{ co="United States"; countryCode="804" }
}
else
{
(echo $User " not in Chicago!")}
}
答案 0 :(得分:1)
你可以试试这个:
$Users = Get-ADGroupMember "Domain Users" -recursive | Select-Object sAMAccountName
foreach ($User in $Users)
{
if
(Get-ADUser -filter {saMAccountName -eq $User.saMAccountName -and Office -eq "Chicago"})
{
Set-ADUser -StreetAddress "66 Chicago Rd" -City "Chicago" -PostalCode "60618" -State "IL" -Country "US" -Replace @{ co="United States"; countryCode="804" }
}
else
{
(echo $User " not in Chicago!")}
}
我只是替换
saMAccountName -eq '$User'
通过
saMAccountName -eq $User.saMAccountName
答案 1 :(得分:0)
尝试这个,出于某种原因,$ obj.Property未在过滤器scriptblock中展开。我使用Foreach-Object而不是Select-Object来提取sAMAccountName的值。您还缺少要在Set-ADUser命令中设置的用户对象:
$Users = Get-ADGroupMember "Domain Users" -recursive | Foreach-Object {$_.sAMAccountName}
foreach ($User in $Users)
{
if (Get-ADUser -Filter {sAMAccountName -eq $User -and Office -eq 'Chicago'})
{
$user | Set-ADUser -StreetAddress '66 Chicago Rd' -City Chicago -PostalCode 60618 -State IL -Country US -Replace @{ co='United States'; countryCode=804 }
}
else
{
"$User not in Chicago!"
}
}
更新:
您还可以使用相关cmdlet的流媒体特性(一次处理一个对象),并使用以下单行:
Get-ADGroupMember 'Domain Users' -Recursive | Where-Object {$_.ObjectClass -eq 'user'} | Get-ADUser -Properties Office | Where-Object {$_.Office -eq 'Chicago'} | Set-ADUser -StreetAddress '66 Chicago Rd' -City Chicago -PostalCode 60618 -State IL -Country US -Replace @{ co='United States'; countryCode=804 }