Active Directory - 使用拆分和foreach循环添加多个组

时间:2016-05-05 15:38:11

标签: powershell foreach active-directory

我正在使用AD组(标签成员)。我有以下代码:

$VPNGroups = $Line.'Business Partner Employee - AD Group Membership'
$VPNGroup = $VPNGroups -split ';'

foreach ($ADGroup in $VPNGroup) {
        if ($ADGroup -ne "") {Add-ADPrincipalGroupMembership -Identity $SamAccountName -MemberOf $ADGroup -WarningVariable 'VPNGroupError';trap{continue}}
        if (-not $VPNGroupError) {
            $LogFile.WriteLine("AddToVPN" + "," + $SamAccountName + "," + $GivenName + "," + $Sn + "," + $ADGroup)
            Write-Verbose ($SamAccountName + " successfully added to VPN group: " + $ADGroup)
        }
        else {
            $ErrorFile.WriteLine("AddToVPN" + "," + $SamAccountName + "," + $VPNGroupError + "," + $ADGroup)
            Write-Warning $VPNGroupError
            $Error.Clear()
        }
    }

我的csv文件中的单元格与此类似:

Business Partner Employee - AD Group Membership
Group1;Group2

有一次,我让它在添加两个AD组的地方工作。当我在测试帐户上再次运行它时,详细说明两个组都已成功添加,但它实际上只添加了第一个组。

VERBOSE: JDoe1 successfully added to VPN group: Group1
VERBOSE: JDoe1 successfully added to VPN group: Group2

如果我使用AD组格式化单元格:

Group1;Group2;

它添加了两个组,但是详细显示添加了一个额外的空白组(实际上,它没有添加)。

VERBOSE: JDoe1 successfully added to VPN group: Group1
VERBOSE: JDoe1 successfully added to VPN group: Group2
VERBOSE: JDoe1 successfully added to VPN group: 

任何想法为什么它不会添加第二个AD组而没有&#39 ;;'或者如果我添加&#39 ;;'?

,我如何删除空白添加

0 个答案:

没有答案