Azure ARM模板-针对位于许多不同Az区域中的VM运行powershell脚本

问题描述 投票:0回答:1

我已经为Windows VM配置准备了ARM模板。代码如下。我测试了位于同一区域的VM的ARM模板遍历列表,它按预期工作,部署成功完成。问题是,当我想对位于许多az区域中的VM运行该代码时,有什么办法吗?

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "vmName": {
            "type": "string",
            "minLength": 1,
            "metadata": {
                "description": "List of virtual machines to be reconfigured, if using multiple VMs, make their names comma separate. E.g. VM01, VM02, VM03."
            },
            "defaultValue": "VM1,VM2"
        },
        "Location": {
            "type": "string",
            "metadata": {
                "description": "Location of the VM"
            },
            "defaultvalue": "WestEurope"
        },
        "customScriptFileToRun": {
            "type": "string",
            "metadata": {
                "description": "Specify the name of the configuration script"
            },
            "defaultvalue": "script.ps1"
        },
        "secureFileUri": {
            "type": "string",
            "defaultValue": "xxxxxxx",
            "metadata": {
                "description": "Secure SAS blob URL"
            }
        },
        "OMSWorkspaceResourceGroup": {
            "type": "string",
            "metadata": {
                "description": "Log analytics workspace Resource Group"
            },
            "defaultvalue": "yourLogAnalyticsRG"
        },
        "omsWorkspacename": {
            "type": "string",
            "metadata": {
                "description": "Log analytics workspace name"
            },
            "defaultvalue": "YourLoganalyticsworkspacename"
        }
    },
    "variables": {
        "vmListArray": "[split(parameters('vmName'),',')]",
        "commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -File', ' ', parameters('customScriptFileToRun'))]"
    },
    "resources": [
        {
            "type": "Microsoft.Compute/virtualMachines/extensions",
            "apiVersion": "2018-10-01",
            "name": "[concat(trim(variables('vmListArray')[copyIndex()]),'/WindowsRegModyfication')]",
            "copy": {
                "name": "ExtentionLooptoAllVMs",
                "count": "[length(variables('vmListArray'))]"
            },
            "location": "[parameters('Location')]",
            "properties": {
                "autoUpgradeMinorVersion": true,
                "settings": {
                    "fileUris": [
                        "[parameters('secureFileUri')]"
                    ],
                    "commandToExecute": "[variables('commandToExecute')]"
                },
                "publisher": "Microsoft.Compute",
                "type": "CustomScriptExtension",
                "typeHandlerVersion": "1.8",
                "protectedSettings": {}
            }
        }
    ]
}
azure-resource-manager azure-powershell azure-automation
1个回答
0
投票

根据您的需要,我们可以将vmName和location定义为Azure ARm模板中的参数,并使用PowerShell get-AzVm列出所有Windows VM。

例如

我的template.json

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "vmName": {
            "type": "string",
            "minLength": 1,
            "metadata": {
                "description": "List of virtual machines to be reconfigured, if using multiple VMs, make their names comma separate. E.g. VM01, VM02, VM03."
            }
        },
        "Location": {
            "type": "string",
            "metadata": {
                "description": "Location of the VM"
            }
        },
        "customScriptFileToRun": {
            "type": "string",
            "metadata": {
                "description": "Specify the name of the configuration script"
            },
            "defaultvalue": "installWebServer.ps1"
        },
        "secureFileUri": {
            "type": "string",
            "defaultValue": "https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/tutorial-vm-extension/installWebServer.ps1",
            "metadata": {
                "description": "the file URL"
            }
        }

    },
    "variables": {

        "commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -File', ' ', parameters('customScriptFileToRun'))]"
    },
    "resources": [
        {

             "type": "Microsoft.Compute/virtualMachines",
            "apiVersion": "2019-07-01",
            "name": "[parameters('vmName')]",
            "location": "[parameters('Location')]",

            "properties": {


            }

        },
        {
            "type": "Microsoft.Compute/virtualMachines/extensions",
            "apiVersion": "2019-07-01",
            "name": "[concat( parameters('vmName'),'/WindowsRegModyfication')]",
            "dependsOn": [
                "[resourceId('Microsoft.Compute/virtualMachines', parameters('vmName'))]"
            ],
            "location": "[parameters('Location')]",
            "properties": {
                "autoUpgradeMinorVersion": true,
                "settings": {
                    "fileUris": [
                        "[parameters('secureFileUri')]"
                    ],
                    "commandToExecute": "[variables('commandToExecute')]"
                },
                "publisher": "Microsoft.Compute",
                "type": "CustomScriptExtension",
                "typeHandlerVersion": "1.8",
                "protectedSettings": {}
            }
        }
    ]
}
  1. 部署
$vms=Get-AzVM | Where-Object {$_.StorageProfile.OsDisk.OsType -eq 'Windows'}
foreach($vm in $vms){

   $TemplateObject=@{"vmName"=$vm.Name;"Location"=$vm.Location}

  New-AzResourceGroupDeployment -ResourceGroupName $vm.ResourceGroupName -TemplateFile "E:\template.json" -TemplateParameterObject $TemplateObject -Debug

}
© www.soinside.com 2019 - 2024. All rights reserved.