【问题标题】:Add proxyAddresses to Active Directory users when created in PowerShell在 PowerShell 中创建时将 proxyAddresses 添加到 Active Directory 用户
【发布时间】:2015-06-30 09:42:00
【问题描述】:

我正在尝试在 Active Directory 中添加用户。这些用户需要有代理地址。我的问题是那些 proxyAddresses 是倍数并存储在一个数组中。

我试试:

$proxyAddresses = @("address1@test.com", "address2@test.com", "address3@test.com")
$userInstance = new-object Microsoft.ActiveDirectory.Management.ADUser
$userInstance.ProxyAddresses = $proxyAddresses
New-ADUser test -Instance $userInstance

我得到了这个错误:

 Invalid type 'System.Management.Automation.PSObject'. Parameter name: proxyAddresses

我想将此 proxyAddresses 数组添加到我的 AD 用户的属性 proxyAddresses 但似乎不可能。

知道如何做到这一点吗?

【问题讨论】:

  • $proxyAddresses 转换为 [String[]]?
  • 我试图“加入” $proxyAddresses 数组,问题是存储在 AD 中的是一个字符串“address1@test.com;address2@test.com;address3@test.com”而不是 3 个不同的字符串。
  • 在分配时尝试直接类型转换:$userInstance.ProxyAddresses = [Microsoft.ActiveDirectory.Management.ADPropertyValueCollection]$proxyAddresses

标签: powershell active-directory


【解决方案1】:

使用Set-ADUser有什么问题吗?

$username = '...'
$proxyAddresses = 'address1@example.com', 'address2@example.com', 'address3@example.com'

New-ADUser -Name $username
Set-ADUser -Identity $username -Add @{
  'proxyAddresses' = $proxyAddresses | % { "smtp:$_" }
}

【讨论】:

  • 我在发布问题时认为它不起作用,但它确实起作用了!非常感谢。
  • 我为我做了一个 Get-ProxyAddresses 函数。它获取用户名和域的各种组合并列出它们。 gist.github.com/PsychoData/dd475c27f7db5ce982cd6160c74ee1d0
  • 例如 Get-ProxyAddress -username 'john.smith', 'james.smith' -domains 'domain.com','domain.net' SMTP:john.smith@domain.com smtp: john.smith@domain.net smtp:james.smith@domain.com smtp:james.smith@domain.net
【解决方案2】:

我刚刚遇到了同样的问题,我很确定我传入了一个字符串数组(这就是它的声明方式)。

问题就在我将字符串数组发送到 AD 之前,我将它传递给“Sort-Object -Unique” - 我不知道它正在更改类型或使 cmdlet 不开心的东西。

仅供参考...Sort-Object 在这些情况下可能会烧死你。

【讨论】:

    【解决方案3】:

    所以,在我对此的测试中。我在 https://gist.github.com/PsychoData/dd475c27f7db5ce982cd6160c74ee1d0 上创建了 Get-ProxyAddresses

    function Get-ProxyAddresses
    {
        Param(
        [Parameter(Mandatory=$true)] 
        [string[]]$username,
        [string[]]$domains = 'domain.com'
        )
    
        #Strip off any leading @ signs people may have provided. We'll add these later 
        $domains = $domains.Replace('@','')
    
        $ProxyAddresses = New-Object System.Collections.ArrayList
        foreach ($uname  in $username) {
            foreach ($domain in $domains ) {
                if ($ProxyAddresses.Count -lt 1) {
                    $ProxyAddresses.Add( "SMTP:$uname@$domain" ) | Out-Null
                } else {
                    $ProxyAddresses.Add( "smtp:$uname@$domain" ) | Out-Null
                }
            }
        }
        return $ProxyAddresses 
    }
    

    它只是作为一个集合返回。很笨拙,但可以满足我的需要。它还假设第一个用户名和第一个域是“主要”

    我将它与 @ansgar 的 answer 结合起来,并在 New-Aduser 上尝试了 -OtherAttributes

    $proxyAddresses = Get-ProxyAddress -username 'john.smith', 'james.smith' -domains 'domain.com','domain.net'
    
    New-ADUser      -Name $username 
                    -OtherAttributes @{       
                        'proxyAddresses'= $proxyAddresses
                    }
    

    完美运行,并在创建时为我添加了 proxyAddresses,之后无需进行单独的设置操作。

    如果您要执行单独的操作,我建议您使用-Server,如下所示,这样您就不会意外与两个不同的 DC 交谈(而且您还知道 New-ADUser 是完成并且已经在那里,您不必等待复制)

    #I like making it all in one command, above, but this should work fine too. 
    $ADServer = (Get-ADDomainController).name
    New-ADUser -Server $ADServer -name $Username
    Set-ADUSer -Server $ADServer -Identity $username -Add @{
      'proxyAddresses' = $proxyAddresses | % { "smtp:$_" }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 2019-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-08
      • 2023-04-09
      相关资源
      最近更新 更多