Search code examples
jsonpowershellazure-devopsazure-cli

Retrieving information in Powershell from Az Devops result


I am retrieving group informations from Azure Devops via the azure CLI (az devops security group membership list --id "xxx"). Unfortunately, the result is somehow not usable in Powershell.

This is the result json, I need the mailAddress.

{
  "DESCRIPTOR": {
    "descriptor": "DESCRIPTOR",
    "directoryAlias": "DIRECTORYALIAS",
    "displayName": "DISPLAYNAME",
    "domain": "DOMAIN",
    "legacyDescriptor": LEGACYDESCRIPTOR,
    "mailAddress": "MAILADDRESS",
    "metaType": "METATYPE",
    "origin": "ORIGIN",
    "originId": "ORIGINID",
    "principalName": "PRINCIPALNAME",
    "subjectKind": "SUBJECTKIND",
    "url": "URL"
  },
.....
}

Has someone worked with the results and knows how to get the mailAddress?

Thanks!


Solution

  • Short answer: You should be able to get to the mailAddress property with $result.'DESCRIPTOR'.mailAddress.

    Here is the why and how:

    PS C:\> $result = az devops security group membership list --id "xxx" --organization "yyy" | ConvertFrom-Json
    PS C:\> $result.GetType()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     False    PSCustomObject                           System.Object
    
    PS C:\Users\BHANNADE> $result | 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()
    aad.NmEzOTc1MTIt...xYr58NWZkLTg4MDQtY2QxZGUxODkzMWQ4 NoteProperty System.Management.Automation.PSCustomObject ...
    aad.Y2I1NjM2NjIt...mZi03NzA5LTg4MWQtNDZjZmI5NjRjYWMy NoteProperty System.Management.Automation.PSCustomObject ...
    

    This tells us that $result is a PowerShell Custom Object with the descriptors themselves as Members (NoteProperties).

    Therefore, $result.'DESCRIPTOR'.mailAddress should let you get to the mailAddress property:

    PS C:\> $result.'aad.NmEzOTc1MTIt...xYr58NWZkLTg4MDQtY2QxZGUxODkzMWQ4'.mailAddress
    abc.xyz@email.com
    

    UPDATE:

    There may be multiple members in a Team and so your $result may contain multiple objects. You can extract all the email addresses as follows:

    $properties = $result | Get-Member -MemberType Properties | Select-Object -ExpandProperty Name
    $mailAddresses = @()
    $mailAddresses += $properties.ForEach({$result.$_.mailAddress})