在 Powershell 中测试 3 个条件 IF 语句让我抓狂

在 Powershell 中测试 3 个条件 IF 语句让我抓狂

我试图从三个变量的组合(即“0”或“1”)中得出六个结果之一,但无论如何尝试,在测试三个条件时都无法获得正确的输出。我尝试了 3 种方法,但都没有产生正确的响应。我将它们附在下面,但我可能对这三种方法都偏离了轨道。

先谢谢您的帮助。

$a = 1
$b = 0
$c = 1

if ($a = 0) {
    if ($b = 0) {
        if ($c = 0) {
            write-host "000"
        }
        else {
            write-host "001"
        }
    }
    else {
        if ($c = 0) {
            write-host "010"
        }
        else {
            write-host "011"
        }
    }
}
else {
    if ($b = 0) {
        if ($c = 0) {
            write-host "100"
        }
        else {
            write-host "101"
        }
    }
    else {
        if ($c = 0) {
            write-host "110"
        }
        else {
            write-host 111
        }
    }
}

尝试 #2

$a = 1
$b = 0
$c = 1

if (($a = 0) -and ($b = 0) -and ($c = 0)) {
write-host "000"
}

elseif (($a = 0) -and ($b = 0) -and ($c = 1)) {
write-host "001"
}

elseif (($a = 0) -and ($b = 1) -and ($c = 0)) {
write-host "010"
}

elseif (($a = 0) -and ($b = 1) -and ($c = 1)) {
write-host "011"
}

elseif (($a = 1) -and ($b = 0) -and ($c = 0)) {
write-host "100"
}

elseif (($a = 1) -and ($b = 0) -and ($c = 1)) {
write-host "101"
}

elseif (($a = 1) -and ($b = 1) -and ($c = 0)) {
write-host "110"
}

elseif (($a = 1) -and ($b = 1) -and ($c = 1)) {
write-host "111"
}

尝试 #3

$a = 1
$b = 0
$c = 1

switch ($a) {

0 {
    if (($b = 0) -and ($c = 0)) { write-host "000" }
    if (($b = 0) -and ($c = 1)) { write-host "001" }
    if (($b = 1) -and ($c = 0)) { write-host "010" }
    if (($b = 1) -and ($c = 1)) { write-host "011" }
  }

1 {
    if (($b = 0) -and ($c = 0)) { write-host "100" }
    if (($b = 0) -and ($c = 1)) { write-host "101" }
    if (($b = 1) -and ($c = 0)) { write-host "110" }
    if (($b = 1) -and ($c = 1)) { write-host "111" }
  }
}

原始脚本

$erroractionpreference="SilentlyContinue"

$ftppath = "\\Server\Folder1\Folder2\Status Monitor\"
$errorpath = "\\Server\Folder1\Folder2\Status Monitor\Errors\"
$ftpstatusfiles = "opmessage21.txt","pgmessage21.txt","opmessage23.txt","pgmessage23.txt","opmessage24.txt","pgmessage24.txt","opmessage25.txt","pgmessage25.txt","opmessage26.txt","pgmessage26.txt"
$agelimit = 60
$to = "[email protected]"
$from = "[email protected]"
$smtp = "smtp.acme.com"
$port = 25

function send-email {
    Send-MailMessage -Body "$body" -to $to -from $from -Subject "$subject" -smtp $smtp
}

foreach ($ftpstatusfile in $ftpstatusfiles) {

    # Set variables

    $fullftppath = $ftppath + $ftpstatusfile
    $fullerrorpath = $errorpath + $ftpstatusfile
    $lastupdated = (get-childitem $fullftppath).LastWriteTime
    $ftpmsgcontents = Get-Content -Path "$fullftppath" -Raw
    $errormsgcontents = Get-Content -Path "$fullerrorpath" -Raw
    $messageid = $ftpstatusfile.subString(0,$ftpstatusfile.length-4)
    $filestale = if ($lastupdated -lt (get-date).AddMinutes(-$agelimit)) { 1 } else { 0 }
    $fileerror = if ($ftpmsgcontents -ne 'OK') { 1 } else { 0 }

    # Define conditions where no alerting or copying tasks are necessary

    if (($filestale -eq 0) -and ($fileerror -eq 0) -and (!(Test-Path $fullerrorpath))) {
    write-host "Skipping to next iteration, file is fresh, indicates "OK", and there's nothing to clean up ($ftpstatusfile)."
    continue
    }

    if (($filestale -eq 1) -and (Test-Path $fullerrorpath)) {
    write-host "Skipping to next iteration, admin has already been notified of outdated file ($ftpstatusfile)."
    continue
    }

    if (($filestale -eq 0) -and ($fileerror -eq 1) -and (Test-Path $fullerrorpath)) {
    write-host "Skipping to next iteration, admin has already been notified of error ($ftpstatusfile)."
    continue
    }

    # Cleanup after an alert condition that's been resolved

    if (($filestale -eq 0) -and ($fileerror -eq 0) -and (Test-Path $fullerrorpath)) {
        write-host "$messageid reported an error that has since been resolved. Deleting $ftpstatusfile from $errorpath."
        Remove-Item $fullerrorpath
        continue
    }

    # Get LastWriteTime of the file and alert the admin if LastWriteTime is older than X minutes

    if (($filestale -eq 1) -and (!(Test-Path $fullerrorpath))) {
        write-host "As of $lastupdated, $ftpstatusfile is older than $agelimit minutes. Copying $ftpstatusfile to $errorpath and alerting admin.s"
        Copy-Item $fullftppath -destination $errorpath      
        $subject = "$messageid is older than $agelimit minutes"
        $body = "Last updated: $lastupdated"
        send-email
        continue
    }

    # Check the file for an error and alert the admin if the status is not OK

    if (($filestale -eq 0) -and ($fileerror -eq 1) -and (!(Test-Path $fullerrorpath))) {
        write-host "As of $lastupdated, $messageid reported the following error: $ftpmsgcontents. Copying $ftpstatusfile to $errorpath and alerting admins."        
        Copy-Item $fullftppath -destination $errorpath
        $subject = "Error reported by $messageid"
        $body = "$ftpmsgcontents (Last updated: $lastupdated)"
        send-email
    }

}

答案1

在 PowerShell 中,=始终是赋值运算符。因此,您的第一个 if 语句始终将 0 重新分配给变量,并且您的输出始终是000

正确的相等运算符是-eq

if ($a -eq 1) {
    Write-Host "111111111111oneoneone!!!!!"
}

相关内容