批量关停azure vm
When you have created a series of Azure VMs – for example for doing demos – you might want to be able to start it and shut it down easily. For convenience purposes but also for keeping the cost down while you’re not using the Azure VMs. In this case Azure Automation comes handy.
创建了一系列Azure VM(例如进行演示)后,您可能希望能够轻松启动和关闭它。 为了方便起见,同时也为了在不使用Azure VM时降低成本。 在这种情况下,Azure自动化非常方便。
Azure automation is a Microsoft Cloud Service for automating the maintenance, deployment, monitoring and creation of Azure Services. In Azure Automation, Windows PowerShell workflows (called runbooks) help you work smarter by allowing you to integrate with the services you depend on like Websites, Virtual Machines, SQL Server and other Azure Services. They can also be used with any other Public Internet APIs.
Azure自动化是一项Microsoft云服务,用于自动执行Azure服务的维护,部署,监视和创建。 在Azure自动化中,Windows PowerShell工作流(称为运行手册)通过允许您与您依赖的服务(如网站,虚拟机,SQL Server和其他Azure服务)集成来帮助您更智能地工作。 它们也可以与任何其他公共Internet API一起使用。
Azure Automation gives you dashboards and log records for easier monitoring, it also gives the opportunity to create checkpoints for restarting your workflows after unexpected errors, crashes or network issues making the setup even more reliable.
Azure自动化为您提供仪表板和日志记录,以便于监视,同时还提供了创建检查点的机会,以在出现意外错误,崩溃或网络问题后重新启动工作流,从而使设置更加可靠。
First of all you need an Azure subscription and add a certificate to it .
首先,您需要一个Azure订阅并向其中添加证书。
查找您的订阅证书 ( Finding your subscription certificates )
is done by clicking on Subscriptions ➜ Manage subscriptions
通过单击订阅➜管理订阅来完成
Or by navigating to :
https://manage.windowsazure.com/@<default_directory_name>#Workspaces/AdminTasks/SubscriptionMapping
或浏览至:
https://manage.windowsazure.com/@ <默认目录名称>#Workspaces / AdminTasks / SubscriptionMapping
You need to note the subscription id GUID : 7ffxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx
您需要注意订阅ID GUID:7ffxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx
Click on management certificates and import a .cer file from a real or a self signed certificate.
单击管理证书,然后从真实或自签名证书中导入.cer文件。
如何创建自签名证书 ( How to create a self signed certificate )
This is out of topic but anyway very useful so I include it here.
这是没有主题的,但是无论如何还是很有用的,所以我在这里包括它。
- Find a server running IIS 查找运行IIS的服务器
- Start Internet Information Services manager (inetmgr) 启动Internet信息服务管理器(inetmgr)
Click Server certificate
单击服务器证书
On the right pane chose Create self-signed certificate
在右侧窗格中,选择“ 创建自签名证书”
- AutomationCertificate and click AutomationCertificate ,然后单击“ OK 确定”。
- Now you need to create two files from this certificate : a .cer and a .pfx (with the private key) 现在,您需要根据此证书创建两个文件:.cer和.pfx(使用私钥)
- View 查看”。
On the Details pane choose Copy to File
在“ 详细信息”窗格上,选择“ 复制到文件”
Call it AutomationCertificate, keep all the default settings, and save it. You should now have a AutomationCertificate.cer file.
将其命名为AutomationCertificate,保留所有默认设置,然后保存。 您现在应该有一个AutomationCertificate.cer文件。
For making the .pfx file the repeat step 8 to 10 and then on the second step of the wizard choose to Export the Private key
要制作.pfx文件,请重复步骤8至10 ,然后在向导的第二步中选择“ 导出私钥”
Leave the Export file format settings as is :
保留“导出文件格式”设置为:
Pick a password (that you can remember) and save the .pfx file next to the .cer file. You should now have an AutomationCertificate.pfx file
选择一个您可以记住的密码,然后将.pfx文件保存在.cer文件旁边。 您现在应该具有一个AutomationCertificate.pfx文件
在Azure中创建自动化 ( Create an Automation in Azure )
Pick automation in the management portal, choose Automation and +Create in the bottom. Give it a meaningful name, choose a region and a subscription.
在管理门户中选择自动化,然后在底部选择“自动化”和“ +创建 ”。 给它起一个有意义的名称,选择一个区域和一个订阅。
You should now have a new Azure Automation and ready to add assets and runbooks.
现在,您应该拥有一个新的Azure自动化,并准备添加资产和Runbook。
为运行手册添加必要的资产 ( Adding the necessary Assets for the Runbooks )
In order for the runbooks to run you will need 3 elements. These are created as Assets ➜ Settings in the Automation
为了运行Runbook,您将需要3个元素。 这些在自动化中作为资产Asset设置创建
# | Type | Name |
1 | Credentials ➜ Certificate | AutomationCredential |
2 | Connection | BIAzure01 |
3 | Module | Azure |
# | 类型 | 名称 |
1个 | 证书➜证书 | 自动化证书 |
2 | 连接 | BIAzure01 |
3 | 模组 | 蔚蓝 |
When adding the Certificate, you will be prompted to add a .pfx certificate file. Choose the certificate you created earlier.
添加证书时,系统将提示您添加.pfx证书文件。 选择您之前创建的证书。
When creating the Connection you will need to add the certificate name and your subscription ID (GUID).
创建连接时,您将需要添加证书名称和订阅ID(GUID)。
- The Module is created automatically when creating the automation 创建自动化时会自动创建模块
Once this is done you’re ready to add a runbook…
完成此操作后,您就可以添加运行簿了……
添加Runbook ( Adding a runbook )
This is done by clicking on +New in the bottom left corner.
通过单击左下角的+ 新建来完成此操作。
In my case I want to create a runbook to start up and stop VMs so I called it ManageVMs
就我而言,我想创建一个启动簿来启动和停止VM,因此我将其称为ManageVMs
Once it is created click on it and you are ready to edit it by clicking on Author.
创建完成后,请单击它,并准备通过单击“作者”进行编辑。
The runbook I am using here is freely inspired by the blog post written by Peter Selch Dahl.
我在这里使用的Runbook受到Peter Selch Dahl撰写的博客文章的自由启发。
The file can be downloaded from here
可以从这里下载文件
workflow ManageVMs
{
param(
# Start or Stop - default = stop
[Parameter(Mandatory = $true)]
[string]$Action="Stop"
)
$MyConnection = "BIAzure01"
$MyCert = "AutomationCredential"
$MySubscriptionName = "<Azure Subscription Name>"
# Get the Azure Automation Connection
$Con = Get-AutomationConnection -Name $MyConnection
if ($Con -eq $null)
{
Write-Output "Connection entered: $MyConnection does not exist in the automation service. Please create one "
}
else
{
$SubscriptionID = $Con.SubscriptionID
$ManagementCertificate = $Con.AutomationCertificateName
Write-Output "--------------------------"
Write-Output "Connection Properties: "
Write-Output "SubscriptionID: $SubscriptionID"
Write-Output "Certificate setting name: $ManagementCertificate "
}
# Get Certificate & print out its properties
$Cert = Get-AutomationCertificate -Name $MyCert
if ($Cert -eq $null)
{
Write-Output "Certificate entered: $MyCert does not exist in the automation service. Please create one "
}
else
{
$Thumbprint = $Cert.Thumbprint
# Write-Output "Certificate Properties: "
# Write-Output "Thumbprint: $Thumbprint"
}
#Set and Select the Azure Subscription
Set-AzureSubscription -SubscriptionName $MySubscriptionName -Certificate $Cert -SubscriptionId $SubscriptionID
Select-AzureSubscription -SubscriptionName $MySubscriptionName
Write-Output "--------------------------"
#Virtual Machines
$vmS = @()
$vmS += 'vm1'
#$vmS += 'vm2'
#$vmS += 'vm3'
#Action
if ($Action.ToUpper() -eq 'START')
{
foreach ($vm in $vmS)
{
Get-AzureVM -ServiceName $vm -Name $vm | Start-AzureVM
Write-Output "Started $vm"
}
}
else
{
foreach ($vm in $vmS)
{
Get-AzureVM -ServiceName $vm -Name $vm | Stop-AzureVM -Force
Write-Output "Stopped $vm"
}
}
Write-Output "--------------------------"
}
The main point of interests in this script are :
该脚本的主要兴趣点是:
工作流程名称 ( Workflow name )
Needs to be the same than the runbook name. Here : ManageVMs
需要与运行手册名称相同。 此处: ManageVMs
输入参数 ( Input parameter )
Start or Stop (default)
启动或停止(默认)
变数 ( Variables )
$vmS : A String array to store all the Virtual Machines you need to start or stop. Here
$vmS += ‘VirtualMachine1’
$vmS += ‘VirtualMachine2’
$vmS += ‘VirtualMachine3’
$vmS += …
$ vmS :一个String数组,用于存储您需要启动或停止的所有虚拟机。 这里
$ vmS + ='VirtualMachine1'
$ vmS + ='VirtualMachine2'
$ vmS + ='VirtualMachine3'
$ vmS + =…
$MyConnection : The name of the connection you created in the assets.
$MyCert : The name of the management certificate from your subscription and the one you created as a credentials in the assets. Here : AutomationCredential
$MySubscriptionName : The name of your azure subscription
$ MyConnection :您在资产中创建的连接的名称。
$ MyCert :您的订阅中管理证书的名称,以及您在资产中作为凭据创建的证书。 此处:AutomationCredential
$ MySubscriptionName :您的天蓝色订阅的名称
Once you’ve pasted this powershell with the right values for the variables you should be able to test it by clicking on Test. Then a dialog windows should appear where you can specify what action you want to perform (Start or Stop)
一旦为Powershell粘贴了正确的变量值,您就可以通过单击Test对其进行测试。 然后将出现一个对话框窗口,您可以在其中指定要执行的操作(开始或停止)
After a few minutes :
几分钟后 :
You should then be able to schedule this runbook to run with your input parameters.
然后,您应该能够安排该运行手册与您的输入参数一起运行。
Happy automationing !!
快乐的自动化!
翻译自: https://www.sqlshack.com/creating-azure-automation-start-azure-vms/
批量关停azure vm