Shut down and start a virtual machine with Azure Automation (automation) Timing

Source: Internet
Author: User

1. Overview

As a user of Windows Azure, the most worrying thing about using Azure is that before the end of the month, the pre-set costs are running out (the two bill chart below is the one I hate to see). But by carefully analyzing your own expense list and discovering that the vast majority of costs are spent on virtual machines, and that Azure's virtual machines are billed by boot time, there is a compelling need to find a solution to save the overhead of virtual machines. The simplest solution is to automatically turn off the virtual machine when it is not needed, and it will take time for it to boot automatically. After Google has made a pass, it is discovered that this can be achieved through Azure's automation (Automation) capabilities. Here's what I've done on azure, by setting up Azure Automation to automatically start and shut down virtual machines at timed intervals.

2. Prerequisites

1. Subscription account for Windows Azure

2. There are virtual machines in azure that can start and shut down normally. You can refer to this link to create a virtual machine https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-windows-tutorial/

3. Create an Automation account Automation

As described by Azure, the Automation Automation account is a container for automation resources, and the Automation Automation account allows you to isolate automated resources from resources allocated to other automation accounts. If you have never created an Automation account in your Azure subscription, refer to the following section to create one:

4. Create and manage Certificates 4.1 Create a certificate in IIS

Running automated tasks (scripts) in an Azure subscription requires the use of certificate-based authentication. You can use a third-party business certificate, or you can create a certificate on any server that has Internet infomation Services (IIS) installed. The following describes how to create a self-signed certificate by using IIS in Windows Server 2012:

4.2 Export the. PFX Certificate from IIS

4.3 Export the. cer Certificate from IIS

4.4 Uploading the. CER certificate to the subscription account

5. Configure the Automation Script 5.1 Configure the asset to prepare the footage needed for the automation script to run

(Upload a. PFX certificate that was previously exported)

(The automation certificate name requires a friendly name for the certificate in IIS; The subscription ID can be queried in Azure settings)

5.2 Configuring Automation scripts

A runbook is a script that performs an automated operation. You can quickly create a script from the script library by using the new button in the lower left corner, or you can fully customize a script:

(After creating the script, select Script, go to edit page)

(Edit the script for Automation to run in authoring, the script is as follows, where highlighting is what needs to be modified according to the actual situation:)

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-automationcertif Icate-name $MyCert
    if ($Cert-eq $null)
    {
    & nbsp;   Write-output "Certificate entered: $MyCert does not exist in the automation service. Please create one ' n '   
   }
    Else
    {
& nbsp;       $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 was November 09:55:18 then day would be 21
$localTimeDay = ($locatTime). Day

# $locatTimeDayOfWeek
# $localTimeDay


#do not start VMs 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 isn't started, because today is not a working day."
}
}

}

(Save and click the "Test" button to run the script)

(If the script is correct, you will see a successful prompt in the Output window and see that the virtual machine has been started; Click the " Publish " button to publish the script)

The process of creating a shut down virtual machine script is exactly the same as above, and the contents of the script refer to the following table:

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-automationcertif Icate-name $MyCert
    if ($Cert-eq $null)
    {
    & nbsp;   Write-output "Certificate entered: $MyCert does not exist in the automation service. Please create one ' n '   
   }
    Else
    {
& nbsp;       $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 was November 09:55:18 then day would be 21
$localTimeDay = ($locatTime). Day

# $locatTimeDayOfWeek
# $localTimeDay


#do not start VMs 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 isn't started, because today is not a working day."
}
}
}

5.3 Configure the schedule to run the script at timed time

After the script has been debugged successfully, you can run the script periodically through schedule schedule to achieve the target of regular startup and shutdown.

(Note that the time here is 20-hour format, and you do not need to consider the time zone, the system will automatically follow your local time zone to do the conversion)

You can set the time to shut down the virtual machine by following the actions above.

6. Summary

Shut down and start a virtual machine with Azure Automation (automation) Timing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.