简体   繁体   中英

Automated way to convert azure powershell scripts to ARM templates

Let's say I have (way too) many *.ps1 scripts that I'd to convert to ARM templates

Is there a way (a script, command, whatever) I can automatically convert a azure powershell *.ps1 to an ARM template, without having to actually do the deployment to Azure?

I'm not looking for a bullet-proof solution. If there's indeed an automated way to do the conversion which fails if the ps1 script isn't correct, I'm OK with that.

No, there's no way to do that (unless you can automate deployment + export, which would create flawed templates anyway).

The closest you can get to this is run all cmdlets with -Debug switch and capture HTTP requests they are doing and convert those to ARM Templates (shouldn't be too hard, copy\\paste and a bunch of editing)

As others have commented, there is no way to automatically convert PowerShell script to ARM templates. However, if you already have these resources deployed, you may consider using the ARM export feature to retrieve the ARM templates.

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

You can run the "-Debug" flag on any of the Azure Powershell cmdlets that relate to creating resources, like New-AzureRmVM, and the output will show you the ARM template that it's going to create:

Be wary of using this as I have found that the Powershell cmdlets are NOT the way you should be automating your deployments. You should be strictly using ARM as the Powershell cmdlets sometimes do not output the correct parameters needed for successful deployment since the Powershell cmdlets do not have a method of specifying the version of the ARM API to use.

Example output using the "New-AzureRmVM" with the the "-Debug" flag:

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

The above is a perfect example of "why not" to use Powershell, as currently, this will return an error:

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

As the API version (2018-04-01) the Powershell command is using to convert the Powershell input into a JSON ARM template doesn't allow for the parameter 'osDisk.Image" as it's expecting it to be formatted as:

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

Instead it's using

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM