用 csv 文件头值替换 AD 属性值

Replace AD attribute value with csv file header values

我想根据 CSV 文件头值替换 AD 属性“userPrincipalName”的值 这是 csv 文件(group.csv)包含的内容

sAMAccountName
--------------
test.user1    
test.user2    

脚本下方

$data = Import-Csv -Path .\group.csv -Header 'sAMAccountName'
foreach($user in $data){
    Get-ADUser -Filter {sAMAccountName -eq "$($user.sAMAccountName)"} | Set-ADUser -Replace @{userPrincipalName="$($user.sAMAccountName)@RES.GROUP"}
}

这里我想用 csv 文件中的 sAMAccountName 值替换 AD 属性“userPrincipalName”,例如 sAMAccountName@RES.GROUP

这个脚本不起作用,谁能纠正一下?

好的,因为您的评论显示 CSV 文件确实没有 header,我建议将代码更改为:

$data = Import-Csv -Path .\group.csv -Header 'sAMAccountName'
foreach($user in $data) {
    $adUser = Get-ADUser -Filter "SamAccountName -eq '$($user.sAMAccountName)'" -ErrorAction SilentlyContinue
    if ($adUser) {
        $newUPN = '{0}@res.group' -f $user.sAMAccountName
        $adUser | Set-ADUser -UserPrincipalName $newUPN
    }
    else {
        Write-Warning "No user with SamAccountName '$($user.sAMAccountName)' could be found.."
    }
}

这样,当找不到具有该 samaccountname 的用户时,文件中的任何错误都不会导致代码退出。相反,在这种情况下,您会看到有关它的警告,代码将继续处理其余数据。


可能值得一提的是,您可以在 Get-ADUserSet-ADUser cmdlet 上使用参数 -Server 以确保您使用相同的域服务器 (DC) 来设置新的 UPN。否则,您可以在一个 DC 上设置它,但正在查看另一个不会立即显示更改的 DC,因为服务器需要时间来同步..



现在我们已经解决了关于 CSV 的问题并回答您的评论:

如果您想将其作为 two-script 解决方案来执行,请按以下步骤操作

步骤 1:获取搜索 OU 中 UserPrincipalName 以“*@test.group”结尾的所有用户

$searchBase = "OU=Teams,OU=Prod,DC=RES,DC=TEST,DC=GROUP"
Get-ADUser -SearchBase $searchBase -Filter "UserPrincipalName -like '*@test.group'" | 
# select ony the SamAccountName and write to CSV with column header
Select-Object SamAccountName | Export-Csv -Path .\group.csv -NoTypeInformation

步骤 2:读取上面创建的 csv 和

$searchBase = "OU=Teams,OU=Prod,DC=RES,DC=TEST,DC=GROUP"
$data = Import-Csv -Path .\group.csv
$result = foreach($user in $data) {
    $adUser = Get-ADUser -SearchBase $searchBase -Filter "SamAccountName -eq '$($user.sAMAccountName)'" -ErrorAction SilentlyContinue
    # if we have a user object AND its UserPrincipalName is not as desired go ahead and change that
    if ($adUser) {
        if ($adUser.UserPrincipalName -notlike '*@res.test.group') {
            $newUPN = '{0}@res.test.group' -f $user.sAMAccountName
            $adUser | Set-ADUser -UserPrincipalName $newUPN
            # output this user object to be collected in variable $result
            $adUser
        }
        else {
            Write-Host "User $($user.sAMAccountName) already has UPN '$($adUser.UserPrincipalName)'"
        }
    }
    else {
        Write-Warning "User with SamAccountName '$($user.sAMAccountName)' not found.."
    }
}

# now that we have changed some users, create a second csv with all users that were actually changed
if (@($result).Count) {
    $result | Select-Object SamAccountName | Export-Csv -Path .\Updatedgroup.csv -NoTypeInformation
}
else {
    Write-Host 'No users needed updating'
}

只将用户 SamAccountName 属性 写入 csv 文件似乎很浪费。特别是因为 Get-ADUser 默认情况下已经 returns 这些属性:DistinguishedName,已启用、GivenName、名称、ObjectClass、ObjectGUID、SamAccountName、SID、姓氏、UserPrincipalName