I have a working Powershell statement like below,
$Uptime = $Uptime | ForEach-Object {
if($_ -eq "N/A"){return $_}else{'{0}:{1}:{2:D2}:{3:D2}' -f ($_.Duration()).Days,($_.Duration()).Hours,($_.Duration()).Minutes,($_.Duration()).Seconds}
}
Where $Uptime is an array of DateTime objects.
I like the one liner for my else statement, but I'd like to create a variable which contain $_.Duration() in my else statement instead of repeating it.
I tried,
$Uptime = $Uptime | ForEach-Object {
if($_ -eq "N/A"){return $_}else{ $d = $_.Duration() return '{0}:{1}:{2:D2}:{3:D2}' -f ($d).Days,($d).Hours,($d).Minutes,($d).Seconds}
}
Which obviously is not valid syntax (unexpected token 'return' in expression or statement).
Obviously my goal is to have the same behavior as above and return the formatted string '{0}:{1}:{2:D2}:{3:D2}', but I can't find the right syntax.