powershell - 找不到 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
解决方案
很简单: 大括号和 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
最好的问候尼西卡卢
推荐阅读
- python - 将相同的字符放在字符串中的两个不同位置
- reactjs - React map() - 未捕获的类型错误:无法读取未定义的属性“类别”
- python - 基于python中另一个数组上的重复值合并数组?
- angular - 在数据可用之前调用 ngrx 选择器
- kotlin - 在 Kotlin、Spring Boot 和 Thymeleaf 模板中使用抽象数据类型
- python - 2个RPis(主/从)之间的蓝牙套接字上的Python双向通信
- html - 如何垂直居中水平滚动div
- python - **更新**基于用户定义方法的 Python 编程
- javascript - React Hook - 在 componentDidMount 处触发多个“useEffect”
- r - 向现有模型提供新数据并使用 broom::augment 添加预测