Share via


Shutting down a Azure VM with Azure Automation

Recently I was asked how to shut down an Azure VM with Azure Automation, it can be a slightly daunting task, due to the fact that you have to use credentials whether or not its a certificate or PSCredentials.. So figured I would write up a post to show how to do this with PSCredentials.

Creating the account

In order to use PSCredentials instead of a certificate, we need an account to use. Lets start by creating an account.

Log into the Azure Management Portal and click on Settings.

image 

Take note of the Directory listed for your subscription, this is important because you could have multiple Azure Active Directories configured.

image

Next click on Active Directory on the side bar.

image

Click the Arrow next to the Name of the Azure Active Directory you saw in the Settings tab earlier.

image

Click on Users and then click Add User at the bottom of the page.

Follow the wizard by choosing “New user in your organization” as a type of user, give the user a username, click the right arrow

image

Now define the profile components for the account, fill out the appropriate information (First Name, Last Name, Display Name) for the Role choose User

image

Finally on the last page click Create

image

On the next page you will be presented with a temporary password for the user. At this point go to https://azure.microsoft.com and log into the Management Portal with the user created, you will be prompted to change the password.

image

Next go back to the Azure Management Portal with your administrator credentials and click Settings –> Administrators and click Add

image

Type in the email address of the account you created, choose the subscription they should be part of and click the Check box.  The Automation Account needs administrator rights to run.

image

Configuring Automation

Now we are ready to configure the automation, it is assumed that you have already created an Azure Automation account for the following steps. Click the Automation tab on the left of the Management Portal and click the arrow next to your Automation Account

image

Click Assets and click Add Setting at the bottom

image

For the Setting choose Add Credential

image

For the Credential Type choose Windows PowerShell Credential and give the credential a name, click the right arrow.

image

Now put in the username and password for the account we created earlier, and click the Checkbox

image

 

Now from the bottom left corner of the screen click the Plus sign , choose Automation, Runbook, and Quick Create.  Give the runbook a good name and description and click Create

image

Next click the Arrow next to the newly created runbook to go to the runbook details.

image

Click Author and now we are ready to create the runbook.

Paste the following code into the designer, where the Name for Get-AutomationPSCredential is the Name of your credentials you created earlier.  You can always click (Insert, Setting) at the bottom of the window and choose Get Windows PowerShell Credential and is will show you all the  credentials you have. This will also paste in the command to get the credentials.

 workflow shutdown-vm
{
    $Cred = Get-AutomationPSCredential -Name 'Azure Auto'
    Add-AzureAccount -Credential $Cred
    
InlineScript 
    {

    Select-AzureSubscription -SubscriptionName "Visual Studio Ultimate with MSDN"
           
    $VMS = Get-AzureVM 

    foreach($VM in $VMS)
        {    
        $VMName = $VM.Name 
        Stop-AzureVM -ServiceName $VM.ServiceName -Name $VM.Name -Force
        Write-Output "Shutting down VM :  $VMName "
        }
    }
}

Now it is time to test, if you have VMs just click the Test button

Finally once you test, you should get output similar to below.

image

Hope this helps someone out there.

Comments

  • Anonymous
    January 01, 2003
    Glad to hear Dirk.
  • Anonymous
    January 01, 2003
    Great Article George. This would certainly come in handy to one and all. Thanks for sharing.
  • Anonymous
    January 01, 2003
    Great post. Thank you.
  • Anonymous
    November 06, 2014
    This was exactly what I was looking for. Much better then trying to do this from within the VM itself. Thanks for the step by step!
  • Anonymous
    February 10, 2015
    Wonderful, saved probably hours of try and error. Thanks a lot!
  • Anonymous
    March 30, 2015
    http://www.google.com">goo
  • Anonymous
    March 30, 2015
    I receive many links saved in my notepad and found yours cause it is interesting.
  • Anonymous
    April 03, 2015
    Many of you use Free MSDN Azure account for Dev/Test. This post is all about using Free Azure account
  • Anonymous
    November 27, 2015
    Nice! very useful!
  • Anonymous
    December 15, 2015
    you mean to use azureauto@..... account not azureautotest@... since you do not have a azureautotest@.... account created
  • Anonymous
    December 15, 2015
    or do we need 2 accounts, one that is set as administrator (azureauto) to the subscriptions and another one for the automation(azureautotest) ? Can they be the same, I see when you define the credential actually you are actually creating a new account, thanks for clarifying it, or am i the only one confused ?
  • Anonymous
    December 15, 2015
    Bob, there are two things in play here.. one is the account in Azure Active Directory.. then there is the automation credential that references the account. To attempt to avoid confusion I named the automation account differently so it was known that they are two different things. The runbooks would reference the automation account of "azureautotest"
  • Anonymous
    January 12, 2016
    I'm having the following error log. Any idea?

    1/12/2016 9:49:43 AM, Error: Add-AzureAccount : Cannot bind argument to parameter 'Credential' because it is null.
    At shutdown-vm:4 char:4
    +
    + CategoryInfo : InvalidData: (:) [Add-AzureAccount], ParameterBindingValidationException
    + FullyQualifiedErrorId :
    ParameterArgumentValidationErrorNullNotAllowed,Microsoft.WindowsAzure.Commands.Profile.AddAzureAccount

    1/12/2016 9:49:44 AM, Error: Select-AzureSubscription : The subscription name Pay-As-You-Go doesn't exist.
    Parameter name: name
    At shutdown-vm:6 char:6
    +
    + CategoryInfo : CloseError: (:) [Select-AzureSubscription], ArgumentException
    + FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.Profile.SelectAzureSubscriptionCommand

    1/12/2016 9:49:51 AM, Error: Get-AzureVM : No default subscription has been designated. Use Select-AzureSubscription -Default to
    set the default subscription.
    At shutdown-vm:6 char:6
    +
    + CategoryInfo : CloseError: (:) [Get-AzureVM], ApplicationException
    + FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.GetAzureVMCommand
  • Anonymous
    January 12, 2016
    Solved: Wrong Credentials :)
  • Anonymous
    February 24, 2016
    The comment has been removed
  • Anonymous
    February 29, 2016
    To learn how such Azure VM automation can be monitored and systematized, check out two blogs below:
    VMs can be shutdown or scaled down on a schedule.
    Shut-down Azure VMs on a schedule: http://cloudmonix.com/blog/how-to-automate-schedule-shutdowns-of-azure-vms/
    Scale-down Azure VMs on a schedule: http://cloudmonix.com/blog/how-to-automate-scaling-of-azure-vms/
  • Anonymous
    April 18, 2016
    Wonderful, was thinking about this one for quite some time..