在New-ADUser中转义变量

时间:2016-11-28 21:59:01

标签: powershell active-directory

我试图在指定targetAddressproxyAddresses的参数部分中使用变量。

这是现在的命令,其中电子邮件地址由用户" newUser"硬编码。如何用变量$accountName替换它?

New-ADUser -Name $fullName -AccountPassword $PWord -OtherAttributes @{
  'extensionAttribute1' = 'Acme Corp';
  'msExchExtensionCustomAttribute1' = 'Acme Corp';
  'proxyAddresses' = 'SMTP:newUser@acmecorp.com',
                     'smtp:newUser@acmecorp.mail.onmicrosoft.com';
  'targetAddress' = 'SMTP:newUser@acmecorp.mail.onmicrosoft.com'
}

2 个答案:

答案 0 :(得分:0)

好吧,-OtherAttributes只是一个哈希,所以你只需要用一个变量来代替哈希值。我强烈建议打破你的哈希,并且" splat"它,例如:

[String[]]  $proxyAddresses  = @( 'SMTP:newUser@acmecorp.com','smtp:newUser@acmecorp.mail.onmicrosoft.com' );

[String]    $targetAddress = 'SMTP:newUser@acmecorp.mail.onmicrosoft.com';

[Hashtable] $otherAttributes = @{ extensionAttribute1 = 'Acme Corp';
                                  msExchExtensionCustomAttribute1 = 'Acme Corp'; 
                                  proxyAddresses = $proxyAddresses;
                                  targetAddress  = $targetAddress;
                                };

try {
    New-ADUser -Name $fullName -AccountPassword $PWord -OtherAttributes @otherAttributes;
    }
catch [<exception type>] {
    }

答案 1 :(得分:0)

这就是我想出来的......它有效......但不是很漂亮

$proxyEmail1 = 'SMTP:' + $accountName + '@acmecorp.com'
$proxyEmail2 = 'smtp:' + $accountName + '@acmecorp.mail.onmicrosoft.com'


[String]    $targetAddress = 'SMTP:' + $accountName + '@acmecorp.mail.onmicrosoft.com';

[Hashtable] $otherAttributes = @{ extensionAttribute1 = 'Acme';
                              msExchExtensionCustomAttribute1 = 'Acme'; 
                              targetAddress  = $targetAddress;
                            };

New-ADUser  -Name $fullName  -OtherAttributes $otherAttributes  [lots of stuff redacted]

Get-ADUser $accountName | Set-ADUser -Add @{proxyAddresses=$proxyEmail1}
Get-ADUser $accountName | Set-ADUser -Add @{proxyAddresses=$proxyEmail2}