I'm calling the VSTS Pull Request Query documented here: https://learn.microsoft.com/en-us/rest/api/vsts/git/pull%20request%20query/get?view=vsts-rest-4.1
It returns an object array of results. I wanted to detect when no results are returned and react accordingly. I thought I could check the response.results.length, but it will be 1 even if there are no results. It returns an array of 1 empty object. I'm having a hard time detecting that condition. The one solution I thought of is:
if(($pullRequests.results | Get-Member -MemberType Properties).Length -eq 0){some code}
Since a vanilla ps object has 4 members and the populated object has an additional note property it will work. My method seems hackish, is there a better approach?
I tried checking length, exists, and bool:
PS> $pullRequests.results[0].Length
1
PS> $temp = $pullRequests.results[0]
PS> $temp | Get-Member
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
PS> $temp -eq $null
False
PS> if($temp){"YAY"}else{"BOO"}
YAY
$pullRequests-- withInvoke-RestMethod? TryInvoke-WebRequestto see what it's actually returning and whether that can be massaged before passing it toConvertFrom-Json. (That also suggest the reverse should be possible -- checking for($pullRequests.results[0] | ConvertTo-Json -Compress) -eq "{}". Though that's still a bit hackish and inefficient.)[{}]and| ConvertFrom-Jsonotherwise.if ($result | Get-Member -Type Properties).