Azure Automation step-by-step guide for Auto-Shutdown Virtual Machine.


-     Create an Azure account

 

-     Go “setting” and assign the user as the Co-administrator

-     Select automation and create an Automation Account, in this example “gwauto"

-     Select the created Automation Account

-     Create a new runbook, in the example, “shutdown-vm”

-     Click runbook “shutdown-vm” to create/edit a script

-     Click Author -> insert script -> Test -> Publish

 

===================== Script example of shutdown-vm=====================

workflow shutdown-vm

{

$username = "azureauto@mfa01.onmicrosoft.com"

$pass = "      "

$password=$pass|ConvertTo-SecureString -AsPlainText -Force

$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username,$password

Add-AzureAccount -Credential $mycred

 

$AzureSubscriptions = Get-AzureSubscription

foreach ($subscription in $AzureSubscriptions)

{

    Select-AzureSubscription -SubscriptionName $subscription.SubscriptionName

    #Write-Host $subscription.SubscriptionName

 

    foreach ($vm in Get-AzureVM)

    {

        $name = $vm.Name

        $servicename = $vm.ServiceName

   

        If($vm.Status -ne 'StoppedDeallocated')

        {

            # Add the VM's which should not be shutdown

            Stop-AzureVM -Service $servicename -name $name -Force

        }

    }

}

}

-     Schedule the runbook


Comments (0)

Skip to main content