vendredi 1 mai 2015

execute if statement in variable value assignement powershell

Sometimes I need to check if a pathname ends with "\" adding it if necessary, the code is pretty simple, something like this

if ($destFolder[-1] -ne '\') {
    $destFolder += '\';
}

Is there a way to evaluate an if statement inside () so that I can use it in variable assignement? I mean something like this

$finalName = $destFolder + (if ($destFolder[-1] -ne '\') { '\' } ) + $fileName

Given that if is not a cmdlet I get this error

if : The term 'if' 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.

Aucun commentaire:

Enregistrer un commentaire