使用 Powershell 在线 Exchange 中批量编辑自定义属性(脚本未按预期工作)

Bulk editing custom attribute in Exchange online with Powershell (Script not working as intended)

希望获得有关我的 powershell 脚本的帮助。基本上有一个脚本,我用它来为多个用户批量编辑 Azure AD 中的字段并且它工作正常。我尝试使用它通过 Exchange Online 为多个用户编辑自定义属性,但它不起作用。我猜它对 EO 的作用不同。这里的目标是提取一个包含 2 列的 csv(用户电子邮件地址“userprincipalname”,一列用于我想为“customattribute1”添加的值)任何帮助表示赞赏。

# Connect to ExchangeOnline
Connect-ExchangeOnline

# Get CSV content
$CSVrecords = Import-Csv C:\pathtofile.csv

# Create arrays for skipped and failed users
$SkippedUsers = @()
$FailedUsers = @()

# Loop trough CSV records
foreach ($CSVrecord in $CSVrecords) {
    $upn = $CSVrecord.UserPrincipalName
    $user = Get-Mailbox -Filter "userPrincipalName eq '$upn'"  
    if ($user) {
        try{
        $user | Set-Mailbox -customattribute1 $CSVrecord.customattribute1 
        } catch {
        $FailedUsers += $upn
        Write-Warning "$upn user found, but FAILED to update."
        }
    }
    else {
        Write-Warning "$upn not found, skipped"
        $SkippedUsers += $upn
    }
}

我认为可能有 2 点导致无法设置 customattribute1

  1. 过滤器表达式应为:"userPrincipalName -eq '$upn'"
  2. 当您导入 .CSV 文件时,我似乎找不到 -Delimiter 参数,这将导致无法正确提取列值。

试试下面最适合我的代码:

Connect-ExchangeOnline

$SkippedUsers = @()
$FailedUsers = @()

$CSVrecords = Import-Csv "C:\Users\Administrator\Desktop\test.csv" -Delimiter ","
foreach($CSVrecord in $CSVrecords ){
    $upn = $CSVrecord.UserPrincipalName
    $user = Get-Mailbox -Filter "userPrincipalName -eq '$upn'"  
    if ($user) {
        try{
        $user | Set-Mailbox -customattribute1 $CSVrecord.customattribute1 
        } catch {
        $FailedUsers += $upn
        Write-Warning "$upn user found, but FAILED to update."
        }
    }
    else {
        Write-Warning "$upn not found, skipped"
        $SkippedUsers += $upn
    }
}

我的测试 .csv 文件:

在 运行 PS 命令之后,尝试获取我的测试用户的 customattribute1

如果您有更多问题,请告诉我。