Powershell脚本检测Azure账号是否登陆,资源组是否创建,并部署ARM template

来源:互联网 发布:淘宝捉猫猫是干什么的 编辑:程序博客网 时间:2024/05/20 16:11

这里写图片描述

前言:本文提供了一个脚本检测,有如下功能:

1. Azure账户是否已经登陆了,如果没登陆,会提示你登陆。
2. 要创建的资源组是否存在,存在的话不再创建,直接部署template,不存在就先创建资源组,再部署template。

## 简单定义变量$ResourceGroupName='myrsg'$Location='china east'## 检测是否已经登陆azure,如果没登陆,会跳转提示登陆。Try{Get-AzureRmContext -ErrorAction Continue}Catch [System.Management.Automation.PSInvalidOperationException]{Login-AzureRmAccount -EnvironmentName Azurechinacloud}## define the deploy function,指定部署文件的路径。可以是远端文件,也可以是本地文件。Function Deployment([string]$deployPath,[string]$deployParameterPath){    Write-Output "test the deployment"    test-AzureRmResourceGroupDeployment -ResourceGroupName $ResourceGroupName `      -TemplateFile $deployPath `      -TemplateParameterFile $deployParameterPath    Write-Output "deploy begin"    New-AzureRmResourceGroupDeployment -ResourceGroupName $ResourceGroupName `      -TemplateFile $deployPath `      -TemplateParameterFile $deployParameterPath}## 检测资源组是否存在,逻辑行为可定制。## reousrceGroup的部署是增量的形式,组下的已有资源不再被重新部署。$resourceGroup = Get-AzureRmResourceGroup -Name $ResourceGroupName -ErrorAction SilentlyContinueif ( -not $ResourceGroup ) {    Write-Output "Could not find resource group '$ResourceGroupName' - will create it"    Write-Output "Creating resource group '$ResourceGroupName' in location '$Location'"    New-AzureRmResourceGroup -Name $resourceGroupName -Location $Location    Deployment .\Desktop\template\template\azuredeploy.json .\Desktop\template\template\azuredeploy.parameters.json}else {    Write-Output "Using existing resource group '$ResourceGroupName'"     Deployment .\Desktop\template\template\azuredeploy.json .\Desktop\template\template\azuredeploy.parameters.json}
阅读全文
0 0
原创粉丝点击