derhoeppi
Goto Top

Powershell nested Hashtables - Problem beim Hinzufügen

Ich experimentiere gerade mit der Powershell und der Microsoft Graph API herum. Hier habe ich die Herausforderung das ich zum Setzen von Settings eine bestimmte JSON Form bereitstellen muss.
Damit ich diese Abbilden kann, bilde ich eine Hashtable in der wiederum Arrays und Hahstables beherbergt werden. Nun gibt es jedoch zwei Bereiche, die dynamisch sein sollen, weil die Einträge variieren können. Genau an diesem Punkt scheitere ich. Ich baue mir dazu kleinere Hashtables, die ich im Anschluss in der vorhandenen Hashtable hinzufügen möchte.

Mein JSON welches ich nachbauen möchte, schaut folgendermaßen aus.
{
    "id": "74f31ba5-b2ab-4d2a-86cf-e6e432ac24e0",  
    "displayName": "Graph-Test",  
    "description": "",  
    "settingDefinitionId": "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata",  
    "settingInstance": {  
        "@odata.type": "#microsoft.graph.deviceManagementConfigurationGroupSettingCollectionInstance",  
        "groupSettingCollectionValue": [  
            {
                "children": [  
                    {
                        "@odata.type": "#microsoft.graph.deviceManagementConfigurationGroupSettingCollectionInstance",  
                        "groupSettingCollectionValue": [  
                            {
                                "children": [  
                                    {
                                        "@odata.type": "#microsoft.graph.deviceManagementConfigurationSimpleSettingInstance",  
                                        "settingDefinitionId": "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata_descriptoridlist_name",  
                                        "simpleSettingValue": {  
                                            "@odata.type": "#microsoft.graph.deviceManagementConfigurationStringSettingValue",  
                                            "value": "Test1"  
                                        }
                                    },
                                    {
                                        "@odata.type": "#microsoft.graph.deviceManagementConfigurationSimpleSettingInstance",  
                                        "settingDefinitionId": "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata_descriptoridlist_serialnumberid",  
                                        "simpleSettingValue": {  
                                            "@odata.type": "#microsoft.graph.deviceManagementConfigurationStringSettingValue",  
                                            "value": "001E0BB89D74BCA0B0004E8F"  
                                        }
                                    } 
                                ]
                            }
                        ],
                        "settingDefinitionId": "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata_descriptoridlist"  
                    },
                    {
                        "@odata.type": "#microsoft.graph.deviceManagementConfigurationGroupSettingCollectionInstance",  
                        "groupSettingCollectionValue": [  
                            {
                                "children": [  
                                    {
                                        "@odata.type": "#microsoft.graph.deviceManagementConfigurationSimpleSettingInstance",  
                                        "settingDefinitionId": "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata_descriptoridlist_name",  
                                        "simpleSettingValue": {  
                                            "@odata.type": "#microsoft.graph.deviceManagementConfigurationStringSettingValue",  
                                            "value": "Test2"  
                                        }
                                    },
                                    {
                                        "@odata.type": "#microsoft.graph.deviceManagementConfigurationSimpleSettingInstance",  
                                        "settingDefinitionId": "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata_descriptoridlist_friendlynameid",  
                                        "simpleSettingValue": {  
                                            "@odata.type": "#microsoft.graph.deviceManagementConfigurationStringSettingValue",  
                                            "value": "Test2"  
                                        }
                                    }
                                ]
                            }
                        ],
                        "settingDefinitionId": "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata_descriptoridlist"  
                    },
                    {
                        "@odata.type": "#microsoft.graph.deviceManagementConfigurationChoiceSettingInstance",  
                        "choiceSettingValue": {  
                            "@odata.type": "#microsoft.graph.deviceManagementConfigurationChoiceSettingValue",  
                            "children": [],  
                            "value": "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata_matchtype_matchany"  
                        },
                        "settingDefinitionId": "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata_matchtype"  
                    }
                ]
            }
        ],
        "settingDefinitionId": "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata"  
    },
    "@odata.type": "#microsoft.graph.deviceManagementReusablePolicySetting"  
}

Jeweils das Setting children ist variable. Jeweils unter groupSettingCollectionValue. Zum Testen habe ich mir eine CSV erstellt, die ich einlese. Letztlich bilde ich ein Grundgerüst

    $GroupDefinition = @{
        "@odata.type"= "#microsoft.graph.deviceManagementReusablePolicySetting"  
        displayName = "$GroupName"  
        description = ""  
        settingDefinitionId = "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata"  
        settingInstance = @( 
            @{
            "@odata.type" = "#microsoft.graph.deviceManagementConfigurationGroupSettingCollectionInstance"  
            groupSettingCollectionValue = @( 
                @{
                    children = @( 
                        @{
                            "@odata.type"= "#microsoft.graph.deviceManagementConfigurationChoiceSettingInstance"  
                            choiceSettingValue = @(
                                @{
                                    "@odata.type"=  "#microsoft.graph.deviceManagementConfigurationChoiceSettingValue"  
                                    children = @()
                                    value = "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata_matchtype_matchany"  
                                }
                            )
                            settingDefinitionId = "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata_matchtype"  
                        }
                    )
                }
            )
            settingDefinitionId = "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata"      
            }
        )  
    }

Und nun möchte ich das Children hinzufügen, welches ich mir an anderer Stellen zusammenbau.
 $GroupBasicDefinition = @{
         "@odata.type" = "#microsoft.graph.deviceManagementConfigurationGroupSettingCollectionInstance"  
         groupSettingCollectionValue = @( 
            @{
                children = @( @{})
            }
         )
         settingDefinitionId = "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata_descriptoridlist"  
    
    }
    
    
    
    
    $SettingName = $GroupEntry.Name
    $Groupchildsettings = @()
        # Name of entry
        if ($GroupEntry.Name){
            $child_name = @(
                @{
                    "@odata.type" = "#microsoft.graph.deviceManagementConfigurationSimpleSettingInstance"  
                    settingDefinitionId = "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata_descriptoridlist_name"  
                    simpleSettingValue = @(
                    @{
                        "@odata.type" = "#microsoft.graph.deviceManagementConfigurationStringSettingValue"  
                        value = $GroupEntry.Name
                    }
                    )
                }
            )
            
            $Groupchildsettings += $child_name
        }
        # SerialNumberId of entry
        if ($GroupEntry.SerialNumberId){
            $child_serialnumberid = @(
                @{
                    "@odata.type" = "#microsoft.graph.deviceManagementConfigurationSimpleSettingInstance"  
                    settingDefinitionId = "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata_descriptoridlist_serialnumberid"  
                    simpleSettingValue = @(
                    @{
                        "@odata.type" = "#microsoft.graph.deviceManagementConfigurationStringSettingValue"  
                        value = $GroupEntry.SerialNumberId
                    }
                    )
                }
            )
        
            $Groupchildsettings += $child_serialnumberid
        } 

Ich habe nun in $Groupchildsettings meine (innersten) Childrens. Egal was ich tue sie kommen aber nicht in $GroupBasicDefinition.groupSettingCollectionValue.children an. Versucht habe ich es mit
$GroupBasicDefinition.groupSettingCollectionValue | Add-Member -NotePropertyName children -NotePropertyValue $Groupchildsettings
aber auch manuelles hinzufügen in der ISE hat nicht zum Erfolg geführt.

Hat jemand von euch eine Idee wie ich das ganze in mein Objekt bekomme, so dass ich es zum Schluss als JSON für die Graph API nutzen kann?

Content-ID: 668597

Url: https://administrator.de/forum/powershell-nested-hashtables-problem-beim-hinzufuegen-668597.html

Ausgedruckt am: 22.12.2024 um 05:12 Uhr

150704
Lösung 150704 05.10.2024 aktualisiert um 16:25:29 Uhr
Goto Top
Hinzufügen kannst du so
# load json into object
$data = @'  
{
    "id": "74f31ba5-b2ab-4d2a-86cf-e6e432ac24e0",    
    "displayName": "Graph-Test",    
    "description": "",    
    "settingDefinitionId": "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata",    
    "settingInstance": {    
        "@odata.type": "#microsoft.graph.deviceManagementConfigurationGroupSettingCollectionInstance",    
        "groupSettingCollectionValue": [    
            {
                "children": [    
                    {
                        "@odata.type": "#microsoft.graph.deviceManagementConfigurationGroupSettingCollectionInstance",    
                        "groupSettingCollectionValue": [    
                            {
                                "children": [    
                                    {
                                        "@odata.type": "#microsoft.graph.deviceManagementConfigurationSimpleSettingInstance",    
                                        "settingDefinitionId": "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata_descriptoridlist_name",    
                                        "simpleSettingValue": {    
                                            "@odata.type": "#microsoft.graph.deviceManagementConfigurationStringSettingValue",    
                                            "value": "Test1"    
                                        }
                                    },
                                    {
                                        "@odata.type": "#microsoft.graph.deviceManagementConfigurationSimpleSettingInstance",    
                                        "settingDefinitionId": "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata_descriptoridlist_serialnumberid",    
                                        "simpleSettingValue": {    
                                            "@odata.type": "#microsoft.graph.deviceManagementConfigurationStringSettingValue",    
                                            "value": "001E0BB89D74BCA0B0004E8F"    
                                        }
                                    } 
                                ]
                            }
                        ],
                        "settingDefinitionId": "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata_descriptoridlist"    
                    },
                    {
                        "@odata.type": "#microsoft.graph.deviceManagementConfigurationGroupSettingCollectionInstance",    
                        "groupSettingCollectionValue": [    
                            {
                                "children": [    
                                    {
                                        "@odata.type": "#microsoft.graph.deviceManagementConfigurationSimpleSettingInstance",    
                                        "settingDefinitionId": "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata_descriptoridlist_name",    
                                        "simpleSettingValue": {    
                                            "@odata.type": "#microsoft.graph.deviceManagementConfigurationStringSettingValue",    
                                            "value": "Test2"    
                                        }
                                    },
                                    {
                                        "@odata.type": "#microsoft.graph.deviceManagementConfigurationSimpleSettingInstance",    
                                        "settingDefinitionId": "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata_descriptoridlist_friendlynameid",    
                                        "simpleSettingValue": {    
                                            "@odata.type": "#microsoft.graph.deviceManagementConfigurationStringSettingValue",    
                                            "value": "Test2"    
                                        }
                                    }
                                ]
                            }
                        ],
                        "settingDefinitionId": "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata_descriptoridlist"    
                    },
                    {
                        "@odata.type": "#microsoft.graph.deviceManagementConfigurationChoiceSettingInstance",    
                        "choiceSettingValue": {    
                            "@odata.type": "#microsoft.graph.deviceManagementConfigurationChoiceSettingValue",    
                            "children": [],    
                            "value": "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata_matchtype_matchany"    
                        },
                        "settingDefinitionId": "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata_matchtype"    
                    }
                ]
            }
        ],
        "settingDefinitionId": "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata"    
    },
    "@odata.type": "#microsoft.graph.deviceManagementReusablePolicySetting"    
}
'@ | Convertfrom-JSON  

# add child section
$data.settingInstance.groupSettingCollectionValue[0].children += [pscustomobject][ordered]@{
    "@odata.type" = "#microsoft.graph.deviceManagementConfigurationSimpleSettingInstance"    
    settingDefinitionId = "device_vendor_msft_defender_configuration_devicecontrol_policygroups_{groupid}_groupdata_descriptoridlist_name"    
    simpleSettingValue =@{
        "@odata.type" = "#microsoft.graph.deviceManagementConfigurationStringSettingValue"    
        value = "BlaBlub"  
    }
}
# output result
$data | ConvertTo-Json -Depth 100
derhoeppi
derhoeppi 05.10.2024 um 16:09:01 Uhr
Goto Top
@150704 wow - manchmal können die Fehler wirklich klein sein. Habe es direkt im Code eingebaut und entscheidend war nur die Adressierung des ersten Eintrags in groupSettingCollectionValue.
Durch deinen Code habe ich jedoch noch andere Ideen erhalten, wie ich meinen Code vereinfachen kann. Hast mir super geholfen.