public/New-ArmDeployment.ps1
<#
.Synopsis Starts an Azure ARM Template deployment with a scope defined in the template. .Description This CmdLet will wrap the complete logic and preparation for a deployment in a single command. It uses New-AzResourceGroupDeployment internally. .Parameter Stage The short name of the stage with a capitalized first letter (e.g. 'Test', 'Prod', 'Demo', 'Int') .Parameter TenantId The GUID of the Azure Tenant in which the subscription resides. .Parameter SubscriptionId The GUID of the subscription to which the deployment should be applied. .Parameter ProjectName The name of the project which will be used to build the name of the resource group and the resources. Leave this empty if your template parameter file contane of the following keys defining the name: project-name, projectName, ProjectName, project or Project. .Parameter ResourceGroupLocation The Azure location for the resource group (defaults to 'West Europe'). .Parameter TemplateFile The path to the template file (if empty the script searches for 'azuredeploy.json' in the current directory). .Parameter TemplateParameterFile Optional path to the template parameter file in JSON format. .Example New-AzdArmDeployment -Stage Test -TenantId 00000-00000-00000 -SubscriptionId 000000-00000-000000-00000 -WhatIf -TemplateFile c:\temp\azuredeploy.json Execute an ARM deployment for the Test stage using a deployment file in c:\temp folder #> Function New-ArmDeployment { [CmdLetBinding()] param ( [Parameter(Mandatory = $true)] [string] $Stage, [Parameter(Mandatory = $true)] [string] $TenantId, [Parameter(Mandatory = $true)] [string] $SubscriptionId, [string] $ProjectName, [string] $Location = "West Europe", [string] $TemplateFile = '.\azuredeploy.json', [string] $TemplateParameterFile, [switch] $NoLogo ) begin { if (!$NoLogo.IsPresent) { Write-Logo $MyInvocation.InvocationName } New-FunctionStartup try { [Microsoft.Azure.Common.Authentication.AzureSession]::ClientFactory.AddUserAgent("VSAzureTools-$UI$($host.name)".replace(' ', '_'), '3.0.0') } catch { } Set-StrictMode -Version 3 # check if deployment file exists $exists = Test-Path $TemplateFile -PathType Leaf if (!$exists) { throw "File $TemplateFile not found." } # build deployment name $DeploymentName = ((Get-ChildItem $TemplateFile).BaseName + '-' + ((Get-Date).ToUniversalTime()).ToString('MMdd-HHmm')); # ensure Azure context Write-HostDebug 'Setting Azure context...' Set-SubscriptionContext -TenantId $TenantId -SubscriptionId $SubscriptionId -NoLogo if (!$?) { Write-HostError "Could not set context." return -1 } } process { # build resource group name try { New-AzDeployment ` -Name $DeploymentName ` -Location $Location ` -TemplateFile $TemplateFile ` -TemplateParameterFile $TemplateParameterFile ` -Verbose } catch { Write-Host "Error: $_" -ForegroundColor Red return -1 } } } |