I try to run a PowershellScript on an AZURE VM with the GO SDK.
After several other not successful attempts with e.g. RunCommand I decided to use the REST API with the autorest package.
Here my code:
RunCommandsClient := compute.NewVirtualMachineRunCommandsClient(subscriptionID)
RunCommandsClient.Authorizer = authorizer
RestClient := autorest.Client{Sender: mocks.NewSender()}
RestClient.Authorizer = authorizer
m, b := map[string]string{
"commandId": "RunPowerShellScript",
"script": "[$test = Get-Date /r/n Add-Content C:\\MPDV\\Test.txt $test]",
}, new(bytes.Buffer)
json.NewEncoder(b).Encode(m)
r, err := http.NewRequest("POST", "https://management.azure.com/subscriptions/52276c4b-d079-49c2-a957-becc3fe931f9/resourceGroups/RESGRP_MPDV2/providers/Microsoft.Compute/virtualMachines/TEST-SPI/runCommand?api-version=2021-07-01", b)
if err != nil {
panic(e)
}
log.Print(r)
//var Decorator autorest.SendDecorator
//MyDecorator := Decorator
MyResponse, err := autorest.SendWithSender(RestClient, r)
//MyResponse, err := RestClient.Do(r)
if err != nil {
panic(e)
}
log.Print(MyResponse)
The authorisation is working. The SPN has OWNER rights on subscription level.
Unfortunatly the response code is "only" HTTP 200 (OK) not HTTP 202 (ACEPTED). The PowershellScript has not been executed.
Using the same HTTP request on the AZURE REST API test site: https://learn.microsoft.com/de-de/rest/api/compute/virtual-machines-run-commands/run-command#code-try-0
Everything works perfect.
What's wrong with my code above?