I'm using Graph API to lookup user objects and the "registeredDevices" associated with that user. registeredDevices is a relational property so I'm using $exand.
My query
/users/<userId>`$expand=registeredDevices
is returning the properties I want (mail. jobTitle, id (user), and registeredDevices).
My problem is that registeredDevices is providing way more details then what I need...
registeredDevices : {@{@odata.type=#microsoft.graph.device;
id=AAA-1234; deletedDateTime=; accountEnabled=True; approximateLastSignInDateTime=2023-09-19T13:53:01Z; complianceExpirationDateTime=; createdDateTime=2022-07-25T13:08:38Z;
deviceCategory=; deviceId=1234-ABCD; deviceMetadata=; deviceOwnership=; deviceVersion=2; displayName=<host name>; domainName=; enrollmentProfileName=; enrollmentType=; externalSourceName=; isCompliant=; isManaged=;
isRooted=; managementType=; manufacturer=; mdmAppId=; model=; onPremisesLastSyncDateTime=; onPremisesSyncEnabled=; operatingSystem=Windows; operatingSystemVersion=10.0.19044.1766; physicalIds=System.Object[]; profileType=RegisteredDevice;
registrationDateTime=2022-07-25T13:09:35Z; sourceType=; systemLabels=System.Object[]; trustType=Workplace; extensionAttributes=; alternativeSecurityIds=System.Object[]},
@{@odata.type=#microsoft.graph.device;
id=BBBB-1234; deletedDateTime=; accountEnabled=True; approximateLastSignInDateTime=2023-09-30T22:08:53Z; complianceExpirationDateTime=; createdDateTime=2023-09-21T14:19:08Z; deviceCategory=;
deviceId=1234-BBBB; deviceMetadata=; deviceOwnership=Company; deviceVersion=2; displayName=<host name>; domainName=; enrollmentProfileName=LM US (Default); enrollmentType=AppleBulkEnrollmentModernAuth;
externalSourceName=; isCompliant=True; isManaged=True; isRooted=False; managementType=MDM; manufacturer=Apple; mdmAppId=<value would be here>; <model=model name>; onPremisesLastSyncDateTime=; onPremisesSyncEnabled=;
operatingSystem=Idevice; operatingSystemVersion=16.7; physicalIds=System.Object[]; profileType=RegisteredDevice; registrationDateTime=2023-09-21T14:19:08Z; sourceType=; systemLabels=System.Object[]; trustType=Workplace; extensionAttributes=;
alternativeSecurityIds=System.Object[]},
@{@odata.type=#microsoft.graph.device; id=CCCC-1234; deletedDateTime=; accountEnabled=True; approximateLastSignInDateTime=2023-09-30T20:54:47Z; complianceExpirationDateTime=;
createdDateTime=2023-09-21T14:21:39Z; deviceCategory=; deviceId=1234-CCC; deviceMetadata=; deviceOwnership=Company; deviceVersion=2; displayName=<host name>; domainName=; enrollmentProfileName=<profile name>;
enrollmentType=AppleBulkEnrollmentModernAuth; externalSourceName=; isCompliant=True; isManaged=True; isRooted=False; managementType=MDM; manufacturer=Apple; mdmAppId=<value would be here>; model=i<model name>;
onPremisesLastSyncDateTime=; onPremisesSyncEnabled=; operatingSystem=Idevice; operatingSystemVersion=17.0.2; physicalIds=System.Object[]; profileType=RegisteredDevice; registrationDateTime=2023-09-21T14:21:39Z; sourceType=;
systemLabels=System.Object[]; trustType=Workplace; extensionAttributes=; alternativeSecurityIds=System.Object[]},
@{@odata.type=#microsoft.graph.device; id=DDDD-1234; deletedDateTime=; accountEnabled=True;
approximateLastSignInDateTime=2023-10-01T21:00:36Z; complianceExpirationDateTime=; createdDateTime=2023-09-22T18:19:33Z; deviceCategory=; deviceId=1234-DDDD; deviceMetadata=; deviceOwnership=Company; deviceVersion=2;
displayName=<model name>; domainName=; enrollmentProfileName=<profile name>; enrollmentType=AppleBulkEnrollmentModernAuth; externalSourceName=; isCompliant=True; isManaged=True; isRooted=False; managementType=MDM; manufacturer=Apple;
mdmAppId=<value would be here>; model=<model name>; onPremisesLastSyncDateTime=; onPremisesSyncEnabled=; operatingSystem=Idevice; operatingSystemVersion=17.0.2; physicalIds=System.Object[]; profileType=RegisteredDevice;
registrationDateTime=2023-09-22T18:20:02Z; sourceType=; systemLabels=System.Object[]; trustType=Workplace; extensionAttributes=; alternativeSecurityIds=System.Object[]}}
All I want from this property is any value starting with "id=". What would be the most efficient way to eliminate all the other stuff coming back?
I researched this the best i could and only found solutions that involve filtering out specific properties which i can already do. How do you filter out values of a property you don't want
I'm assuming you're using Invoke-RestMethod
, which automatically parses the API's JSON response into an object [graph] - a potentially nested [pscustomobject]
instance or an array thereof (in a manner of speaking, Invoke-RestMethod
has ConvertFrom-Json
built in).
What you're seeing is the for-display representation of this object graph which shows that the top-level [pscustomobject]
instance has a .registeredDevices
property containing an array of nested [pscustomobject]
instances, each of which has an .id
property.
Therefore, taking advantage of member-access enumeration, you can extract all nested .id
property values as follows:
(
Invoke-RestMethod ".../users/<userId>`$expand=registeredDevices"
).registeredDevices.id
Note:
You can reduce the amount of data transferred by only asking for the specific property/ies of interest, such as by appending $select=id
(`$select=id
if inside "..."
, e.g. ".../users/<userId>`$expand=registeredDevices&`$select=id"
) to your query string - see Project from an entity to properties.
However, the results are still wrapped in objects (that also contain system properties such as @odata.type
), so you still need the extraction technique shown above.