在PowerShell中创建时,将proxyAddresses添加到Active Directory用户

时间:2015-06-30 09:42:01

标签: powershell active-directory

我正在尝试在Active Directory中添加用户。这些用户需要具有proxyAddresses。我的问题是那些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中,但似乎不可能。

知道如何做到这一点?

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

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

问题就在我将字符串数组发送到AD之前我将其传递给“Sort-Object -Unique” - 我不知道的是更改了使cmdlet不满意的类型或内容。

仅供参考......在这种情况下,Sort-Object会烧掉你。

答案 2 :(得分:0)

所以,在我的测试中。我在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 
}

它只是作为一个集合返回。非常kludgy,但适合我需要的东西。它还假定第一个用户名和第一个域是“主要”

我将其与@ 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交谈(并且您也知道新的 - 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:$_" }
}