使用本地计划任务定时关闭azure虚拟机

本文包含以下内容

前提条件

Controller 机器上必须安装 Azure PowerShell,并且要在 PowerShell 里登录一次 Azure, 请参见:如何安装和配置 Azure PowserShell

如何实现定时关闭虚拟机

配好 Azure PowerShell 以后,就可以用下面这个脚本创建定时关机任务。把下面的代码另存为一个 PowerShell 脚本保存到本地磁盘,比如叫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 Azure Powershell is avaiable
if ((Get-Module -ListAvailable Azure) -eq $null)
{
  throw "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 

还是刚才那个文件,右键用 PowerShell 运行,按 PowerShell 提示输入四个必要的参数:

  • VMName, 要关闭的 Virtual Machine 名称
  • ServiceName, 关联的服务名称
  • TaskName, 任务名称
  • At,具体操作时间

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

到指定时间 powershell 开始执行关闭虚拟机的操作。

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

                     立即访问http://market.azure.cn

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值