What is Azure Automation? Released to General Availability in October 2014. This feature allows you to automate many common aspects of using the Azure platform, integrating with other features such as web sites, cloud services and virtual machines.
Let's see how we can leverage Azure Automation to ensure usage costs are minimized, by only running Virtual Machines when they are going to be needed (Set to normal office hours 9 AM to 6 PM). This post shows how to schedule starting and stopping of virtual machines in Azure.
Step 1: Let's start off by accessing our Azure Automation section. Create an Automation account.
Step 2: Create Secure Assets for our new Automation Account.
What are Secure Assets? These include credentials, certificates, connections, and encrypted variables. These assets are encrypted and stored in the Azure Automation using a unique key that is generated for each automation account.
Let's firstly create credential named "MyAzureCredential", this helps us holding our Azure Credential and used as a dynamic variable utilized for our PowerShell Runbooks. See how it is used in Step 5.
Step 3: Create a variable named "SubscriptionName", this will helps us holding our Azure Subscription Name and used as a dynamic variable utilized for our PowerShell Runbooks. See how it is used in Step 5.
Step 4: Create our Runbook and provided the runbook name as "StartVM"
Step 5: Writing the Runbook PowerShell command. Select "Author" click on "Draft" input the PowerShell command.
Add the below PowerShell Script to the Draft section
workflow StartVM {
#Input Parameters
Param (
[parameter(Mandatory=$true)] [String] $VMName,
[parameter(Mandatory=$true)] [String] $ServiceName
)
#Check for Weekend and exit the workflow in case of weekend
$day = (Get-Date).DayOfWeek
if ($day -eq 'Saturday' -or $day -eq 'Sunday'){ exit }
#Fetch My Azure Credential stored in Assets of our Automation Account
$credential = Get-AutomationPSCredential –Name "MyAzureCredential"
#Add the Azure Account
Add-AzureAccount -Credential $credential
InlineScript {
#Fetch Subscription Name stored in Assets of our Automation Account
$subscriptionName = Get-AutomationVariable -Name "SubscriptionName"
#Select the Azure Subscription
Select-AzureSubscription -SubscriptionName $subscriptionName
}
#Start the Azure Machine
Start-AzureVM -Name $VMName -ServiceName $ServiceName
}
Param (
[parameter(Mandatory=$true)] [String] $VMName,
[parameter(Mandatory=$true)] [String] $ServiceName
)
#Check for Weekend and exit the workflow in case of weekend
$day = (Get-Date).DayOfWeek
if ($day -eq 'Saturday' -or $day -eq 'Sunday'){ exit }
#Fetch My Azure Credential stored in Assets of our Automation Account
$credential = Get-AutomationPSCredential –Name "MyAzureCredential"
#Add the Azure Account
Add-AzureAccount -Credential $credential
InlineScript {
#Fetch Subscription Name stored in Assets of our Automation Account
$subscriptionName = Get-AutomationVariable -Name "SubscriptionName"
#Select the Azure Subscription
Select-AzureSubscription -SubscriptionName $subscriptionName
}
#Start the Azure Machine
Start-AzureVM -Name $VMName -ServiceName $ServiceName
}
Click on Test, this should the Strat Up the Virtual machine specified as the "VMName" & "ServiceName".
Once the PowerShell Script is working as expected, click on Publish.
Once the PowerShell Script is working as expected, click on Publish.
Step 6: For the Runbook "StartVM" select "Schedule" à "Link" à "Link to a New Schedule".
We would set the Type (One Time / Hourly / Daily), & Start Time as required.
We would set the Type (One Time / Hourly / Daily), & Start Time as required.
Now consider the time I specified is 9:00 AM, when the scheduled time arrives my specified Virtual Machine would Start Up.
For stopping the Virtual machine at a scheduled time of 6:00 PM, I would have to repeat the above procedure from Step 4 to Step 6.
The PowerShell Script would be modified as mentioned below:
workflow StopVM {
#Input Parameters
Param (
[parameter(Mandatory=$true)] [String] $VMName,
[parameter(Mandatory=$true)] [String] $ServiceName
)
#Check for Weekend and exit the workflow in case of weekend
$day = (Get-Date).DayOfWeek
if ($day -eq 'Saturday' -or $day -eq 'Sunday'){ exit }
#Fetch My Azure Credential stored in Assets of our Automation Account
$credential = Get-AutomationPSCredential –Name "MyAzureCredential"
#Add the Azure Account
Add-AzureAccount -Credential $credential
InlineScript {
#Fetch Subscription Name stored in Assets of our Automation Account
$subscriptionName = Get-AutomationVariable -Name "SubscriptionName"
#Select the Azure Subscription
Select-AzureSubscription -SubscriptionName $subscriptionName
}
#Stop the Azure Machine
Stop-AzureVM -Name $VMName -ServiceName $ServiceName
}
#Input Parameters
Param (
[parameter(Mandatory=$true)] [String] $VMName,
[parameter(Mandatory=$true)] [String] $ServiceName
)
#Check for Weekend and exit the workflow in case of weekend
$day = (Get-Date).DayOfWeek
if ($day -eq 'Saturday' -or $day -eq 'Sunday'){ exit }
#Fetch My Azure Credential stored in Assets of our Automation Account
$credential = Get-AutomationPSCredential –Name "MyAzureCredential"
#Add the Azure Account
Add-AzureAccount -Credential $credential
InlineScript {
#Fetch Subscription Name stored in Assets of our Automation Account
$subscriptionName = Get-AutomationVariable -Name "SubscriptionName"
#Select the Azure Subscription
Select-AzureSubscription -SubscriptionName $subscriptionName
}
#Stop the Azure Machine
Stop-AzureVM -Name $VMName -ServiceName $ServiceName
}
Comments
Post a Comment