在Powershell IF语句中testing3个条件正在推动着我

我试图从“0”或“1”这三个variables的组合中挑出六个结果中的一个,并且在testing三个条件时我无法得到正确的结果,不pipe我如何尝试。 我已经尝试了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 } } 

在PowerShell中, =总是一个赋值操作符。 所以,你的第一个if语句总是给variables赋0,而你的输出总是000

正确的等号运算符是-eq

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