It seems that if the command inside the condition area fails to execute and doesn't return a value, the entire if-else block is not executed and the continue on error chooses to continue below the if-else block.
How to Simulate the Problem
While this is not exactly what is going on in my real code, it does produce the same problem of the if-else block not being executed. It even does it every time.
if ( Test-Path -invalid-switch )
{
write-output "true path of if statement"
} else {
write-output "false path of if statement"
}
write-output "finished if statement"
has the output
Test-Path : A parameter cannot be found that matches parameter name 'invalid-switch'.
At C:\scripts\test.ps1:1 char:16
+ if ( Test-Path -invalid-switch )
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Test-Path], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.TestPathCommand
finished if statement
The Fix
To fix the issue the code that may have an error needs to be taken out of the if() condition tested beforehand, and logic added to the if-statement to deal with a potential failure. To fix up the actual code to not suffer from this problem I have now changed the code to
$err = $false
try { $tmpStatus = Test-Path ( $target_dir + "receivals.tmp") }
catch
{
$tmpStatus = $error | foreach { $_.Exception }
$err = $true
}
if ( $err -or $tmpStatus )
{
del ( $target_dir + "receivals.tmp")
$DelTmpStatus = $?
} else {
$DelTmpStatus = "NA"
}
The astute will realise that I could just replace all of that with just the lines
$tmpStatus = test-path ( $target_dir + "receivals.tmp")
del ( $target_dir + "receivals.tmp")
$DelTmpStatus = $?
since I try and delete the file even if it has an error finding the file. The only difference will be that the shorter code will have errors more often but still perform the same function.
However having the longer code with more error checking sheds a bit more light on the situation as $tmpStatus has come through with the value
System.Management.Automation.CommandNotFoundException: The term 'Test-Path' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
Unfortunately this still leaves me wondering what is going on...? How can 'Test-Path' be a valid cmdlet some days but not others, with exactly the same script running on the same computer from the same scheduled job. As an extra bonus, on the days when 'Test-Path' is not a valid cmdlet, when I use it again later on in the same script it works fine every time.