博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Azure Powershell script检测登陆并部署ARM Template
阅读量:4688 次
发布时间:2019-06-09

本文共 1908 字,大约阅读时间需要 6 分钟。

本文简单提供了一个Azure powershell脚本,能实现如下功能

  1. Azure (China)账户是否已经登陆了,如果没登陆,会提示你登陆。
  2. 要创建的资源组是否存在,存在的话不再创建,直接部署template,不存在就先创建资源组,再部署template。
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 Azurechinacloud12 }13 ## define the deploy function,指定部署文件的路径。可以是远端文件,也可以是本地文件。14 Function Deployment([string]$deployPath,[string]$deployParameterPath)15 {16     Write-Output "test the deployment"17     test-AzureRmResourceGroupDeployment -ResourceGroupName $ResourceGroupName 18       -TemplateFile $deployPath 19       -TemplateParameterFile $deployParameterPath20     Write-Output "deploy begin"21     New-AzureRmResourceGroupDeployment -ResourceGroupName $ResourceGroupName 22       -TemplateFile $deployPath 23       -TemplateParameterFile $deployParameterPath24 }25 ## 检测资源组是否存在,逻辑行为可定制。26 ## reousrceGroup的部署是增量的形式,组下的已有资源不再被重新部署。27 $resourceGroup = Get-AzureRmResourceGroup -Name $ResourceGroupName -ErrorAction SilentlyContinue28 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 $Location34     Deployment .\Desktop\template\template\azuredeploy.json .\Desktop\template\template\azuredeploy.parameters.json35 }36 else {37     Write-Output "Using existing resource group '$ResourceGroupName'" 38     Deployment .\Desktop\template\template\azuredeploy.json .\Desktop\template\template\azuredeploy.parameters.json39 }

 

主体逻辑大致如上,你可以自己优化一下。Line 11是登陆China Azure的,登陆global Azure移除参数即可。

如果你对Azure ARM 不了解,可以参考如下,进行深入学习:

转载于:https://www.cnblogs.com/yangwenbo214/p/9836215.html

你可能感兴趣的文章
MySQL 网络访问连接
查看>>
在aws ec2上使用root用户登录
查看>>
数据访问 投票习题
查看>>
CIO知识储备
查看>>
cnblog!i'm coming!
查看>>
使用点符号代替溢出的文本
查看>>
Axios 中文说明
查看>>
fatal: remote origin already exists.
查看>>
gridview 自定义value值
查看>>
2018二月实现计划成果及其三月规划
查看>>
封装springmvc处理ajax请求结果
查看>>
tyvj P2018 「Nescafé26」小猫爬山 解题报告
查看>>
类名.class和getClass()区别
查看>>
开发脚本自动部署及监控
查看>>
JavaScript--语句
查看>>
12/17面试题
查看>>
css 继承和层叠
查看>>
javascript实现图片轮播3D效果
查看>>
ssl初一组周六模拟赛【2018.3.17】
查看>>
[RxJS] Avoid mulit post requests by using shareReplay()
查看>>