在PowerShell中创建时,向Active Directory用户添加代理地址



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

我尝试:

$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中,但这似乎不可能。

你知道怎么做吗?

使用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:$_" }
}

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

问题是,就在我将字符串数组发送到AD之前,我将其传递给"排序对象-唯一"-我不知道它正在更改类型或使cmdlet不满意的东西。

仅供参考。。。在这种情况下,排序对象可能会烧着你。

所以,在我的测试中。我在https://gist.github.com/PsychoData/dd475c27f7db5ce982cd6160c74ee1d0

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的答案结合起来,并在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对话(而且你也知道新的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:$_" }
}

最新更新