部署后如何在 Azure VM(win 7、8、10、服务器)上运行简单的自定义命令?



我正在研究如何在Azure中新部署的Windows VM上远程运行命令,并有几个基本问题。

似乎"自定义脚本扩展"是答案,但根据文档,声明仅适用于服务器操作系统:

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

我认为这是正确的吗?如果是这样,那么非服务器Windows操作系统呢?

继续前进,我尝试针对Windows Server 2016数据中心使用自定义脚本扩展,基于MS教程: 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

我的目标是创建一个新的 Windows VM,并指示它在部署后简单地创建一个新的目录。

命令行步骤:

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

这将返回错误:

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.'

是否应涉及额外的步骤才能在 VM 上成功完成此操作?

谢谢

正如 4c74356b41 所说,您使用的是 Linux 脚本扩展,对于 Windows 服务器,我们应该使用CustomScriptExtension,publisherMicrosoft.Compute

我们可以使用 CLI 2.0 将扩展名设置为 Windows VM,这是我的步骤:1.创建一个 json 文件,如下所示:

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

2.使用 CLI 设置窗口虚拟机的扩展: 我们可以使用此命令脚本:

az vm extension set -n CustomScriptExtension --publisher Microsoft.Compute --version 1.8 --vm-name jasonvm --resource-group vmm --settings C:UsersjasonDesktoptestjasontest5.json

结果如下:

C:Usersjason>az vm extension set -n CustomScriptExtension --publisher Microsoft.Compute --version 1.8 --vm-name jasonvm --resource-group vmm --settings C:UsersjasonDesktoptestjasontest5.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"
}

===========================================================================

==============================================================正如大卫所说,我们可以在没有 json 文件的情况下使用此命令:

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

你正在使用针对Windows VM的Linux脚本扩展,试着猜猜这会有多成功?这是您要查找的链接:
https://learn.microsoft.com/en-us/azure/virtual-machines/windows/extensions-customscript?toc=%2fazure%2fvirtual-machines%2fwindows%2ftoc.json

此外,自定义脚本扩展是可行的方法,或者你可以使用 DSC 扩展或 Azure 自动化,具体取决于实际需要的复杂性。

相关内容

最新更新