How to run simple custom commands on a Azure VM (w

2020-07-29 00:37发布

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://docs.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://docs.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

2条回答
何必那么认真
2楼-- · 2020-07-29 00:54

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'}"
查看更多
ゆ 、 Hurt°
3楼-- · 2020-07-29 01:03

you are using a Linux Script extension against windows VM, try and guess how successful could that be? This is the link you are looking for:
https://docs.microsoft.com/en-us/azure/virtual-machines/windows/extensions-customscript?toc=%2fazure%2fvirtual-machines%2fwindows%2ftoc.json

Also, custom script extension is the way to go, or you might use DSC extension, or Azure Automation, depending on complexity of what you actually need.

查看更多
登录 后发表回答