《Windows Azure Platform 系列文章目录

 

  在之前的文章中,我们已经介绍了如何创建1个Azure Automation Account。

  在本章节中,我们将介绍如何在Azure Automation中使用Connection连接和Variables变量

  请先按照之前的文章:Azure Automation (7) 新建Azure Automation Account,更新Module先创建1个Automation Account

 

  Automation里面有几个核心的概念:

  (1)Connection,设置到Azure的链接

  (2)Variables,设置Azure Automation的变量

  (3)Runbook,设置执行的脚本

  (4)Schdule,设置计划任务的时间,来调用Runbook

  

 

  本章实现的目的是,通过调用Azure Automation,定期对Azure Analysis Service进行扩容和伸缩操作。

 

 

  1.我们创建完毕Azure Automation Account之后,在Connections中会出现创建成功的Connection。

  这个Connection是通过Azure App Service Principle自动生成的,默认有效期为1年。

  Azure Automation (9) 在Automation中使用Connections连接和Variables变量
    




Windows Azure Platform 系列文章目录

  这个Connection我们后续会使用到。

  

  2.因为本章需要对Analysis Service进行操作。我们需要导入Az模块,分别是Az.AccountsAz.AnalysisServices。具体步骤略。

 

  3.在Variables,点击Add a variable。设置变量名和变量值。

  Azure Automation (9) 在Automation中使用Connections连接和Variables变量
    




Windows Azure Platform 系列文章目录

  分别设置4个变量

变量名 变量值 说明
ResourceGroupName 需要用户设置 Analysis Service所在资源组名称
ASServerName 需要用户设置  Analaysis Service名称
MinSKU 需要用户设置 

Analaysis Service最小SKU值

如S0,S1,S2,S4,S8

MaxSKU  需要用户设置

Analaysis Service最大SKU值

如S0,S1,S2,S4,S8

 

  5.在Runbooks里面,新增一个Runbook

  Azure Automation (9) 在Automation中使用Connections连接和Variables变量
    




Windows Azure Platform 系列文章目录

  

  6.在Runbooks里面,输入下面的内容。注意看下面的注释部分。

  具体的Automation Powershell我已经Share在我的GitHub:

  https://github.com/leizhang1984/AzureChinaAutomation/blob/master/Az/ScaleUpAAS.ps1

  https://github.com/leizhang1984/AzureChinaAutomation/blob/master/Az/ScaleDownAAS.ps1

 

$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Connect-AzAccount -EnvironmentName AzureChinaCloud -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint

# 获得变量
$ResourceGroupName = Get-AutomationVariable -Name 'ResourceGroupName'
$ASServerName = Get-AutomationVariable -Name 'ASServerName'
$MinSKU = Get-AutomationVariable -Name 'MinSKU'
"AAS最小SKU为 " + $MinSKU

$MaxSKU = Get-AutomationVariable -Name 'MaxSKU'
"AAS最大SKU为 " + $MaxSKU

#获得当前AAS状态
$srv = Get-AzAnalysisServicesServer -ResourceGroupName $ResourceGroupName -Name $ASServerName -WarningAction silentlyContinue -ErrorAction Stop
$CurrentSKU = $srv.Sku.Name 
"当前AAS SKU为 " + $CurrentSKU

$CurrentCapacity = $srv.Sku.Capacity
"当前AAS Capacity为 " + $CurrentCapacity

if ($srv.State -ne "Succeeded")
{
    Write-Output "AAS服务必须设置为启动状态"
    exit
}

#提升SKU
Set-AzAnalysisServicesServer -ResourceGroupName $ResourceGroupName -Name $ASServerName -Sku $MaxSKU -ErrorAction Stop | Out-Null
"AAS SKU已经设置为" + $MaxSKU

 

  7.输入完毕后,先点击Save,后点击Publish进行发布

  Azure Automation (9) 在Automation中使用Connections连接和Variables变量
    




Windows Azure Platform 系列文章目录

 

  8.发布完毕后,还可以进行调试

  Azure Automation (9) 在Automation中使用Connections连接和Variables变量
    




Windows Azure Platform 系列文章目录

 

  9.在调试过程中,还可以通过Jobs看到Automation执行的日志

  Azure Automation (9) 在Automation中使用Connections连接和Variables变量
    




Windows Azure Platform 系列文章目录

 

  10.Automation执行成功后,可以与Schdule计划任务做链接。具体步骤略。

  Azure Automation (9) 在Automation中使用Connections连接和Variables变量
    




Windows Azure Platform 系列文章目录

 

相关文章: