将 azure powershell 脚本转换为 ARM 模板的自动化方法

Automated way to convert azure powershell scripts to ARM templates

假设我有(太多)许多 *.ps1 脚本,我要将其转换为 ARM 模板

有没有办法(脚本、命令等)我可以自动 将 azure powershell *.ps1 转换为 ARM 模板,无需 实际部署到 Azure?

我不是在寻找万无一失的解决方案。如果 ps1 脚本不正确,确实有一种自动转换失败的方法,我可以接受。

不,没有办法做到这一点(除非您可以自动部署 + 导出,否则无论如何都会创建有缺陷的模板)。

最接近的是 运行 所有具有 -Debug 的 cmdlet 切换并捕获他们正在执行的 HTTP 请求并将其转换为 ARM 模板(应该不会太难,copy\paste 和一堆编辑)

正如其他人评论的那样,无法将 PowerShell 脚本自动转换为 ARM 模板。但是,如果您已经部署了这些资源,则可以考虑使用 ARM 导出功能来检索 ARM 模板。

https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-manager-export-template

您可以 运行 任何与创建资源相关的 Azure Powershell cmdlet 上的“-Debug”标志,例如 New-AzureRmVM,输出将向您显示它将要创建的 ARM 模板:

小心使用它,因为我发现 Powershell cmdlet 不是您应该自动化部署的方式。您应该严格使用 ARM,因为 Powershell cmdlet 有时不会输出成功部署所需的正确参数,因为 Powershell cmdlet 没有指定要使用的 ARM API 版本的方法。

使用带有“-Debug”标志的 "New-AzureRmVM" 的示例输出:

New-AzureRmVM -ResourceGroupName $RGName -Location $Location -VM $VM -LicenseType "Windows_Server" -Debug

DEBUG: ============================ HTTP REQUEST ============================

HTTP Method:
PUT

Absolute Uri:
https://management.azure.com/subscriptions/<subscription>/resourceGroups/LL_SQL_Test/providers/Microsoft.Compute/virtualMachines/LLSQL3?api-version=2018-04-01

Headers:
x-ms-client-request-id        : 5920b683-e8fe-455e-969a-63f4c6e246d7
accept-language               : en-US

Body:
{
"properties": {
    "hardwareProfile": {
    "vmSize": "Standard_DS2_v2"
    },
    "storageProfile": {
    "osDisk": {
        "osType": "Windows",
        "image": {
        "uri": "https://<storageaccount>.blob.core.windows.net/vhds/<VM>.vhd"
        },
        "caching": "ReadWrite",
        "writeAcceleratorEnabled": false,
        "createOption": "FromImage",
        "diskSizeGB": 127,
        "managedDisk": {
        "storageAccountType": "Standard_LRS"
        }
    }
    },
    "osProfile": {
    "computerName": "<computername>",
    "adminUsername": "<username>",
    "adminPassword": "<Password>",
    "windowsConfiguration": {
        "provisionVMAgent": true,
        "enableAutomaticUpdates": true
    }
    },
    "networkProfile": {
    "networkInterfaces": [
        {
        "id": "/subscriptions/<subscription>/resourceGroups/<resourcegroup>/providers/Microsoft.Network/networkInterfaces/<NIC>"
        }
    ]
    },
    "diagnosticsProfile": {
    "bootDiagnostics": {
        "enabled": false
    }
    },
    "availabilitySet": {
    "id": "/subscriptions/<subscription>/resourceGroups/<resourcegroup>/providers/Microsoft.Compute/availabilitySets/SQL_Availability_Set_Test"
    },
    "licenseType": "Windows_Server"
},
"location": "West US"
}

以上是 "why not" 使用 Powershell 的完美示例,因为目前,这将 return 一个错误:

Body:
{
"error": {
    "code": "InvalidParameter",
    "target": "osDisk.image",
    "message": "Parameter 'osDisk.image' is not allowed."
}
}

作为 API 版本 (2018-04-01),Powershell 命令用于将 Powershell 输入转换为 JSON ARM 模板不允许参数 'osDisk.Image" 因为它期望它被格式化为:

"storageProfile": {
    "imageReference": {
        "id": "[resourceId('Microsoft.Compute/images', parameters('images_LL_SQL_IMG_name'))]"
    },
    "osDisk": {
        "osType": "Windows",

而是使用

"storageProfile": {
    "osDisk": {
        "osType": "Windows",
        "image": {
        "uri": "https://<storageaccount>.blob.core.windows.net/vhds/LLSQL220180621090257.vhd"
        },