优秀的编程知识分享平台

网站首页 > 技术文章 正文

通过Azure Automation自动化脚本 定时开启和关闭虚拟机

nanyue 2024-09-09 05:02:13 技术文章 6 ℃

应用场景

在许多应用场景中我们都有类似定时开启和关闭虚拟机这样的需求,比如凌晨12点关闭虚拟机,早上5点再开启;或者我们需要批量对成百上千台虚拟机做开启和关闭操作,对于类似这种场景靠工程师手动去操作肯定是不现实的,也很容易出错。

那么,在这里我们介绍下通过Azure自动化脚本来实现这种定时后者大量重复性的工作。而使用过Azure的童鞋知道Azure上的虚拟机用ASM和ARM两种,而对于这两种虚拟机的自动化脚本还是用所区别的,下面我们就详细看看脚本的实现。

创建Azure Automation

  • 创建Azure Automation要先进入Azure Portal(https://manage.windowsazure.cn)界面。

请点击此处输入图片描述

  • 按照上图先创建一个Azure Automation账户,然后可以在这个Auto

请点击此处输入图片描述

自动化脚本(Runbook)

我们先来看一下如何在Runbook上编写PS脚本,下图以开启ASM虚拟机为例。

请点击此处输入图片描述

下面所有的Demo Code都是以一台虚拟机做测试,对于批量操作几十台甚至几百台虚拟机只需要修改一下脚本的逻辑。

  1. 开启ASM虚拟机脚本

workflow StartAzureVM

{

#获取Azure账号凭证

$Cert = Get-AutomationPSCredential -Name 'PSCertificate'

$SubscriptionId = '{订阅ID}'

$ServiceName = '{云服务名称}'

$VMName='{虚拟机名称}'

#登录Azure账号

Add-AzureAccount -Environment AzureChinaCloud -Credential $Cert

#选中指定订阅

Select-AzureSubscription -SubscriptionId $SubscriptionId

#获取指定虚拟机

$VM = Get-AzureVM -ServiceName $ServiceName -Name $VMName

#判断虚拟机状态

if($VM.PowerState -eq "Started"){

#虚拟机已经启动

Write-Output ($VM.InstanceName + " is already running")

}else{

#启动虚拟机

$StartRtn = Start-AzureVM -Name $VM.Name -ServiceName $VM.ServiceName -ErrorAction Continue

#判断虚拟机是否启动成功

if ($StartRtn.OperationStatus -ne 'Succeeded')

{

# The VM failed to start, so send notice

Write-Output ($VM.InstanceName + " failed to start")

}

else

{

# The VM started, so send notice

Write-Output ($VM.InstanceName + " has been started")

}

}

}

  1. 关闭ASM虚拟机脚本

workflow StopAzureVM

{

#获取Azure账号凭证

$Cert = Get-AutomationPSCredential -Name 'PSCertificate'

$SubscriptionId = '34eef720-74d9-4428-9056-e47df19ef6cd'

$ServiceName = 'trainservice'

$VMName='trainwebvm02'

#登录Azure账号

Add-AzureAccount -Environment AzureChinaCloud -Credential $Cert

#选中指定订阅

Select-AzureSubscription -SubscriptionId $SubscriptionId

#获取指定虚拟机

$VM = Get-AzureVM -ServiceName $ServiceName -Name $VMName

#判断虚拟机状态

if($VM.PowerState -eq "Stopped"){

#虚拟机已经关闭

Write-Output ($VM.InstanceName + " is already stoped")

}else{

#关闭虚拟机

$StopRtn = Stop-AzureVM -Name $VM.Name -ServiceName $VM.ServiceName -Force -ErrorAction Continue

#判断虚拟机是否关闭成功

if ($StopRtn.OperationStatus -ne 'Succeeded')

{

# The VM failed to stop, so send notice

Write-Output ($VM.InstanceName + " failed to stop")

}

else

{

# The VM stoped, so send notice

Write-Output ($VM.InstanceName + " has been stoped")

}

}

}

开启ARM虚拟机脚本

workflow StartAzureRmVM

{

#获取Azure账号凭证

$Cert = Get-AutomationPSCredential -Name 'PSCertificate'

$SubscriptionId='34eef720-74d9-4428-9056-e47df19ef6cd'

$ResourceGroupName='tbfilemigrate'

$VMName='tbfilemigrate'

#登录Azure账号

Add-AzureRmAccount -EnvironmentName AzureChinaCloud -Credential $Cert

#选择指定订阅

Select-AzureRmSubscription -SubscriptionId 34eef720-74d9-4428-9056-e47df19ef6cd

#获取虚拟机

$VM = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMName

#启动虚拟机,这里我们甚至可以自己实现Retry策略

$StartRtn=Start-AzureRmVM -ResourceGroupName $VM.ResourceGroupName -Name $VM.Name -ErrorAction SilentlyContinue

#判断虚拟机是否启动成功

if($StartRtn.StatusCode -ne 'OK'){

Write-Output ($VM.Name + " failed to start")

}else{

Write-Output ($VM.Name + " has been started")

}

}

关闭ARM虚拟机脚本

workflow StopAzureRmVM

{

#获取Azure账号凭证

$Cert = Get-AutomationPSCredential -Name 'PSCertificate'

$SubscriptionId='34eef720-74d9-4428-9056-e47df19ef6cd'

$ResourceGroupName='tbfilemigrate'

$VMName='tbfilemigrate'

#登陆Azure账号

Add-AzureRmAccount -EnvironmentName AzureChinaCloud -Credential $Cert

#选中指定Azure订阅

Select-AzureRmSubscription -SubscriptionId 34eef720-74d9-4428-9056-e47df19ef6cd

#获取虚拟机

$VM = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMName

#关闭虚拟机

$StartRtn=Stop-AzureRmVM -ResourceGroupName $VM.ResourceGroupName -Name $VM.Name -ErrorAction SilentlyContinue -Force

#判断虚拟机是否关闭成功

if($StartRtn.StatusCode -ne 'OK'){

Write-Output ($VM.Name + " failed to stop")

}else{

Write-Output ($VM.Name + " has been stoped")

}

}

关闭ARM虚拟机脚本

workflow StopAzureRmVM

{

#获取Azure账号凭证

$Cert = Get-AutomationPSCredential -Name 'PSCertificate'

$SubscriptionId='34eef720-74d9-4428-9056-e47df19ef6cd'

$ResourceGroupName='tbfilemigrate'

$VMName='tbfilemigrate'

#登陆Azure账号

Add-AzureRmAccount -EnvironmentName AzureChinaCloud -Credential $Cert

#选中指定Azure订阅

Select-AzureRmSubscription -SubscriptionId 34eef720-74d9-4428-9056-e47df19ef6cd

#获取虚拟机

$VM = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMName

#关闭虚拟机

$StartRtn=Stop-AzureRmVM -ResourceGroupName $VM.ResourceGroupName -Name $VM.Name -ErrorAction SilentlyContinue -Force

#判断虚拟机是否关闭成功

if($StartRtn.StatusCode -ne 'OK'){

Write-Output ($VM.Name + " failed to stop")

}else{

Write-Output ($VM.Name + " has been stoped")

}

}

Azure申请使用地址:https://www.azure.cn/pricing/1rmb-trial-full/

Tags:

最近发表
标签列表