简体   繁体   English

可以使用 Microsoft Graph PowerShell cmdlet 从 AdditionalProperties 字典中提取信息吗?

[英]Possible to pull info from AdditionalProperties dictionary with Microsoft Graph PowerShell cmdlets?

I am trying to use PowerShell Graph cmdlets instead of the Azure AD module cmdlets.我正在尝试使用 PowerShell Graph cmdlet 而不是 Azure AD 模块 cmdlet。 With the Azure AD module, I can do this:使用 Azure AD 模块,我可以这样做:

# This is what I want:
get-azureadgroupmember -objectid $GroupID | select-object -property displayname, `
    mail, userprincipalname, objectid

DisplayName     Mail                        UserPrincipalName  ObjectId
-----------     ----                        -----------------  --------
John Smith      John.Smith@example.org      jsmith@example.org 4bae8291-6ec3-192b-32ce-dd21869ef784
(...)


# All of these properties are directly accessible in the returned objects:
$res = get-azureadgroupmember -objectid $GroupID
$res[0] | fl -prop *
# Shows long list of directly accessible properties

I'm trying to figure out the equivalent with PowerShell Graph:我试图找出与 PowerShell Graph 的等价物:

$res = get-mggroupmember -groupid $GroupID
$res[0] | fl -prop *
# Only properties are DeletedDateTime, Id, and AdditionalProperties

# Want to do something like this, but it doesn't work:
get-mggroupmember -groupid $GroupID | select-object -property id, `
    additionalproperties['displayName'], additionalproperties['mail'], `
    additionalproperties['userPrincipalName']

# This works, but is there a better option???
get-mggroupmember -groupid $GroupID | foreach-object { `
        "{0},{1},{2},{3}" -f $_.id, $_.additionalproperties['displayName'], `
        $_.additionalproperties['mail'], $_.additionalproperties['userPrincipalName']
    }

AdditionalProperties is a dictionary (IDictionary) which contains displayname, mail, and userprincipalname. AdditionalProperties 是一个字典 (IDictionary),其中包含显示名称、邮件和用户主体名称。 My thought is there is probably a better way to do this or to get at the information.我的想法是可能有更好的方法来做到这一点或获取信息。

There are a few interesting parameters in get-mggroupmember that I'm not clear on including "-expandproperty" and "-property". get-mggroupmember 中有一些我不清楚的有趣参数,包括“-expandproperty”和“-property”。 I've tried playing around with these but haven't had any luck.我试过玩这些,但没有任何运气。 I'm wondering if there's a way to use these to do what I want.我想知道是否有办法使用这些来做我想做的事。

Suggestions?建议?

Given the following $object , 3 properties and one of them AdditionalProperties is a Dictionary<TKey,TValue> :给定以下$object ,3个属性,其中一个AdditionalPropertiesDictionary<TKey,TValue>

$dict = [Collections.Generic.Dictionary[object, object]]::new()
$dict.Add('displayName', 'placeholder')
$dict.Add('mail', 'placeholder')
$dict.Add('userPrincipalName', 'placeholder')

$object = [pscustomobject]@{
    DeletedDateTime = 'placeholder'
    Id = 'placeholder'
    AdditionalProperties = $dict
}

Supposing from this object you're interested in Id , displayName and mail , you could use Select-Object with calculated properties :假设您对此对象感兴趣IddisplayNamemail ,您可以使用Select-Object计算属性

$object | Select-Object @(
    'Id'
    @{
        N = 'displayName'
        E = { $_.additionalProperties['displayName'] }
    }
    @{
        N = 'mail'
        E = { $_.additionalProperties['mail'] }
    }
)

However this gets messy as soon as you need to pick more property values from the objects, PSCustomObject with a loop comes in handy in this case:但是,一旦您需要从对象中选择更多属性值,这就会变得混乱,在这种情况下,带有循环的PSCustomObject会派上用场:

$object | ForEach-Object {
    [pscustomobject]@{
        Id          = $_.Id
        displayName = $_.additionalProperties['displayName']
        mail        = $_.additionalProperties['mail']
    }
}

Both alternatives would output the same "flattened" object that can be converted to Csv without any issue:两种选择都会输出相同的“扁平化”对象,可以毫无问题地转换为 Csv:

  • As Object作为对象
Id          displayName mail
--          ----------- ----
placeholder placeholder placeholder
  • As Csv作为 CSV
"Id","displayName","mail"
"placeholder","placeholder","placeholder"

In that sense, you could construct an array of objects using one of the above techniques, for example:从这个意义上说,您可以使用上述技术之一构造一个对象数组,例如:

Get-MgGroupMember -GroupId $GroupID | ForEach-Object {
    [pscustomobject]@{
        Id                = $_.id
        displayName       = $_.additionalproperties['displayName']
        mail              = $_.additionalproperties['mail']
        userPrincipalName = $_.additionalproperties['userPrincipalName']
    }
}

If you're looking for a programmatical way to flatten the object, you can start by using this example, however it's important to note that this can only handle an object which's property is nested only once , in other words, it can't handle recursion:如果您正在寻找一种编程方式来展平对象,您可以从使用此示例开始,但重要的是要注意,这只能处理属性仅嵌套一次的对象,换句话说,它无法处理递归:

$newObject = [ordered]@{}
foreach($property in $object.PSObject.Properties) {
    if($property.Value -is [Collections.IDictionary]) {
        foreach($addproperty in $property.Value.GetEnumerator()) {
            $newObject[$addproperty.Key] = $addproperty.Value
        }
        continue
    }
    $newObject[$property.Name] = $property.Value
}
[pscustomobject] $newObject

The output from this would become a flattened object like this, which also, can be converted to Csv without any issue:这样的输出将成为一个像这样的扁平对象,它也可以毫无问题地转换为 Csv:

DeletedDateTime   : placeholder
Id                : placeholder
displayName       : placeholder
mail              : placeholder
userPrincipalName : placeholder

It's also worth noting that above example is not handling possible key collision , if there are 2 or more properties with the same name, one would override the others.还值得注意的是,上面的示例没有处理可能的键冲突,如果有 2 个或多个具有相同名称的属性,则其中一个会覆盖其他属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM