zoukankan      html  css  js  c++  java
  • 使用本地计划任务定时关闭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

  • 相关阅读:
    【PBR的基本配置】
    【super vlan的配置】
    Day_03-函数和模块的使用
    Day_02-Python的循环结构
    Day_02-Python的分支结构和循环结构
    Day01_课后练习题
    Day01_初识Python
    一、Linux知识体系结构图
    NAND Flash结构及驱动函数
    区分大端和小端
  • 原文地址:https://www.cnblogs.com/zangdalei/p/7516159.html
Copyright © 2011-2022 走看看