【问题标题】:Elseif statements only reading the "if" portion in boolean based statementsElseif 语句仅读取基于布尔语句中的“if”部分
【发布时间】:2022-01-08 06:13:07
【问题描述】:

大家好, 我浏览了一些以前的帖子,不幸的是,无法找到适用于我的特定情况的答案。背景是我有一个脚本,它删除了 Visual Studio 及其所有有效的目录路径/注册表条目。我遇到的问题是验证。我正在尝试验证某些路径/目录已被删除。

发生的情况是,该函数只会查看第一个“if”语句,并且似乎会忽略我包含的其他 elseif 和 else 语句。我知道我可能将它们格式化错误,因为我是 powershell 的新手。可能有更好的方法来做到这一点,如果是这样,请随时分享。代码如下:

    function scriptvalidation{
$l1 = Test-Path -path "Path placeholder Number One"
$l2 = Test-Path -path "Path placeholder Number Two"
$l3 = Test-Path -path "Path placeholder Number Three"
$r1 = Test-Path -path "Registry path Number One"
$r2 = Test-Path -path "Registry path Number Two"
$r3 = Test-Path -path "Registry path Number Three"
$DirectoryArray = @($l1, $l2, $l3)
$RegistryArray = @($r1, $r2, $r3)

上面是我使用的变量和数组,下面是语句:

    if ($l1 -eq $true) {
Write-Host ("Path Placeholder number one was found and needs to be removed")
}
elseif ($l2 -eq $true) {
Write-Host ("Path Placeholder Number Two was found and needs to be removed")
}
elseif ($l3 -eq $true) {
Write-Host ("Path Placeholder Number three was found and needs to be removed")
}
else{[String] "$DirectoryArray -eq $false" Write-Host "Directory paths were removed successfully"
}


    if ($r1 -eq $true) {
Write-Host ("Registry Placeholder Number One was found and needs to be removed")
}
elseif ($r2 -eq $true) {
Write-Host ("Registry Placeholder Number Two was found and needs to be removed")
}
elseif ($r3 -eq $true) {
Write-Host ("Registry Placeholder Number Three was found and needs to be removed")
}
else{[String] "$RegistryArray -eq $false Write-Host "Registry paths were removed, script completed successfully"
}
scriptvalidation

我无法正常运行的这部分脚本到此结束。当我安装有问题的程序时,我得到的输出如下: “找到第一个路径占位符,需要删除” “已找到第一个注册表占位符,需要删除” 仅此而已。

我知道我设置错了,但是作为新手,很难知道哪里错了。

任何帮助将不胜感激。

【问题讨论】:

    标签: arrays powershell if-statement path format


    【解决方案1】:

    elseif 的意思是“当且仅当不满足前一个条件并且满足这个条件时”。由于无论文件 1 是否存在,文件 2 都可能存在,因此这不是您想要的行为。

    改为使用 3 个单独 if 语句:

    if ($l1) {
       Write-Host ("Path Placeholder number one was found and needs to be removed")
    }
    
    if ($l2) {
       Write-Host ("Path Placeholder Number Two was found and needs to be removed")
    }
    
    if ($l3) {
       Write-Host ("Path Placeholder Number three was found and needs to be removed")
    }
    

    现在唯一需要的是最后一个 else 块 - 在这里您要测试 两个 $l* 变量是否持有 $true,我们可以这样做:

    if(-not($l1 -or $l2 -or $l3)){
      [string]"$DirectoryArray -eq $false" 
      Write-Host "Directory paths were removed successfully"
    }
    

    $l1 -or $l2 -or $l3 将在三个变量中的 任何 包含 $true 时计算为 true,因此如果它们都是 $false,则此表达式将为 $false 到 - -not然后将其反转,因此只有在没有变量为 $true 时才会出现 $true

    【讨论】:

    • Mathias,感谢您的快速回复,更感谢您的出色回答。我实现了你提到的并运行了整个事情。像魅力一样工作!唯一的一点是,我确实得到了每个语句的输出,如下所示: False False False -eq False 目录路径已成功删除。我会假设这是由于默认输出为 false,因为测试路径应该失败?
    • @Treeman 太好了,不客气! Wrt 额外的输出:PowerShell 函数将输出未捕获或分配的 anything ,因此如果不查看整个函数定义就无法判断 :) 检查您是否有任何类似 $a -eq $b 的语句或只是脚本中某处的$a
    • 知道了,我想我大概可以解决这个问题,再次感谢您的帮助!!
    猜你喜欢
    • 2018-10-01
    • 2015-12-21
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    相关资源
    最近更新 更多