Azure automation is a cool feature that can use to automate the creation, monitoring, deployment and management of cloud resources in your Microsoft Azure subscription using a highly-available workflow execution engine.
It’s a really cool feature, but when it’s first released, all the PowerShell run books have to use the certificate based authentication. And there should be a separate run book required to publish to access this certificate based authentication. But with new updated azure automation, its support PowerShell credential based authentication, which will enable to write PowerShell run books much easier.
This article describe how to automate the VM start and stop (deallocation) using PowerShell run books by credential level authentication.
Steps
- Create an Azure Administrator account in the Azure active directory to run Azure automation run books.
- Create a Azure Active Directory User
- Make this user as a Co-Administrator
- Create Automation Account
- Define credential for azure automation account
- Create VM start Run Book.
- Click the run book tab
- Click new (plus sign)
- Automation – runbook – quick create, give the runbook name as VM start.
- Click edit runbook or click on the Run book – Autor – Draft
- Type the following powershell script
workflow Get-VMStart
{
$Cred = Get-AutomationPSCredential –Name automationuser@xxx.com
Add-AzureAccount -Credential $Cred
Select-AzureSubscription -SubscriptionName “Visual Studio Premium with MSDN”
Start-AzureVM -ServiceName “VM01” -Name “VM01” -force
}
NOTE-
- In automation ps credential name, type the account name that defined earlier.
- Subscription name, you can find out from the portal – settings – subscription
- AzureVM – Service name, is the cloud service name for the VM
- AzureVM – name , is the VM name.
- Create a VM Stop runbook
- Use the same steps with the following script (use a new runbook)
workflow Get-VMStop{
$Cred = Get-AutomationPSCredential –Name automationuser@xxx.com
Add-AzureAccount -Credential $Cred
Select-AzureSubscription -SubscriptionName “Visual Studio Premium with MSDN”
Stop-AzureVM -ServiceName “VM01” -Name “VM01”
}
- Use the same steps with the following script (use a new runbook)
- Click Test to run the Runbook.
you can schedule this runbooks to run any preferred time
Hope this is helpful
cheers