根据办公室位置修改用户

发布于 2024-11-25 16:54:15 字数 564 浏览 1 评论 0原文

我无法让它发挥作用...有什么想法吗?基本上,即使用户位于芝加哥,该声明也表明该用户不在芝加哥,并且属于 else 语句。

我基本上想收集AD中的所有用户。

然后我想查看他们的办公室位置,并根据他们所在的位置,设置他们的地址......

 $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!")}
}

I can't get this to work... any ideas? Basically, even if a user is in Chicago, the claim is the user isn't and falls in the else statement.

I basically want to collect all users in AD.

Then I want to look at their Office location and based on where they are located, set their address....

 $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!")}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

内心激荡 2024-12-02 16:54:15

你能试试这个吗:

$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

Can you try this :

$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!")}
}

I just replace

saMAccountName -eq '$User' 

by

saMAccountName -eq $User.saMAccountName
猫性小仙女 2024-12-02 16:54:15

试试这个,由于某种原因 $obj.Property 没有在过滤器脚本块中扩展。我使用 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 }

Try this, for some reason $obj.Property is not expanded in the filter scriptblock. I used Foreach-Object instead of Select-Object to extract the values of sAMAccountName. You were also missing the user object to set in the Set-ADUser command:

$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!"
    }
}

UPDATE:

You can also use the streaming nature of the cmdlets in question (process one object at a time) with the following one-liner:

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 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文