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.
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
Und nun möchte ich das Children hinzufügen, welches ich mir an anderer Stellen zusammenbau.
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
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?
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
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?
Please also mark the comments that contributed to the solution of the article
Content-ID: 668597
Url: https://administrator.de/contentid/668597
Printed on: November 13, 2024 at 11:11 o'clock
2 Comments
Latest comment
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