1 ## 简单定义变量 2 $ResourceGroupName='myrsg' 3 $Location='china east' 4 ## 检测是否已经登录azure,若是没登录,会跳转提示登录。 5 Try 6 { 7 Get-AzureRmContext -ErrorAction Continue 8 } 9 Catch [System.Management.Automation.PSInvalidOperationException] 10 { 11 Login-AzureRmAccount -EnvironmentName Azurechinacloud 12 } 13 ## define the deploy function,指定部署文件的路径。能够是远端文件,也能够是本地文件。 14 Function Deployment([string]$deployPath,[string]$deployParameterPath) 15 { 16 Write-Output "test the deployment" 17 test-AzureRmResourceGroupDeployment -ResourceGroupName $ResourceGroupName <code> 18 -TemplateFile $deployPath </code> 19 -TemplateParameterFile $deployParameterPath 20 Write-Output "deploy begin" 21 New-AzureRmResourceGroupDeployment -ResourceGroupName $ResourceGroupName <code> 22 -TemplateFile $deployPath </code> 23 -TemplateParameterFile $deployParameterPath 24 } 25 ## 检测资源组是否存在,逻辑行为可定制。 26 ## reousrceGroup的部署是增量的形式,组下的已有资源再也不被从新部署。 27 $resourceGroup = Get-AzureRmResourceGroup -Name $ResourceGroupName -ErrorAction SilentlyContinue 28 if ( -not $ResourceGroup ) { 29 30 Write-Output "Could not find resource group '$ResourceGroupName' - will create it" 31 32 Write-Output "Creating resource group '$ResourceGroupName' in location '$Location'" 33 New-AzureRmResourceGroup -Name $resourceGroupName -Location $Location 34 Deployment .\Desktop\template\template\azuredeploy.json .\Desktop\template\template\azuredeploy.parameters.json 35 } 36 else { 37 Write-Output "Using existing resource group '$ResourceGroupName'" 38 Deployment .\Desktop\template\template\azuredeploy.json .\Desktop\template\template\azuredeploy.parameters.json 39 }
主体逻辑大体如上,你能够本身优化一下。Line 11是登录China Azure的,登录global Azure移除参数便可。git
若是你对Azure ARM 不了解,能够参考以下,进行深刻学习:
ARM template
Azure ARM template githubgithub