在Powershell中使用条件if语句遇到麻烦

时间:2020-09-16 14:00:30

标签: powershell

如果使用if elseif语句,则很难使我的代码读取多个条件。不知道为什么它从不读取第二个条件。我已经尝试了十几种不同的方式。任何帮助,将不胜感激。这是我最后一次尝试的代码。我尝试了多个字符串!在前面等等。仍然不会阅读第二条语句。

if (($attachscreenshot = $false) -and ([bool]$textbox2))
{
    newmail0
}

elseif (($attachscreenshot = $true) -and ([bool]$textbox2))
{
    newmail3
}
    
elseif ($attachscreenshot = $false)
{
    newmail1
}

else
{
    newmail
}

3 个答案:

答案 0 :(得分:0)

如Theo的评论所述,请使用-eq等来比较对象。但是,如果您希望使用字符串条件,则[string]::IsNullOrEmpty($screenshot)会更干净。要获得相反的条件,请使用-not($screenshot -eq $true)(例如)。

if (($screenshot -eq $false) -and ([bool]$textbox2)) {
    Write-Host "First condition met!"
}
elseif ($screenshot -eq $true) {
    Write-Host "Second condition"
}
else {
    Write-Host "Third condition!"
}

答案 1 :(得分:0)

尝试一下,这使逻辑更清晰。当然,这是我了解您的逻辑!

if ($attachscreenshot -eq $false {
  if ([bool]$textbox2) { newmail0 }
  else { newmail1 } 
else {
    #$attachscreenshot is $true
    if ([bool]$textbox2) { newmail3 }
    else { newmail }
}

HTH

答案 2 :(得分:0)

if ($attachscreenshot -eq $false)
    {
        if (!$filePath)
        {
            stanmail
        }
        else
        {
            upmail
        }
    }
    if ($attachscreenshot -eq $true)
        {
        if (!$filePath)
        {
            screenmail
        }
            else
            {
                upscreenmail
            }       
    }
相关问题