1. 概述

作为Windows Azure的用户,使用Azure的过程中,最担心的事情就是还没到月底,预设的费用就快消耗完了(下面两张账单图是我最讨厌看到的)。但是仔细分析自己的费用列表,发现绝大部分费用消耗在虚拟机上,而Azure的虚拟机是按照开机时间来计费的,因此迫切需要找到一个方案来节省虚拟机的开销。最简单的方案就是在不需要的时候将虚拟机自动关闭,需要的时间让其自动开机。在Google了一通以后,发现可以通过Azure的自动化(Automation)功能达到上述目的。下面介绍我在Azure上的实践,通过设置Azure Automation,实现定时自动启动和关闭虚拟机。

 

2. 必要条件

1. Windows Azure的订阅账户

2. 在Azure中有可以正常启动和关闭的虚拟机。可以参考这个链接创建一个虚拟机https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-windows-tutorial/ 

3. 创建自动化账户 Automation

按照Azure的描述,Automation自动化账户是自动化资源的容器,使用Automation自动化账户可以将自动化的资源与分配给其他自动化账户的资源隔离。如果你从未在Azure订阅中创建过自动化账户,参考下面介绍创建一个:

4. 创建和管理证书

4.1 在IIS中创建证书

在Azure订阅中运行自动化的任务(脚本),需要使用基于证书的认证。你可以使用第三方的商业证书,也可以在任意一台安装了Internet Infomation Services (IIS)的服务器上创建一个证书。下面介绍如何在Windows Server 2012中使用IIS创建一个自签名的证书:

4.2 从IIS中导出.pfx证书

4.3 从IIS中导出.cer证书

4.4 将.cer证书上传到订阅账户

5. 配置自动化脚本

  5.1 配置资产,准备自动化脚本运行过程中需要的素材

(上传之前导出的.pfx证书)

(自动化证书名称需要使用证书在IIS中的友好名称;订阅ID可以在Azure的设置中查询到)

5.2 配置自动化脚本

Runbook是执行自动化操作的脚本。可以通过左下角的“新建”按钮从脚本库中快速创建一个脚本,也可以完全自定义一个脚本:

(创建好脚本后,选择脚本,进入编辑页面)

(在“创作”中编辑自动化运行的脚本,脚本如下,其中高亮部分是需要根据实际情况修改的内容:)

workflow Start-VM-danzhang-win7
{
    param()
    #connection      
       $MyConnection = "AzureConnection-1"
       $MyCert = "AutomationCredential-1"
 
    # 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 `n"  
    }
    else
    {
        $SubscriptionID = $Con.SubscriptionID
        $ManagementCertificate = $Con.AutomationCertificateName
      
    }  

    # 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 `n"  
    }
    else
    {
        $Thumbprint = $Cert.Thumbprint
    }

        #Set and Select the Azure Subscription
         Set-AzureSubscription `
            -SubscriptionName "My Azure Subscription" `
            -Certificate $Cert `
            -SubscriptionId $SubscriptionID `

        #Select Azure Subscription
         Select-AzureSubscription `
            -SubscriptionName "My Azure Subscription"

    Write-Output "-------------------------------------------------------------------------"

       Write-Output "Starting the VM.."

       # Please type the name of your Domain Controllers

   
    inlinescript{
  
# function to get local time (example Convert UTC tome to Indian Time Zone)
Function Get-LocalTime($UTCTime)
{
$strCurrentTimeZone = 'India Standard Time'
$TZ = [System.TimeZoneInfo]::FindSystemTimeZoneById($strCurrentTimeZone)
$LocalTime = [System.TimeZoneInfo]::ConvertTimeFromUtc($UTCTime, $TZ)
Return $LocalTime
}

#convert date time to UTC time Zone
$date = (Get-Date).ToUniversalTime()
# call function to get local time
$locatTime= Get-LocalTime($date)
#get day of week eg. Friday
$locatTimeDayOfWeek= ($locatTime).DayOfWeek
#get current day of the date eg. if current date is 21 November 2014 09:55:18 then day will be 21
$localTimeDay= ($locatTime).Day

#$locatTimeDayOfWeek
#$localTimeDay

#do not start VM on saturday and Sunday

if($locatTimeDayOfWeek -ne "Saturday" -and $locatTimeDayOfWeek -ne "Sunday")
{
#$sample = Get-AzureWinRMUri -ServiceName $Using:CloudServiceName -Name $Using:VMName
$StartOutPut = Start-AzureVM -ServiceName "danzhang-win7" -Name "danzhang-win7"
Write-Output $"Virtual Machine danzhang-win7 started."
Write-Output $StartOutPut

}
elseif($localTimeDay -le 7 -and $locatTimeDayOfWeek -eq "Saturday")
{
$StartOutPut = Start-AzureVM -ServiceName "danzhang-win7" -Name "danzhang-win7"

Write-Output $"Virtual Machine danzhang-win7 started."
Write-Output $StartOutPut
}
else{
Write-Output "Virtual Machine is not started, because today is not a working day."
}
}
      
}

(保存并点击“测试”按钮运行脚本)

(如果脚本正确,你会在输出窗口中看到成功的提示,同时看到虚拟机已经启动了;点击“发布”按钮发布脚本)

创建关闭虚拟机脚本的过程与上面完全一致,脚本的内容参考下表:

workflow Stop-VM-danzhang-win7
{
    param()
    #connection
       $MyConnection = "AzureConnection-1"
       $MyCert = "AutomationCredential-1"
      
    # 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 `n"  
    }
    else
    {
        $SubscriptionID = $Con.SubscriptionID
        $ManagementCertificate = $Con.AutomationCertificateName
      
    }  

    # 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 `n"  
    }
    else
    {
        $Thumbprint = $Cert.Thumbprint
    }

        #Set and Select the Azure Subscription
         Set-AzureSubscription `
            -SubscriptionName "My Azure Subscription" `
            -Certificate $Cert `
            -SubscriptionId $SubscriptionID `

        #Select Azure Subscription
         Select-AzureSubscription `
            -SubscriptionName "My Azure Subscription"

    Write-Output "-------------------------------------------------------------------------"

       Write-Output "Stoping the VM.."

       # Please type the name of your Domain Controllers

   
    inlinescript{
  
# function to get local time (example Convert UTC tome to Indian Time Zone)
Function Get-LocalTime($UTCTime)
{
$strCurrentTimeZone = 'India Standard Time'
$TZ = [System.TimeZoneInfo]::FindSystemTimeZoneById($strCurrentTimeZone)
$LocalTime = [System.TimeZoneInfo]::ConvertTimeFromUtc($UTCTime, $TZ)
Return $LocalTime
}

#convert date time to UTC time Zone
$date = (Get-Date).ToUniversalTime()
# call function to get local time
$locatTime= Get-LocalTime($date)
#get day of week eg. Friday
$locatTimeDayOfWeek= ($locatTime).DayOfWeek
#get current day of the date eg. if current date is 21 November 2014 09:55:18 then day will be 21
$localTimeDay= ($locatTime).Day

#$locatTimeDayOfWeek
#$localTimeDay

#do not start VM on saturday and Sunday

if($locatTimeDayOfWeek -ne "Saturday" -and $locatTimeDayOfWeek -ne "Sunday")
{

 

#$StopOutPut = Start-AzureVM -ServiceName "mkadamvm" -Name $Using:test

#$sample = Get-AzureWinRMUri -ServiceName $Using:CloudServiceName -Name $Using:VMName

$StopOutPut = Stop-AzureVM -ServiceName "danzhang-win7" -Name "danzhang-win7" -Force
Write-Output $"Virtual Machine danzhang-win7 Stopped."
Write-Output $StopOutPut

}
elseif($localTimeDay -le 7 -and $locatTimeDayOfWeek -eq "Saturday")
{
$StopOutPut = Stop-AzureVM -ServiceName "danzhang-win7" -Name "danzhang-win7" -Force

Write-Output $"Virtual Machine danzhang-win7 Stopped."
Write-Output $StartOutPut
}
else{
Write-Output "Virtual Machine is not started, because today is not a working day."
}
}
}

  5.3配置日程,实现定时运行脚本

脚本调试成功以后,就可以通过“计划日程”定期运行脚本,以实现定期启动和关机的目标。

(注意这里的时间是20小时格式的,并且你不需要考虑时区,系统会自动按照你本地的时区做转换)

可以按照上面的操作,设置关闭虚拟机的时间。

http://www.cnblogs.com/danzhang/  ALM MVP 张洪君

使用Azure Automation(自动化)定时关闭和启动虚拟机的更多相关文章

  1. 【Azure Developer】Azure Automation 自动化账号生成的时候怎么生成连接 与证书 (Connection & Certificate)

    Azure Automation :The Azure Automation service provides a highly reliable and scalable workflow exec ...

  2. 利用Azure Automation实现云端自动化运维(4)

    在上述基本准备工作做完后,wo们看看如何实现利用Azure Automation实现定时自动开关机的操作,这种场景非常适合Dev/Test环境,因为Azure的虚拟机是按照分钟收费的,所以我们可以在开 ...

  3. Azure Automation (1) 入门

    <Windows Azure Platform 系列文章目录> 通过Azure Automation(自动化),开发人员可以自动完成通常要在云环境中执行的手动.长时间进行.易出错且重复性高 ...

  4. Azure 上通过Automation 实现定时开关虚拟机

    更多内容,请关注公众号: Azure Automation 可以提供一些自动化的功能,比如我们可以指定在每天早上6点开启虚拟机,每天晚上8点关闭虚拟机.同时还提供一些基于监控参数的自动化配置.今天的主 ...

  5. 利用Azure Automation实现云端自动化运维(1)

    Azure Automation是Azure上的一个自动化工作流引擎,基于Powershell,来帮助用户简化,集成和自动化Azure上的运维工作,例如: 实现定时开关虚拟机,节约成本 实现定时创建删 ...

  6. 利用Azure Automation实现云端自动化运维(3)

    Azure automation的认证方式:证书   该种方式是推荐的进行Automation认证的方式,好处在于安全性高,过期时间由自己控制,不好的地方在于大家在Windows上要生成证书比较麻烦, ...

  7. 利用Azure Automation实现云端自动化运维(2)

      Azure automation的认证: 用户名和密码   在Azure的automation中使用Powershell可以管理当前订阅的资源,也可以管理不同订阅的资源,那么问题就来了,安全性如何 ...

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

    本文包含以下内容 前提条件 如何实现定时关闭虚拟机 前提条件 Controller 机器上必须安装 Azure PowerShell,并且要在 PowerShell 里登录一次 Azure, 请参见: ...

  9. Step by Step 用Azure Automation 来开虚机(ARM)

    使用Azure Automation来自动化处理各种重复的耗时的云管理任务从而帮助云运维人员提升效率,帮助降低运营成本. 具体相关的介绍以及怎样利用Azure Automation来完成定期开关虚拟机 ...

随机推荐

  1. sencha treestore 取消自动加载数据

    gridstore在设置了autoLoad=false后不会自动加载数据,但是treestore不行,后来发现删掉root里的expanded:true后就可以了.但是界面上树没有展开,需在store ...

  2. 使用栈Stack对整数数值的运算表达式字符串进行运算C#

    这里如果对于形如字符串“((6+((7+8)-9)*9+8/2)-3)/2”的运算表达式进行运算.接触过此类的同学知道这种存在着运算符优先级的表达式,不能直接从左到右进行运算,我们使用OperandS ...

  3. RSA密钥——JAVA与C#的区别和联系

    PS:好久没写博了,最近在考虑以后的事情,而且手上杂事也比较多,终于得空来写两篇.   首先感谢:http://www.codeproject.com/Articles/25487/Cryptogra ...

  4. sql server2008安装错误(无法处理异常)

    我在安装sql server2008时出现安装错误,无法处理的异常,问题如下: 解决方法:在地址栏输入C:\Users\Administrator\AppData\Local ,找到Microsoft ...

  5. 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件

    [源码下载] 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件 作者:webabcd 介绍重新想象 Windows 8 Store ...

  6. spring aop advice

    1.前置通知(BeforeAdvice): import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAd ...

  7. MySQL Cluster配置概述

    一.     MySQL Cluster概述 MySQL Cluster 是一种技术,该技术允许在无共享的系统中部署“内存中”数据库的 Cluster .通过无共享体系结构,系统能够使用廉价的硬件,而 ...

  8. 关于C#中Environment.OSVersion判断操作系统及Win10上的问题

    我们都知道在C#中可以通过Environment.OSVersion来判断当前操作系统,下面是操作系统和主次版本的对应关系: 操作系统 主版本.次版本 Windows 10 10.0* Windows ...

  9. ArcEngine尝试读取或写入受保护的内存

    先说一下我的开发环境: Win10 + ArcGIS10.0 + ArcEngine10.0 + Framework4.0 今天调用新的GP工具则出现"尝试读取或写入受保护的内存.这通常指示 ...

  10. ArcGIS 10 SP5中文版(ArcGIS10补丁5中文版)

    下载地址:百度网盘下载地址:http://pan.baidu.com/s/1o7qPGhk 来自:http://zhihu.esrichina.com.cn/?/sort_type-new__day- ...