Search code examples
azure-powershellazure-cliazure-rest-api

Issues using rest api body in powershell with Azure cli


I'm trying to az rest put command to enable one extension on my vm but have some issues with my body and don't know what should I try next or change. My syntax is probably wrong

These are my lines

 az rest --method put --uri https://management.azure.com/$resourceId/extensions/MDE.Windows?api-version=2015-06-15 --body { 
    "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",
            "vNextEnabled": "true"
        },
        "protectedSettings": {
          "defenderForEndpointOnboardingScript": "$defenderForEndpointOnboardingScript"
        }
      }
    }

This then outputs following

ParserError: "name": "MDE.Windows", Unexpected token ':' in expression or statement.

i have tried --body with ' '

(Unsupported Media Type({"error":{"code":"UnsupportedMediaType","message":"The content media type '<null>' is not supported. Only 'application/json' is supported."}})) 

and " "

but no luck.

(The command line is too long)  

Any tips that I should try next or change in my code? Maybe try next something like this?

az rest --method put --uri https://management.azure.com/$resourceId/extensions/MDE.Windows?api-version=2015-06-15 --body @body.json

and just fyi: $defenderForEndpointOnboardingScrip is around 20 000 characters (one 'word').


Solution

  • ParserError: "name": "MDE.Windows", Unexpected token ':' in expression or statement.

    I have reproduced in my environment by using below command and I followed Microsoft-Document:

    yy = name of the vm

    az rest --method get --uri https://management.azure.com//subscriptions/$resourceId --body
    @{
    "name"="yy
    }
    

    enter image description here

    Alternatively, I have reproduced in powershell in my environment the below example and I followed Microsoft-Document:

    xx-Name of the Resource group

     $getParams = @{    
     ResourceGroupName = 'xx'     
     Method = 'GET' }
    

    Then I used below command:

    Invoke-AzRestMethod @getParams
    

    Then I get the below result:

    enter image description here

    So, you need to declare nested hash table like @{"x"="y"} like above example. You can use powershell CMD Invoke-AzRestMethod with put method it works similarly as az rest method.