为其他电话添加分机 属性

Adding extension to otherTelephone Property

这些天我在编写 PowerShell 时遇到了麻烦。我目前有一个包含以下内容的 CSV headers - 名称、用户名、EXT、UserPrincipleName。我需要将 OtherTelephone 的列数据添加到每个用户的 AD 配置文件中。我有一个基本的脚本,但我想看看我是否遗漏了什么或者是否需要更正。我已将数据添加到 non-array 属性,但不确定它是否相同。

$users = Import-csv -path ".\extensions.csv"
foreach ($ext in $users {
Get-ADUser -Fi * -prop othertelephone -searchbase "MyOU" | select name,@{n="othertelephone";e={$_.othertelephone -join ";"}} | Set-aduser $_.userName -add @{othertelephone = $ext}

谢谢。一如既往,我感谢您的帮助。

由于 LDAP 属性 otherTelephone 是一个多值(字符串数组)属性,我会首先检查是否已经为该用户设置了来自 csv 的新扩展名。 然后,如果不是,则将其添加到现有数组中。

尝试

$searchBase = "OU=MyOU, ...."
$users = Import-csv -Path ".\extensions.csv"
foreach ($user in $users) {
    # first check, is csv field EXT populated?
    if ([string]::IsNullOrWhiteSpace($user.EXT) {
        Write-Warning "Empty extension value for user $($user.Name) in the CSV.."
        continue   # skip this one and proceed with the next
    }
    # try and find the user using its UserPrincipalName
    $adUser = Get-ADUser -Filter "UserPrincipalName -eq '$($user.UserPrincipalName)'" -Properties otherTelephone -Searchbase $searchBase
    if ($adUser) {
        # test if the new extension has already been set (otherTelephone is an array)
        if ($adUser.otherTelephone -contains $user.EXT) {
            Write-Host "Extension $($user.EXT) is already set for user $($user.Name).."
        }
        else {
            Write-Host "Adding extension $($user.EXT) to user $($user.Name)"
            # add the new extension to whatever is already set
            # using -Add or -Replace usually wants strongly typed object arrays
            # therefore the cast to [string[]]
            [string[]]$extensions = @($user.EXT) + $adUser.otherTelephone
            $adUser | Set-ADUser -Replace @{otherTelephone = $extensions}
        }
    }
    else {
        Write-Host "User $($user.Name) not found.."
    } 
}

请注意 属性 称为 UserPrincipalName,而不是问题 UserPrincipleName 中所述。