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

本秂侑毒 提交于 2019-12-04 19:54:08

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'}"

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!