首页 > 解决方案 > 找不到 ELSEIF 命令 PowerShell

问题描述

对为什么这部分代码不起作用感到困惑。

144       #Evaluate Column C
145       $COLUMNC = $_.C
146    
147       #Find Prepared With:
148       $COLUMNCOUT = $COLUMNC -like "*Prepared*"
149       if($COLUMNCOUT -eq $True){
150            $sheet.Cells.Item($rowcount,"F") = $COLUMNC
151       }
152    
153       #Find Contains:
154       $COLUMNCOUT = $COLUMNC -like "*Contains:*"
155       elseif($COLUMNCOUT -eq $True){
156            $sheet.Cells.Item($rowcount,"G") = $COLUMNC
157       }
158       
159       else{
160            $sheet.Cells.Item($rowcount,"R") = $COLUMNC
161       }

我相信我所拥有的内容是正确的。我可以没有第 154 行吗???

任何帮助都会得到帮助。

错误:

elseif : The term 'elseif' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\TEST\Working_Test.ps1:155 char:4
+    elseif($COLUMNCOUT -eq $True){
+    ~~~~~~
    + CategoryInfo          : ObjectNotFound: (elseif:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
else : The term 'else' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\TEST\Working_Test.ps1:159 char:4
+    else{
+    ~~~~
    + CategoryInfo          : ObjectNotFound: (else:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

标签: powershell

解决方案


很简单: 大括号和 elseif 之间没有代码

如果在这些语句之间放置一行代码,powershell 会抛出错误,因为在 elseif 子句之前没有 if 子句。

您的代码应如下所示:

#Evaluate Column C
$COLUMNC = $_.C

#Find prepared with
if ($COLUMNC -like "*Prepared*") {
    $sheet.Cells.Item($rowcount,"F") = $COLUMNC
}

#Find Contains:
elseif ($COLUMNC -like "*Contains:*") {
    $sheet.Cells.Item($rowcount,"G") = $COLUMNC
}
else{
    $sheet.Cells.Item($rowcount,"R") = $COLUMNC
}

有关 if 和 else-if 表达式的更多信息: https ://www.tutorialspoint.com/powershell/if_else_statement_in_powershell.htm


最好的问候尼西卡卢


推荐阅读