-1

我得到的错误如下

语句块中缺少结束“}”。

我正在使用它来自动将用户添加到 .dat 文件中,该文件有数百个用户用于我们的系统之一。我的任务是完成这部分工作,而且我不喜欢手动做任何事情。所以我写了一个脚本来为我完成 95% 的工作,它删除了用户的就好了。但是我在这里尝试的这个功能将用户添加到 2 个不同的特定位置,以保持它的美观和整洁。我仍在学习 powershell,所以任何方向都会很棒,你不会伤​​害我的感受,我在这里既不是专业人士也不是新手。

Function Add{
$username = Read-Host "User to add to REP01-02 Printing"
$LOCreportADD1 = "\\rep01\E$\orant\Report60\Server\cgicmd.dat"
$LOCreportADD2 = "\\rep02\e$\orant\Report60\Server\cgicmd.dat"
$Username1 = $username+": "
$prd = "prd: "
$userid = "userid="
$henation = "/henation@rmsprd %*"


$Text1 = get-content $LOCreportADD1
$Text2 = get-content $LOCreportADD2

$NewText1 = @()
foreach ($Line in $Text1) {
    if ($Line -eq "Insert new user1") {
        $Line = $Line.Replace("Insert new user1 \", "Insert new user1 \")
        $NewText1 += $Line
        $NewText1 += $username1+$userid+$username+$henation
    }
    Elseif ($Line -eq "New User2") {
        $Line = $Line.Replace("New User2 \", "New User2 \")
        $NewText12 += $Line
        $NewText12 += $username+$prd+$userid+$username+$henation
}
$NewText1 | Set-Content $LOCreportADD1
}
4

4 回答 4

3

错误说明了一切。您}最后缺少 a 来关闭该功能。

提示是将您的脚本放入可以处理{}匹配的编辑器(如 Notepad++)中,并且很容易看到您是否错过了{/ }

于 2013-09-27T12:41:32.280 回答
2

错误说明了一切。您最后缺少一个“}”来关闭 foreach 块..

于 2013-09-27T12:45:01.253 回答
0
Function Add{
    $username = Read-Host "User to add to REP01-02 Printing"
    $LOCreportADD1 = "\\rep01\E$\orant\Report60\Server\cgicmd.dat"
    $LOCreportADD2 = "\\rep02\e$\orant\Report60\Server\cgicmd.dat"
    $Username1 = $username+": "
    $prd = "prd: "
    $userid = "userid="
    $henation = "/henation@rmsprd %*"


    $Text1 = get-content $LOCreportADD1
    $Text2 = get-content $LOCreportADD2

    $NewText1 = @()
    foreach ($Line in $Text1) {
        if ($Line -eq "Insert new user1") {
            $Line = $Line.Replace("Insert new user1 \", "Insert new user1 \")
            $NewText1 += $Line
            $NewText1 += $username1+$userid+$username+$henation
        }
        Elseif ($Line -eq "New User2") {
            $Line = $Line.Replace("New User2 \", "New User2 \")
            $NewText12 += $Line
            $NewText12 += $username+$prd+$userid+$username+$henation

        } # <---- This is your missing brace, to close out the Else condition

    }
    $NewText1 | Set-Content $LOCreportADD1
}
于 2013-09-27T13:06:50.390 回答
0

在 Powershell ISE 中,您可以突出显示“}”。然后它将突出显示与它形成代码块的括号。突出显示它,然后右键单击它。让您更容易找到您的问题。

于 2013-09-28T18:37:24.630 回答