vendredi 19 juillet 2019

Using an If statement to set a variable, but not change it if a condition isn't met?

I'm trying to set an output variable from an IF statement but only when it's condition is met, but leave the variable untouched if the condition isn't met. For some reason, the IF statement is changing the variable even when the condition isn't met. How do I tell an IF statement to do nothing if it's condition fails?

I originally started with the IF statement setting $Output = "Error" in it's script block, but that just sets it to fail even when it shouldn't.

$Output = "Pass"
$ErrorCodes=2,26,50,48,2820,4239
$ErroredBackups = Import-Csv C:\temp\NetBackupJobs_GS.csv
                    foreach ($ErroredBackup in $ErroredBackups) {$Output = If ($ErroredBackup.status -in $ErrorCodes) { "Error" }}
$Output 

Sample of the CSV:

jobid,started,status 334382,17/07/2019 16:35,0

I keep getting $Output as blank, when it should either be "Error" or "Pass"

Aucun commentaire:

Enregistrer un commentaire