Search code examples
azureazure-cli2

How to run simple custom commands on a Azure VM (win 7,8,10, Server) post deploy?


I'm looking into how I can run commands remotely on a freshly deployed windows VM in Azure, and have a few basic questions.

It seems like the 'Custom Script Extension' is the answer, but according to the documentation, is stated as only applicable for Server operating systems:

https://learn.microsoft.com/en-us/azure/virtual-machines/windows/extensions-customscript

This is correct I assume? And if so, what about non-server windows OS?

Moving on, I have tried using the Custom Script Extension against a Windows Server 2016 data centre, based on the MS tutorial at: https://learn.microsoft.com/en-us/azure/virtual-machines/scripts/virtual-machines-linux-cli-sample-create-vm-nginx?toc=%2fazure%2fvirtual-machines%2flinux%2ftoc.json

My aim was to create a new Windows VM and instruct it to simply create a new dir after deployment.

CLI Steps:

1. Create a resource group
2. Create a new virtual machine (Server 2016 Datacentre) 
3. Finally, run the following command:

az vm extension set --publisher Microsoft.Azure.Extensions --version 2.0 --name CustomScript --vm-name (nameOfMyVM) --resource-group (nameOfMyResourceGroup) --settings '{"commandToExecute":"powershell.exe md c:\testFolder"}'

This returns the error:

Handler 'Microsoft.Azure.Extensions.CustomScript' has reported failure for VM Extension 'CustomScript' with terminal error code '1007' and error message: 'Install failed for plugin (name: Microsoft.Azure.Extensions.CustomScript, version 2.0.3) with exception The specified executable is not a valid application for this OS platform.'

Should extra steps have been involved to accomplish this action on the VM successfully?

Thanks


Solution

  • As 4c74356b41 said, you are using a Linux Script extension, for windows server, we should use CustomScriptExtension and the publisher is Microsoft.Compute.

    We can use CLI 2.0 to set extension to windows VM, here are my steps:
    1.create a json file, like this:

    {
      "commandToExecute": "powershell.exe mkdir C:\\test321"
    }
    

    2.Use CLI to set extension for windows VM: we can use this command script:

    az vm extension set -n CustomScriptExtension --publisher Microsoft.Compute --version 1.8 --vm-name jasonvm --resource-group vmm --settings C:\Users\jason\Desktop\test\jasontest5.json
    

    Here is the result:

    C:\Users\jason>az vm extension set -n CustomScriptExtension --publisher Microsoft.Compute --version 1.8 --vm-name jasonvm --resource-group vmm --settings C:\Users\jason\Desktop\test\jasontest5.json
    {
      "autoUpgradeMinorVersion": true,
      "forceUpdateTag": null,
      "id": "/subscriptions/5384xxxx-xxxx-xxxx-xxxx-xxxxe29a7b15/resourceGroups/vmm/providers/Microsoft.Compute/virtualMachines/jasonvm/extensions/CustomScriptExtension",
      "instanceView": null,
      "location": "centralus",
      "name": "CustomScriptExtension",
      "protectedSettings": null,
      "provisioningState": "Succeeded",
      "publisher": "Microsoft.Compute",
      "resourceGroup": "vmm",
      "settings": {
        "commandToExecute": "powershell.exe mkdir C:\\test321"
      },
      "tags": null,
      "type": "Microsoft.Compute/virtualMachines/extensions",
      "typeHandlerVersion": "1.8",
      "virtualMachineExtensionType": "CustomScriptExtension"
    }
    

    ==========================================
    Update:

    As David said, we can use this command without json file:

    az vm extension set -n CustomScriptExtension --publisher Microsoft.Compute --version 1.8 --vm-name DVWinServerVMB --resource-group DVResourceGroup --settings "{'commandToExecute': 'powershell.exe md c:\\test'}"