• Azure Powershell script检测登陆并部署ARM Template


    本文简单提供了一个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 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 &quot;deploy begin&quot;
    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 &quot;Could not find resource group '$ResourceGroupName' - will create it&quot;
    31  
    32     Write-Output &quot;Creating resource group '$ResourceGroupName' in location '$Location'&quot;
    33     New-AzureRmResourceGroup -Name $resourceGroupName -Location $Location
    34     Deployment .Desktop	emplate	emplateazuredeploy.json .Desktop	emplate	emplateazuredeploy.parameters.json
    35 }
    36 else {
    37     Write-Output &quot;Using existing resource group '$ResourceGroupName'&quot; 
    38     Deployment .Desktop	emplate	emplateazuredeploy.json .Desktop	emplate	emplateazuredeploy.parameters.json
    39 }

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

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

  • 相关阅读:
    chrome 等浏览器不支持本地ajax请求的问题
    3:1 类型转换
    WebService-WSDL简单介绍
    WebService—CXF整合Spring实现接口发布和调用过程
    WebService—CXF—实现接口发布和客户端调用
    WebService—规范介绍和几种实现WebService的框架介绍
    (转)c# 扩展方法
    (转)C# Textbox的ImeMode取值对中文输入法的影响
    (转)Nandflash读写
    (转+整理)Nandflash存储
  • 原文地址:https://www.cnblogs.com/yangwenbo214/p/9836215.html
Copyright © 2020-2023  润新知