Home > Net >  entering if when True
entering if when True

Time:07-27

writing a Powershell script to monitor a log file and have a condition for a fail over to a redundant connection that needs a service to be restarted.

$cond = "WARN: Attempting auto reconnect to Host"
$lfil = "c:\temp_logs\error.log"
Get-Content $lfil -wait | Select-String -Quiet $cond
if(true) {
    write-host "restarting due to fail over "
    Stop-service "Outbound HL7"
    Rename-Item -Path $lfil -NewName "c:temp_logs\error_restart.log"
    Start-sleep 2
    Start-Service "Outbound HL7"
} else {
    write-host "no match"
} 

in the ISE i get a line when the condition is met True, but cant enter in the if condition, tried the $_. I know this has to be really simple but very new to Powershell and looking things up is pointing me in a bunch of different directions. Any guidance would be greatly apprciated.

Joe

CodePudding user response:

In PowerShell the literal term true has no meaning. The PowerShell version of the True/true constants many other languages use is instead $true.

if ($true) { Write-Host 'True' }

That being said you also can use other values that can be cast to the true boolean value to satisfy an if condition such as

if (1) { Write-Host 'True' }

and

if ('non-empty string') { Write-Host 'True' }

just to name a few

CodePudding user response:

You're looking to operate on whether or not your Select-String found a match.

Adding -Quiet outputs a Boolean that indicates that; it does not set an exit code in the manner that calls to external programs do.

Thus, instead of trying to test for the outcome after the Select-String call, directly evaluate the call itself:

if (Get-Content $lfil -wait | Select-String -Quiet $cond) { 
  ...
}

As for what you tried:

if (true) { ...

true is interpreted as a command in this case; there is no such command built into PowerShell, so it'll look for an external program named true, which is unlikely to exist on Windows, but does exist on Unix-like platforms, where it produces no output and only sets an exit code; however, if only acts on output, and empty output is considered falsey, so the conditional is always false.

If you wanted to operate on the most recent command's success status, PowerShell's automatic $? variable reflects that: however, unlike in POSIX-like shells such as bash, where it contains the most recent statement's exit code, in PowerShell it is an abstract indicator reflecting either $true or $false (PowerShell's Boolean constants).

$? is rarely used in PowerShell, and it doesn't quite operate the same way that it does in POSIX-compatible shells, because native PowerShell commands do not use exit codes to communicate success vs. failure. Thus, $? usually just tells you whether a genuine error occurred, not whether a condition was met or not. E.g., after executing 'a' | Select-String 'b', $? still contains $true, since no error occurred during execution (the string was simply not found). As with Select-String -Quiet, PowerShell-native commands in general indicate success vs. failure via a Boolean output value, as the Test-* cmdlets such as Test-Path do.

However, for commands that do set (process) exit codes - external programs and potentially also .ps1 script (primarily if they're called from outside PowerShell) - it is reflected in the automatic $LastExitCode variable.

  • Related