I just had some discussion around the practices being followed to save cost using Azure VMs. One of the discussion was around, development and test servers which are used just on weekdays and they are idle during weekends. The practice which administrators follow is, they automate the snooze the machine for Saturday/Sunday and start of the machine before the work hours on Monday.
In this post, I am going to share the script which you can use to create this task for Azure VMs :
1. Download makecert.exe using this link
2. Using mmc , export the certificate in both .cer and .pfx format
Export the certificate –> right click on the certificate –> all tasks –> export (use both the options one at time and create .cer and .pfx file):
3. Create automation account:
4. Click on the automation account –> go to assets -> add setting -> create credential using .pfx file:
Add Certificate to management certificates section under settings:
5. Create variables for CertificateName , SubscriptionId , ServiceName , VMName and subscriptionName – add the correct value for each variable (Click on the automation account –> go to assets -> add setting –> Add variable –>String)
6. Create PowerShell runbooks using the scripts below:
Start_vm.ps1 (copy the below script into a notepad file and save it with ps1 extn)
workflow Start-VM
{
Param (
[parameter(Mandatory=$true)]
[String]
$VMName,
[parameter(Mandatory=$true)]
[String]
$ServiceName
)
$day = (Get-Date).DayOfWeek
if ($day -eq ‘Sunday’){
exit
}
$subscriptionName = Get-AutomationVariable -Name “SubscriptionName”
$subscriptionID = Get-AutomationVariable -Name “SubscriptionID”
$certificateName = Get-AutomationVariable -Name “CertificateName”
$certificate = Get-AutomationCertificate -Name $certificateName
Set-AzureSubscription -SubscriptionName $subscriptionName -SubscriptionId $subscriptionID -Certificate $certificate
Select-AzureSubscription $subscriptionName
Start-AzureVM -Name $VMName -ServiceName $ServiceName
}
Stop_vm.ps1 (copy the below script into a notepad file and save it with ps1 extn)
workflow Stop-VM
{
Param (
[parameter(Mandatory=$true)]
[String]
$VMName,
[parameter(Mandatory=$true)]
[String]
$ServiceName
)
$day = (Get-Date).DayOfWeek
if ($day -eq ‘Saturday’ -or $day -eq ‘Sunday’){
exit
}
$subscriptionName = Get-AutomationVariable -Name “SubscriptionName”
$subscriptionID = Get-AutomationVariable -Name “SubscriptionID”
$certificateName = Get-AutomationVariable -Name “CertificateName”
$certificate = Get-AutomationCertificate -Name $certificateName
Set-AzureSubscription -SubscriptionName $subscriptionName -SubscriptionId $subscriptionID -Certificate $certificate
Select-AzureSubscription $subscriptionName
Stop-AzureVM -Name $VMName -ServiceName $ServiceName -Force
}
7. Upload these workbooks under runbook section on the same page:
8. Screen will be like :
9. Publish the Runbooks – just click on the runbook name and then publish the script as follows:
just click on test before publishing it.
10. Once it’s published , click on schedule and then schedule it:
Guest Post:
Written by Gaurav Srivastava