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?

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.