【问题标题】:Update Active Directory "mail" attribute via PowerShell通过 PowerShell 更新 Active Directory“邮件”属性
【发布时间】:2022-02-07 04:23:40
【问题描述】:

我正在尝试为特定 OU 中的所有用户更新 AD 中列出的电子邮件地址。这是我正在使用的 powershell 脚本,但它无法正常工作

Import-Module ActiveDirectory
Get-ADUser -Filter * -SearchBase "OU=OtherOU,OU=SomeOu,DC=Domain,DC=local" | Set-ADUser -email $_.samaccountname@domain.com

我认为这是因为当我尝试执行 Set-ADUser 时,$_.samaccountname 没有返回任何内容。 谁能指出我解决这个问题的正确方向?谢谢!

【问题讨论】:

    标签: powershell active-directory


    【解决方案1】:

    在当前上下文中 $_ 为空。您需要使用 Foreach-Object 才能使 $_ 可用。

    Get-ADUser -Filter * ... | Foreach-Object{
       Set-ADUser -Identity $_ -Email "$($_.samaccountname)@domain.com"
    }
    

    【讨论】:

      【解决方案2】:

      使用 SamAccountName 和电子邮件地址创建一个 csv 文件

      "SamAccountName","EmailAddress"
      "john","john@xyz.com"
      

      第 1 步:导入变量

      $users = Import-Csv .\email.csv
      

      第二步:调用变量

      foreach ($user in $users) {
          Set-ADUser -Identity $user.SamAccountName -EmailAddress $user.EmailAddress
      }
      

      【讨论】:

        【解决方案3】:

        我怀疑您需要为此使用子表达式:

        "$($_.samaccountname)@domain.com"
        

        【讨论】:

          【解决方案4】:

          假设用户名是 domain\user1 或 user1@domain.com

          $user = "user1"
          Set-ADUser $user -emailaddress "firtname.lastname@xyz.com"
          Get-ADUser -Identity $user -Properties emailaddress
          

          【讨论】:

            【解决方案5】:
            Get-ADUser -Filter * -SearchScope Subtree -SearchBase "OU=OUName,DC=domain,DC=com" |  
              Foreach-Object { Set-ADUser -Identity $_ -Email "$($_.samaccountname)@domain.com" }
            

            这是来自: https://social.technet.microsoft.com/wiki/contents/articles/33311.powershell-update-mail-and-mailnickname-for-all-users-in-ou.aspx

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2023-03-26
              • 2014-02-24
              • 1970-01-01
              • 1970-01-01
              • 2017-11-16
              • 1970-01-01
              • 2013-11-24
              • 1970-01-01
              相关资源
              最近更新 更多