Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell: remove element from object

I’m trying to create a new Azure ARM deployment file scripted based on an existing ARM template.

Basically what I do is:

  1. Export a JSON file for a VM
  2. Convert the JSON file into an object in PShell
  3. Make changes
  4. Write a new JSON file

The problem I’m having now, is that the original JSON file has the storageprofile.imagereference enabled which needs to be deleted from the new JSON file.

I’ve tried to set the value of it (storageprofile.imagereference.id = $null) but ARM doesn’t like that and wants the whole storageprofile.imagereference entry removed.

So in my Pshell, my VMObjectFile is read like:

$VMObjectFile = (Get-Content $ImportFile | Out-String | ConvertFrom-Json)

And the property I want to remove is set on:

$VMObjectFile.resources.properties.storageProfile.imageProfile

But how do I delete the whole tree? I already got rid of the “id” value and afterward can set the $VMObjectFile.resources.properties.storageProfile.imageProfile to $null (removing the id attribute),

But how do I completely remove imagereference from the subtree $VMObjectFile.resources.properties.storageProfile as there are multiple entries that still exists under that path:

$VMObjectFile.resources.properties.storageProfile.osDisk
$VMObjectFile.resources.properties.storageProfile.dataDisk
like image 719
Roelf Zomerman Avatar asked Mar 09 '26 07:03

Roelf Zomerman


1 Answers

Removing properties from a PowerShell object is not as obvious as one would think:

$data = '{"a":{"b":{"c":"something","d":"another thing"}}}' | ConvertFrom-Json

$data.a.b.PSObject.Properties.Remove("c")

$data | ConvertTo-Json -Compress

Result:

{"a":{"b":{"d":"another thing"}}}
like image 135
Tomalak Avatar answered Mar 11 '26 21:03

Tomalak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!