Function Powershell函数问题
我得到的错误如下 语句块中缺少结束“}” 我用它来自动将用户添加到一个.dat文件中,该文件包含了我们其中一个系统的数百个用户。我负责接管这部分,我不喜欢手工操作。所以我写了一个脚本,为我完成了95%的任务,它删除了用户的代码。但我在这里尝试的这个函数将用户添加到两个不同的特定位置,以保持它的整洁。我还在学习powershell,所以任何方向都很好,你不会伤害我的感情,我在这里既不专业也不是新手Function Powershell函数问题,function,powershell,Function,Powershell,我得到的错误如下 语句块中缺少结束“}” 我用它来自动将用户添加到一个.dat文件中,该文件包含了我们其中一个系统的数百个用户。我负责接管这部分,我不喜欢手工操作。所以我写了一个脚本,为我完成了95%的任务,它删除了用户的代码。但我在这里尝试的这个函数将用户添加到两个不同的特定位置,以保持它的整洁。我还在学习powershell,所以任何方向都很好,你不会伤害我的感情,我在这里既不专业也不是新手 Function Add{ $username = Read-Host "User to add t
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
}
错误说明了一切。关闭函数的结尾缺少一个
}
提示是将脚本放入一个编辑器(如记事本++),该编辑器可以处理
{}
匹配,很容易看出是否遗漏了{
/}
错误说明了一切。关闭foreach块的末尾缺少“}”..函数Add{
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
}
$username=读取主机“要添加到REP01-02打印的用户”
$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=获取内容$LOCreportADD1
$Text2=获取内容$LOCreportADD2
$NewText1=@()
foreach($Text1中的行){
如果($Line-eq“插入新用户1”){
$Line=$Line.Replace(“插入新用户1\”,“插入新用户1\”)
$NewText1+=$Line
$NewText1+=$username1+$userid+$username+$henation
}
Elseif($Line-eq“新用户2”){
$Line=$Line.Replace(“新用户2\”,“新用户2\”)
$NewText12+=$Line
$NewText12+=$username+$prd+$userid+$username+$henation
}#在Powershell ISE中,您可以突出显示“}”。然后它将突出显示它形成一个代码块的括号。突出显示它,然后右键单击它。这样可以更容易地找到您的问题。对于每个人来说,它允许函数正常工作,但现在它的工作方式与我希望的不同。