[英]Add proxyAddresses to Active Directory users when created in PowerShell
我正在嘗試在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中,但似乎不可能。
知道如何做到這一點?
使用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之前我將它傳遞給“Sort-Object -Unique” - 我不知道的是更改了使cmdlet不滿意的類型或內容。
僅供參考... Sort-Object可以在這些情況下燒毀你。
所以,在我的測試中。 我在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的答案 ,並試圖剛剛-OtherAttributes
上新ADUser便有
$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:$_" }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.