【问题标题】:Powershell elseif is not recognizedPowershell elseif 无法识别
【发布时间】:2014-07-08 17:27:44
【问题描述】:

为什么我收到此代码的错误?术语“elseif”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确并重试。

    $EAFRID=($item.name.Split("_")[2])
        if ($EAFRID -match "EAFRID")
        { 
             Write-Host "EAFR is $EAFR"
        }

    $AppID= ($item.name.Split("_")[1])                  
            elseif($AppID -match "CPL")
            {               
            Write-Host "CPL is $AppID"
            }

           else{    
            $OtherID = ($item.name.Split("_")[0])
                            Write-Host "other is $OtherID"
                }

【问题讨论】:

  • 也许你应该改用else if

标签: powershell


【解决方案1】:

因为 elseif 没有紧跟在 if 块之后 - 行 $AppID=... 在路上。像这样的东西可能会起作用:

$EAFRID =($item.name.Split("_")[2])
$AppID = ($item.name.Split("_")[1])
if ($EAFRID -match "EAFRID")
{ 
     Write-Host "EAFR is $EAFR"
}
elseif ($AppID -match "CPL")
{               
    Write-Host "CPL is $AppID"
} 

【讨论】:

  • 这让我通过了 elseif 现在我得到了与 else 相同的信息
  • @MrKWatkings 我刚刚做了另一个 elseif 并且成功了。
  • 此外,如果你有如下构造:if (some_predicate_here) { #actions here } elseif (another_predicate) { #fallback scenario } else #notice the absence of curly braces {} surrounding #other sentence here 你会得到同样的错误。所以要注意用正确的花括号括住你的句子。
猜你喜欢
  • 2014-01-08
  • 2012-09-13
  • 2020-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多