PS脚本,用于从交换添加自定义属性到AD对象

时间:2019-12-10 13:06:25

标签: powershell exchange-server

我正在研究一个自定义脚本,以将一些Exchange的自定义属性从在线Exchange添加到我们的本地AD。

我遇到一个问题,使用set-aduser命令将值传递到AD:

#Set UPN of current user from CSV
$UPN = $User.aduser

#Get user's MSOL object
$User = get-msoluser -UserPrincipalName "$UPN@domain.com"

#Get a list of additional email addresses for the user
$userproxies = $user.ProxyAddresses

#Find same user's local AD Object
$LocalADUser = Get-ADUser -Filter {userPrincipalName -eq $user.UserPrincipalName} -Properties * -server Server

#set custom attributes
$CustomAttr1 = get-mailbox  $upn | Select-Object CustomAttribute1
$CustomAttr2 = get-mailbox  $upn | Select-Object CustomAttribute2
$CustomAttr3 = get-mailbox  $upn | Select-Object CustomAttribute3

#Copies properties from MSOL to Local AD Object (minus the ProxyAddresses)
Set-ADUser -server Server -DisplayName $User.DisplayName -Identity $LocalADUser.SamAccountName -Office $User.Office -Company $User.CompanyName -Department $User.Department -Title $User.title -MobilePhone $User.Mobile -OfficePhone $User.TelephoneNumber 

#For each ProxyAddress in the users MSOL Object
foreach ($Proxy in $UserProxies)
{
    #Add the ProxyAddress to the users Local AD Object
    set-ADUser -server Server -Identity $LocalADUser.SamAccountName -Add @{Proxyaddresses=$Proxy}
}

Set-ADUser -server Server -Identity $LocalADUser.SamAccountName -add @{CustomAttribute1=$CustomAttr1}
Set-ADUser -server Server -Identity $LocalADUser.SamAccountName -add @{CustomAttribute2=$CustomAttr2}
Set-ADUser -server Server -Identity $LocalADUser.SamAccountName -add @{CustomAttribute3=$CustomAttr3}

我在最后3行中失败了。 ({-add @(CustomAttribute1=$CustomAttr1}等)

我收到以下错误

  

Set-ADUser:无效的类型“ System.Management.Automation.PSObject”。       参数名称:CustomAttribute1       在C:\ Users \ user \ Documents \ AD Azure Sync.ps1:51 char:13       + Set-ADUser-服务器Server -Identity $ LocalADUser.SamA ...       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~       + CategoryInfo:InvalidArgument :(用户名:ADUser)[Set-ADUser],ArgumentException       + FullyQualifiedErrorId:         ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.SetADUser

关于如何解决甚至改善此问题的任何建议?

如果可能的话,我想将自定义属性部分转换为数组,并将其传递给for-each循环。

0 个答案:

没有答案