本文包含以下内容

前提条件

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

使用本地计划任务定时关闭azure虚拟机的更多相关文章

  1. 关闭Azure虚拟机

    Press the Windows key, and then in the Start page, located at the bottom-left, click the Power butto ...

  2. 使用Azure Automation(自动化)定时关闭和启动虚拟机

    1. 概述 作为Windows Azure的用户,使用Azure的过程中,最担心的事情就是还没到月底,预设的费用就快消耗完了(下面两张账单图是我最讨厌看到的).但是仔细分析自己的费用列表,发现绝大部分 ...

  3. Azure Site Recovery 通过一键式流程将虚拟机故障转移至 Azure虚拟机

    Ruturaj Dhekane 云 + Enterprise 项目经理  现在,AzureSite Recovery可以通过其"灾难恢复至 Azure"功能保护您的工作负荷,并 ...

  4. 在 Windows Azure 虚拟机中使用 Microsoft SQL Server 安全功能

    编辑人员注释:本文章由 SQL Server 团队高级项目经理 Sung Hsueh 撰写. SQL Server 的最新用法之一是利用 Microsoft 的 Windows Azure 基础结构服 ...

  5. Kubernetes 1.12公布:Kubelet TLS Bootstrap与Azure虚拟机规模集(VMSS)迎来通用版本号

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/M2l0ZgSsVc7r69eFdTj/article/details/82880341 https: ...

  6. windows 定时任务:schtasks,定时关闭网易云音乐

    大部分属于转载和粘贴. 使用命令:schtasks windows 定时任务   使用样例: 每天定时关闭网易云音乐: 每天22:20关闭网易云音乐: schtasks /create /tn &qu ...

  7. 在 Azure 虚拟机中配置 Always On 可用性组(经典)

    在开始之前,请先假设现在可以在 Azure Resource Manager 模型中完成此任务. 我们建议使用 Azure Resource Manager 模型来进行新的部署. 请参阅 Azure ...

  8. 利用Azure虚拟机安装Dynamics 365 Customer Engagement之六:安装后端服务器

    我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...

  9. 利用Azure虚拟机安装Dynamics 365 Customer Engagement之七:安装前端服务器及部署管理器

    我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...

随机推荐

  1. ES6,CommonJS 区别

    Javascript,javascript是一种脚本编程语言,有自己独立的语法与语义,没有javascript,也就没有其他的那些概念了. 关于ES6,可直接理解为javascript的增强版(增加了 ...

  2. 【算法笔记】B1005 继续(3n+1)猜想+sort()用法

    1005 继续(3n+1)猜想 (25 分) 卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程 ...

  3. 毕业设计 python opencv实现车牌识别 码云地址

    码云地址:https://gitee.com/yinghualuowu/Python_VLPR 删除了冗余代码,可以更加便于运行.其实是为了那些进不去github准备的~

  4. 113th LeetCode Weekly Contest Reveal Cards In Increasing Order

    In a deck of cards, every card has a unique integer.  You can order the deck in any order you want. ...

  5. POJ - 1080 枚举 / DP

    要求max{F/P},先枚举下界lowf,再贪心求符合约束条件的n个最小价值和 记录F的离散值和去重可以大幅度常数优化 (本来想着用DP做的) (辣鸡POJ连auto都Complie Error) # ...

  6. Phyton Flask框架学习记录。

    注意:在左侧菜单栏(在JQuery插件库下载的)右边是采用<iframe> 标签嵌入其他页面,此时标签的src应用用后台中的方法名称(本人测试用的是无参数的方法), 而页面跳转window ...

  7. nginx+uwsgi+virtualenv+supervisor部署项目

    一.导论 WSGI是Web服务器网关接口.它是一个规范,描述了Web服务器如何与Web应用程序通信,以及Web应用程序如何链接在一起以处理一个请求,(接收请求,处理请求,响应请求) 基于wsgi运行的 ...

  8. 【研究】Weblogic XMLDecoder反序列化漏洞(CVE-2017-10271)

    影响范围: Oracle WebLogic Server 10.3.6.0.0版本 Oracle WebLogic Server 12.1.3.0.0版本 Oracle WebLogic Server ...

  9. Promise个人笔记---【Promise的前世今生】

    Promise第一版本 案例是使用Node.js内置的fs模块[就是文件系统模块,负责读写文件.]来模拟异步操作 const fs = require('fs'); function getFileP ...

  10. python 学习笔记一——Python安装和IDLE使用

    好吧,一直准备学点啥,前些日子也下好了一些python电子书,但之后又没影了.年龄大了,就是不爱学习了.那就现在开始吧. 安装python 3 Mac OS X会预装python 2,Linux的大多 ...