Getting a specific value in a multivalued property

I know i have done this in the past, but do not recall how.
I am interested in specific value’s in a multivalued property, namely the values CreateDateTime and EnrollmentPolicyName, which are bot located in the additionalproperties value.

Get-MgDevice -Filter "TrustType eq 'AzureAD'" -all | Select DisplayName, TrustType, @{Name="CreateDateTime";Expression={$_.AdditionalProperties.CreateDateTime}}, @{Name="EnrollmentProfileName";Expression={$_.AdditionalProperties.EnrollmentProfileName}}

How do i do this?

Killerbe,
Welcome to the forum. :wave:t3:

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

Since we cannot see your screen or read your mind you have to help us helping you. :wink: What’s wrong with the code you posted? What happens when you run it?

I am sorry about the formatting. I hope i have corrected it.
What i get as a result is this:

DisplayName     TrustType CreateDateTime
-----------     --------- --------------
Computer21 AzureAd
Computer12 AzureAd
Computer1 AzureAd

So the Object CreateDateTime is present in the result, however it does not display the value I am looking for. The AdditionalInformation property has following value:

[createdDateTime, 2022-11-07T15:08:20Z];[deviceOwnership, Company];[enrollmentProfileName, AutoPilot internal];[enrollmentType, OnPremiseUsingWhiteGlove];[isRooted, False];[managementType, MDM];[manufacturer, Dell Inc.];[model, Latitude 5530];[registrationDateTime, 2023-04-07T08:54:40Z];[extensionAttributes, System.Collections.Generic.Dictionary`2[System.String,System.Object]]

And the value’s i am interested with are CreatedDateTime and EnrollmentProfileName

Almost. :smirk: I did the rest for you. :+1:t3:

There are columns missing? Aren’t they there or did you cut them because they’re empty anyway at the moment?

Is it AdditionalInformation or AdditionalProperties?

What’s the output when you output the AdditionalInformation or AdditionalProperties of only one user?
… like this:

Get-MgDevice -Filter "TrustType eq 'AzureAD'" -all | 
Select-Object -First 1 -ExpandProperty AdditionalProperties

Please share the output as it is formatted as code (of course you should obfuscate or remove sensitive information)

The property is called createdDateTime but in your Select-Object statement you’ve dropped a ‘d’ and have createDateTime.

1 Like

This is my line:

Get-MgDevice -Filter "TrustType eq 'AzureAD'" -all | Select DisplayName, TrustType, @{Name="CreatedDateTime";Expression={$_.AdditionalProperties.CreatedDateTime}}, @{Name="EnrollmentProfileName";Expression={$_.AdditionalProperties.EnrollmentProfileName}}

Which Gives following results:

DisplayName     TrustType CreatedDateTime EnrollmentProfileName
-----------     --------- --------------- ---------------------
Computer1 AzureAd
Computer2 AzureAd
Computer3 AzureAd

The multivalued property is “AdditionalProperties”

$Test = Get-MgDevice -Filter "TrustType eq 'AzureAD'" -all
$Test | Get-Member
Name                          MemberType            Definition
----                          ----------            ----------
Add                           Method                void Add(string key, System.Object value), void IAssociativeArray[…
Clear                         Method                void Clear(), void IAssociativeArray[Object].Clear()
ContainsKey                   Method                bool ContainsKey(string key), bool IAssociativeArray[Object].Conta…
CopyFrom                      Method                void CopyFrom(System.Collections.IDictionary source), void CopyFro…
Equals                        Method                bool Equals(System.Object obj)
GetHashCode                   Method                int GetHashCode()
GetType                       Method                type GetType()
Remove                        Method                bool Remove(string key), bool IAssociativeArray[Object].Remove(str…
ToJson                        Method                Microsoft.Graph.PowerShell.Runtime.Json.JsonNode, Microsoft.Graph.…
ToJsonString                  Method                string ToJsonString()
ToString                      Method                string ToString()
TryGetValue                   Method                bool TryGetValue(string key, [ref] System.Object value), bool IAss…
Validate                      Method                System.Threading.Tasks.Task Validate(Microsoft.Graph.PowerShell.Ru…
EntityItem                    ParameterizedProperty System.Object EntityItem(string index) {get;set;}
Item                          ParameterizedProperty System.Object IAssociativeArray[Object].Item(string index) {get;se…
AccountEnabled                Property              System.Nullable[bool] AccountEnabled {get;set;}
AdditionalProperties          Property              System.Collections.Generic.IDictionary[string,System.Object] Addit…
AlternativeSecurityIds        Property              Microsoft.Graph.PowerShell.Models.IMicrosoftGraphAlternativeSecuri…
ApproximateLastSignInDateTime Property              System.Nullable[datetime] ApproximateLastSignInDateTime {get;set;}
ComplianceExpirationDateTime  Property              System.Nullable[datetime] ComplianceExpirationDateTime {get;set;}
DeletedDateTime               Property              System.Nullable[datetime] DeletedDateTime {get;set;}
DeviceId                      Property              string DeviceId {get;set;}
DeviceMetadata                Property              string DeviceMetadata {get;set;}
DeviceVersion                 Property              System.Nullable[int] DeviceVersion {get;set;}
DisplayName                   Property              string DisplayName {get;set;}
Extensions                    Property              Microsoft.Graph.PowerShell.Models.IMicrosoftGraphExtension[], Micr…
Id                            Property              string Id {get;set;}
IsCompliant                   Property              System.Nullable[bool] IsCompliant {get;set;}
IsManaged                     Property              System.Nullable[bool] IsManaged {get;set;}
MdmAppId                      Property              string MdmAppId {get;set;}
MemberOf                      Property              Microsoft.Graph.PowerShell.Models.IMicrosoftGraphDirectoryObject[]…
OnPremisesLastSyncDateTime    Property              System.Nullable[datetime] OnPremisesLastSyncDateTime {get;set;}
OnPremisesSyncEnabled         Property              System.Nullable[bool] OnPremisesSyncEnabled {get;set;}
OperatingSystem               Property              string OperatingSystem {get;set;}
OperatingSystemVersion        Property              string OperatingSystemVersion {get;set;}
PhysicalIds                   Property              string[] PhysicalIds {get;set;}
ProfileType                   Property              string ProfileType {get;set;}
RegisteredOwners              Property              Microsoft.Graph.PowerShell.Models.IMicrosoftGraphDirectoryObject[]…
RegisteredUsers               Property              Microsoft.Graph.PowerShell.Models.IMicrosoftGraphDirectoryObject[]…
SystemLabels                  Property              string[] SystemLabels {get;set;}
TransitiveMemberOf            Property              Microsoft.Graph.PowerShell.Models.IMicrosoftGraphDirectoryObject[]…
TrustType                     Property              string TrustType {get;set;}

Got a bit further.
The property is a dictionary, so when i create an array from the property, i can see the Keys and there values:

PS C:\Windows\System32> $test1[1].values
2020-01-29T09:45:36Z
Company
Company internal
OnPremiseUsingWhiteGlove
False
MDM
Dell Inc.
Latitude 5590
2020-01-29T09:45:36Z
PS C:\Windows\System32> $test1[1].Keys
createdDateTime
deviceOwnership
enrollmentProfileName
enrollmentType
isRooted
managementType
manufacturer
model
registrationDateTime
extensionAttributes
PS C:\Windows\System32>

Looks like the dictionary is case sensitive. Try this

Get-MgDevice -Filter "TrustType eq 'AzureAD'" -all | Select DisplayName, TrustType, @{Name="CreatedDateTime";Expression={$_.AdditionalProperties.createdDateTime}}, @{Name="EnrollmentProfileName";Expression={$_.AdditionalProperties.enrollmentProfileName}}
1 Like

That did the trick, THX!

Solved one thing, to bump my head into another issue. They Asked me if it would be possible to get the assigned User Principal Name, in the list as well. This is not possible with the Get-MGDevice cmdlet. After some research i found that that info is available in the Get-MGDeviceManagementManagedDevice cmdLet. I have created an array, called $AllDevs from the initial query:

$AllDevs = Get-MgDevice -Filter "TrustType eq 'AzureAD'" -all | Select DisplayName, TrustType, @{Name="CreatedDateTime";Expression={$_.AdditionalProperties.createdDateTime}}, @{Name="EnrollmentProfileName";Expression={$_.AdditionalProperties.enrollmentProfileName}}, ApproximateLastSignInDateTime

Now when i try to filter with Get-MGDeviceManagementManagedDevice i Run into a Syntax error:

foreach ($obj in $alldevs) {Get-MgDeviceManagementManagedDevice -Filter "DeviceName eq $($Obj.DisplayName)"}
Get-MgDeviceManagementManagedDevice_List: Invalid filter clause: Syntax error at position 19 in 'DeviceName eq Computer1'.
Get-MgDeviceManagementManagedDevice_List: Invalid filter clause: Syntax error at position 19 in 'DeviceName eq Computer2'.
Get-MgDeviceManagementManagedDevice_List: Invalid filter clause: Syntax error at position 24 in 'DeviceName eq Computer3'.

DeviceName does have the correct value…

You could use a loop creating a PSCustomObject to combine the results of two different but related queries with each other.
The current thread next to yours has an example of the approach …

Well actually solved it yesterday. The problem is the double/single quotes:

$DSName = $Obj.DisplayName
foreach ($obj in $alldevs) {Get-MgDeviceManagementManagedDevice -Filter "DeviceName eq '$DSName'"}

So by placing the object within single quotes it works. This looks weird, as single quotes used to be literal (string), and you needed to use double quotes for PowerShell to use the string as a dynamic object.

What matters is the outer most quotes. The entire string is expandable since it’s double quotes.

1 Like

Thanks for clarifying.