Home > OS >  issues PowerShell POST request with body
issues PowerShell POST request with body

Time:10-26

I have a bit issue with powershell invoke-webrequest

This is my body

$pram = @{
   "name": "MDE.Windows",
   "id": "$resourceId/extensions/MDE.Windows",
   "type": "Microsoft.Compute/virtualMachines/extensions",
   "location": "westeurope",
   "properties": {
     "autoUpgradeMinorVersion": true,
     "publisher": "Microsoft.Azure.AzureDefenderForServers",
     "type": "MDE.Windows",
     "typeHandlerVersion": "1.0",
     "settings": {
       "azureResourceId": "$resourceId",
       "defenderForServersWorkspaceId": "$subscriptionId",
       "vNextEnabled": "true",
       "forceReOnboarding": true,
       "provisionedBy": "Manual"
     },
     "protectedSettings": {
       "defenderForEndpointOnboardingScript": "$defenderForEndpointOnboardingScript"
     }
   }
 }

I don't get watch wrong with my body because by looking examples from google this should be right but it still ouputs red

enter image description here

I have tried also with @"{ }"@, @'{ }'@, { }, "{ }" but no matter what I do it is more or less red.

CodePudding user response:

I think your mistaking powershell hash tables for json. Normally you would create a hashtable using powershell syntax, then convert that object into Json. eg

$pram = @{
   name= "MDE.Windows";
   id= "$resourceId/extensions/MDE.Windows";
 } | ConvertTo-Json

You can now pass the json encoded value of $parm to Invoke-WebRequest.

The other option is to create a String and write the JSON yourself:

$paramString = '{"id":  "/extensions/MDE.Windows", "name":  "MDE.Windows"}'

(but the first solution is probably the solution your looking for).

  • Related