2

I want to perform a check of a variable's existence while creating a PSCustomObject. I have quite a few objects to query and gather data for into my new object so I don't want to duplicate the entire code block with an "if" statement as I'm trying to be concise.

[array]$newObject += [PSCustomObject][ordered]@{
  JitterInterArrival = (if ($_.QoeReport.AudioStreams){$_.QoeReport.AudioStreams[0].JitterInterArrival}else{"N/A"}
}

I know the above block produces an error that the "if" statement isn't recognized. Is there another way to include a code block while defining the PSCustomObject?

Jason Shave
  • 2,462
  • 2
  • 29
  • 47

1 Answers1

4

You were very close!

[array]$newObject += [PSCustomObject][ordered]@{
  JitterInterArrival = $(if ($_.QoeReport.AudioStreams){$_.QoeReport.AudioStreams[0].JitterInterArrival}else{"N/A"})
}

By surrounding it with $() we make it a subexpression which is executed first.

Mark Wragg
  • 22,105
  • 7
  • 39
  • 68