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