Wednesday, 21 December 2016

Automation PS Workflow to Start or Stop VMs Simultaneously

#PowerShell Script to Create Automation Account and Import RunBook!

Run Book which will start or stop VMs Simultaneously 


#Select-AzureRmProfile -Path 'C:\Users\gulab\Desktop\Text\Azure Scripts\Login\hotmail.txt'

Login-AzureRmAccount

$ResourceGroup = "Automation-SorStVMs"
$AutomationAcct = "cloudsupport"
$runbookName = "startORstop"
$scheduleName1 = "Start"
$scheduleName2 = "Stop"

New-AzureRmResourceGroup -Name $ResourceGroup -Location 'southeastasia'

New-AzureRmAutomationAccount -Name $AutomationAcct -ResourceGroupName $ResourceGroup -Location 'southeastasia'

New-AzureRmAutomationRunbook -Name startORstop -Type PowerShellWorkflow `
-Description "Start or Stop all VMs in a Subscription Parallely" `
-ResourceGroupName $ResourceGroup -AutomationAccountName $AutomationAcct


#Import-AzureRmAutomationRunbook -AutomationAccountName $AutomationAcct `
#-ResourceGroupName $ResourceGroup -Path 'C:\Users\gulab\Desktop\Text\Azure Scripts\startORstop.ps1' `
#-Type PowerShellWorkflow -Description "Parallel Start or Stop All VMs"


#Once you are done creating empty RunBook, copy the below attached .ps workflow script into empty RunBook and save the runbook.


#################################################################################


workflow startORstop
{
   Param
    (

        [Parameter(Mandatory=$true)]
        [String]
        $ResourceGroupName = '*',

        [Parameter(Mandatory=$true)]
        [Boolean]
        $Shutdown
    )
    $connectionName = "AzureRunAsConnection"
    try
    {
    
        # Get the connection "AzureRunAsConnection "
        $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         

        "Logging in to Azure..."
        Add-AzureRmAccount `
            -ServicePrincipal `
            -TenantId $servicePrincipalConnection.TenantId `
            -ApplicationId $servicePrincipalConnection.ApplicationId `
            -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
    }
    catch 
    {
        if (!$servicePrincipalConnection)
        {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
        } 
        else
        {
        Write-Error -Message $_.Exception
        throw $_.Exception
        }
    }

    If ($ResourceGroupName -eq '*')
    {
        $vms = Get-AzureRmVM
        Foreach -Parallel ( $vm in $vms ) 
        {
                        
            if ( $Shutdown ) 
            {
                Write-Output "Stopping $($vm.Name)";              
                Stop-AzureRmVm -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName -Force;
            }

            else 
            {
                Write-Output "Starting $($vm.Name)";                
                Start-AzureRmVm -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName;
            }
        }

    }
    
    Else
    {
        $vms = Get-AzureRmVM -ResourceGroupName $ResourceGroupName;   

        Foreach -Parallel ( $vm in $vms ) 
        {                        
            if ( $Shutdown ) 
            {
                Write-Output "Stopping $($vm.Name)";              
                Stop-AzureRmVm -Name $vm.Name -ResourceGroupName $ResourceGroupName -Force;
            }

            else 
            {
                Write-Output "Starting $($vm.Name)";                
                Start-AzureRmVm -Name $vm.Name -ResourceGroupName $ResourceGroupName;
            }
        }
    }

}

#################################################################################




Publish-AzureRmAutomationRunbook -ResourceGroupName $ResourceGroup -AutomationAccountName $AutomationAcct -Name "startORstop"


New-AzurermAutomationSchedule –AutomationAccountName $AutomationAcct `
–Name $scheduleName1 –StartTime "11/26/2016 08:45:00 AM" -TimeZone "UTC+05:30" `
–DayInterval 1 -ResourceGroupName $ResourceGroup -Description "Parallel Start all VMs"


New-AzurermAutomationSchedule –AutomationAccountName $AutomationAcct `
–Name $scheduleName2 –StartTime "11/26/2016 07:00:00 PM" -TimeZone "UTC+05:30" `
–DayInterval 1 -ResourceGroupName $ResourceGroup -Description "Parallel Stop all VMs"
To link a schedule to a runbook with the Azure Portal to Start VMs
1.      In the Azure portal, from your automation account, click the Runbooks tile to open the Runbooks blade.
2.      Click on the name of the runbook to schedule.
3.      Click on Schedule Tile
4.      If the runbook is not currently linked to a schedule, then you will be given the option to create a new schedule or link to an existing schedule (Start).
5.      Click on Add a Schedule à Schedule (Link a schedule to your runbook) à Select the Existing Schedule (Start)
6.      The runbook has parameters, you can select the option à Click Configure Parameters and Run Settings à RESOURCEGROUPNAME “Leave it” Blank à Shutdown (Select False), and OK & OK to Link your schedule with your runbook.



To link a schedule to a runbook with the Azure Portal to Stop VMs
7.      In the Azure portal, from your automation account, click the Runbooks tile to open the Runbooks blade.
8.      Click on the name of the runbook to schedule.
9.      Click on Schedule Tile
10.  If the runbook is not currently linked to a schedule, then you will be given the option to create a new schedule or link to an existing schedule (Stop).
11.  Click on Add a Schedule à Schedule (Link a schedule to your runbook) à Select the Existing Schedule (Stop)
12.  The runbook has parameters, you can select the option à Click Configure Parameters and Run Settings à RESOURCEGROUPNAME “Leave it” Blank à Shutdown (Select True), and OK & OK to Link your schedule with your runbook.




No comments:

Post a Comment

Get SSL Certificate Expiry Notification on Mail.

 There are multiple ways to get SSL Certification expiry details/alert. We all know if our SSL certificate get expire, how critical situatio...