我正在尝试为 AD 清理工作创建一个 CSV,其中将包含几百个用户SamAccountName和一个要从中删除用户的组列表。每个用户将有一个不同的组列表来删除他们。
CSV 将如下所示:
SamAccountName、ADgroupName1、ADgroupName2、ADgroupName3、ADgroupName4 等... 用户 1、组 1、组 2、组 3、组 4 用户 2,组 2,组 3,,, 用户 3,Group5,,,,
我到目前为止的脚本:
# Get the list of SAMAccountNames
$user = Import-Csv .\GroupsToRemove.csv | Select-Object -ExpandProperty SAMAccountName
foreach ($user in $users) {
    # Loop through the user list and select the list of groups to remove for each user
    # from the CSV and set to the $Groups array
    $Group = @()
    $Group = %{(Import-Csv .\GroupsToRemove.csv | Where-Object {$_.SamAccountName -eq $user})} | select "GroupName*"
    foreach ($group in $Groups) {
        # Remove the AD groups from each User
        Remove-ADPrincipalGroupMembership $user -Member $Group -Confirm:$false
    }
}
我认为部分问题是当我从 CSV 导入组名时,它还会将列名添加到$Group数组中吗?所以Remove-ADPrincipalGroupMembership命令失败了?
$groups输出如下:
组名 1:组 1 组名 2:组 2 组名 3:组 3 组名 4:组 4