作为一个屌丝,用Azure的时候得处处想着省钱,在你不用虚拟机的时候关机是个省钱的好办法。当然,每天手动登录Portal去开关机比较麻烦。如果你用VM的时间段比较固定,可以做个定时开关机的任务帮你省钱。

做法是用一台机器作为Controller,在上面建立开关机任务,调度Azure上的VM。比如你自己的机器每天早上8点开机,下午5点关机。你希望Azure上的某台VM早上8:30开机,下午4:30关机。那你就可以把自己的机器作为Controller。当然,你也可以在Azure上开一台专门作为Controller的Server,给它分配一个768MB内存的屌丝instance(diaostance)来省钱。

这台Controller机器上必须安装Microsoft Azure PowerShell,并且要在PowerShell里登录一次Azure。本文不再叙述,具体做法请看我之前写的《图解:使用Windows Azure PowerShell启动和关闭虚拟机 》。

定时开机


在你配好Azure PowerShell以后,就可以用下面这个脚本创建定时开机任务:

脚本是大微软网站上找到的,但是我做过修改。原版是工作日定时做,我修改成每天都做了。

把下面的代码另存为一个PowerShell脚本,比如叫“Start-AzureVMsOnSchedule.ps1”

<#
.SYNOPSIS
    Creates scheduled tasks to start Virtual Machines.
.DESCRIPTION
    Creates scheduled tasks to start a single Virtual Machine or a set of Virtual Machines (using
    wildcard pattern syntax for the Virtual Machine name).
.EXAMPLE
    Start-AzureVMsOnSchedule.ps1 -ServiceName "MyServiceName" -VMName "testmachine1" `
        -TaskName "Start Test Machine 1" -At 8AM
    
    Start-AzureVMsOnSchedule.ps1 -ServiceName "MyServiceName" -VMName "test*" `
        -TaskName "Start All Test Machines" -At 8:15AM
#>


param(
    # The name of the VM(s) to start on schedule.  Can be wildcard pattern.
    [Parameter(Mandatory = $true)] 
    [string]$VMName,


    # The service name that $VMName belongs to.
    [Parameter(Mandatory = $true)] 
    [string]$ServiceName,


    # The name of the scheduled task.
    [Parameter(Mandatory = $true)] 
    [string]$TaskName,


    # The name of the "Stop" scheduled tasks.
    [Parameter(Mandatory = $true)] 
    [DateTime]$At)


# The script has been tested on Powershell 3.0
Set-StrictMode -Version 3


# Following modifies the Write-Verbose behavior to turn the messages on globally for this session
$VerbosePreference = "Continue"


# Check if Windows Azure Powershell is avaiable
if ((Get-Module -ListAvailable Azure) -eq $null)
{
    throw "Windows Azure Powershell not found! Please install from http://www.windowsazure.com/en-us/downloads/#cmd-line-tools"
}


# Define a scheduled task to start the VM(s) on a schedule.
$startAzureVM = "Start-AzureVM -Name " + $VMName + " -ServiceName " + $ServiceName + " -Verbose"
$startTaskTrigger = New-ScheduledTaskTrigger -Daily -At $At
$startTaskAction = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument $startAzureVM
$startTaskSettingsSet = New-ScheduledTaskSettingsSet  -AllowStartIfOnBatteries 

$startScheduledTask = New-ScheduledTask -Action $startTaskAction -Trigger $startTaskTrigger -Settings $startTaskSettingsSet


# Register the scheduled tasks to start and stop the VM(s).
Register-ScheduledTask -TaskName $TaskName -InputObject $startScheduledTask

然后右键,选择“Run With PowerShell”

在弹出的PowerShell对话框里依次输入虚拟机名称(VMName)、云服务名称(ServiceName)、计划任务的名称(TaskName)和启动时间(At)。计划任务的名称和Azure无关,可以自定义。至于怎么找VMName和ServiceName,还是得看我之前写的博客。这里不多重复。

窗口自动关闭后你的定时开机任务就创建好了,你应该能在系统的计划任务中看到:

然后你可以当场试验一下:右键Run这个Task

你会看到PS窗口弹出,开始启动你的VM了:

如果你不信可以到Azure Portal里看看,VM正在启动:

定时关机


和刚才类似,把下面这段脚本保存为“Stop-AzureVMsOnSchedule.ps1”。这个也是在大微软的网站上找到的,但是原版关机后计费,我改了一下,关机后不会计费。

<#
.Synopsis
    Creates scheduled tasks to stop Virtual Machines.
.DESCRIPTION
    Creates scheduled tasks to stop a single Virtual Machine or a set of Virtual Machines (using
    wildcard pattern syntax for the Virtual Machine name).
.EXAMPLE
    Stop-AzureVMsOnSchedule.ps1 -ServiceName "MyServiceName" -VMName "testmachine1" -TaskName "Stopt Test Machine 1" -At 5:30PM
    Stop-AzureVMsOnSchedule.ps1 -ServiceName "MyServiceName" -VMName "test*" -TaskName "Stop All Test Machines" -At 5:30PM
#>


param(
    # The name of the VM(s) to start on schedule.  Can be wildcard pattern.
    [Parameter(Mandatory = $true)] 
    [string]$VMName,


    # The service name that $VMName belongs to.
    [Parameter(Mandatory = $true)] 
    [string]$ServiceName,


    # The name of the scheduled task.
    [Parameter(Mandatory = $true)] 
    [string]$TaskName,


    # The name of the "Stop" scheduled tasks.
    [Parameter(Mandatory = $true)] 
    [DateTime]$At
)


# The script has been tested on Powershell 3.0
Set-StrictMode -Version 3


# Following modifies the Write-Verbose behavior to turn the messages on globally for this session
$VerbosePreference = "Continue"


# Check if Windows Azure Powershell is avaiable
if ((Get-Module -ListAvailable Azure) -eq $null)
{
    throw "Windows Azure Powershell not found! Please install from http://www.windowsazure.com/en-us/downloads/#cmd-line-tools"
}


# Define a scheduled task to stop the VM(s) on a schedule.
$stopAzureVM = "Stop-AzureVM -Name " + $VMName + " -ServiceName " + $ServiceName + " -Force -Verbose"
$stopTaskTrigger = New-ScheduledTaskTrigger -Daily -At $At
$stopTaskAction = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument $stopAzureVM
$startTaskSettingsSet = New-ScheduledTaskSettingsSet  -AllowStartIfOnBatteries 

$stopScheduledTask = New-ScheduledTask -Action $stopTaskAction -Trigger $stopTaskTrigger -Settings $startTaskSettingsSet


# Register the scheduled tasks to start and stop the VM(s).
Register-ScheduledTask -TaskName $TaskName -InputObject $stopScheduledTask

还是和刚才一样,右键用PS运行,并输入必要的参数:

然后你的计划任务列表里也能刷出这个定时关机的任务了,可以测一下:

到Portal里验证一下,已经成功关机了: