错误:字符串缺少终止符:

时间:2016-12-28 07:49:59

标签: powershell

#Write-Host "Hello, World!"
Import-Module ActiveDirectory

$str = $args

$str1 = $str.Replace(""", "")
$array1 = $str1.split(",")
$array_length = $array1.Length
$user_id = $array1[0]
$unremoved_Ad = @($array_length - 1)

for($i = 1; $i -lt $array_length; $i++) {
    Write-Host "$($user_id) - $($array1[$i])"

    try {
        #Remove-ADGroupMember -Identity $array[$i] -Member $user_id -Confirm:$false
    } catch {
        $unremoved_Ad = $unremoved_Ad + $array[$i]

        if($i -ne ($array_length - 1)) {
            $unremoved_Ad = $unremoved_Ad
        }
    }
}
Write-Host $unremoved_Ad

嗨,我是Powershell的新手,我正在尝试做一个基本的事情。但是,我收到错误:The string is missing terminator:

更新代码。看起来在Write-Host循环后for中出现了一次重大问题。

请帮忙。提前谢谢。

1 个答案:

答案 0 :(得分:2)

问题是括号和额外点。

代替您的代码, 执行此操作:

#Write-Host "Hello, World!"
Import-Module ActiveDirectory

$str = $args

$str1 = $str.Replace('"', "")
$array1 = $str1.split(",")
$array_length = $array1.Length
$user_id = $array1[0]
$unremoved_Ad = @($array_length - 1)


for($i = 1; $i -lt $array_length; $i++) {
    Write-Host "$($user_id) - $($array1[$i])"

    try {
        #Remove-ADGroupMember -Identity $array[$i] -Member $user_id -Confirm:$false
    } catch {
        $unremoved_Ad = $unremoved_Ad + $array[$i]

        if($i -ne ($array_length - 1)) 
        {
            $unremoved_Ad = $unremoved_Ad
        }
    }
}                
Write-Host $unremoved_Ad