Wednesday, October 29, 2014

Powershell script for starting and stopping Azure VMs

At work we've got 2 subscriptions in which we've got a bunch of Azure VMs. At night, we try to shut these things down to save a bit of cost. I was getting a bit tired of going to the portal all the time, so I decided to play around with PowerShell a bit.

So I came up with this script that allows me to shut down the VMs without having to go to the portal. Please note: so there is a subscription for DEV/TEST and one for PROD. Furthermore, I'm not shutting down the domain controllers ( macine name ends with AD01 ).


$OTASubscription = "SomeSubscriptionName"
$PRODSubscription = "AnotherSubscriptionName" 

Function PromptForAzureLogin
{
        $title = "Log in to Azure first?"
        $message = "Do you want to log in to Azure and install a management certificate ?"
 
        $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Yes"
        $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "No"
        $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

        $result = $host.ui.PromptForChoice($title, $message, $options, 1)

        switch ($result)
            {
                0 {
                    # This will cause a login screen to appear which lets you login
                    Add-AzureAccount

                     # Displays a list of subscriptions
                    Get-AzureSubscription
                }
                1 {
                    # Do nothing
                }
            }
}

Function SelectSubscription
{
        $title = "Select subscription"
        $message = "Please select AzureSubscription"

        $ota = New-Object System.Management.Automation.Host.ChoiceDescription "&OTA", "OTA"
        $prod = New-Object System.Management.Automation.Host.ChoiceDescription "&PROD", "PRODUCTION
        $options = [System.Management.Automation.Host.ChoiceDescription[]]($ota, $prod)

        $result = $host.ui.PromptForChoice($title, $message, $options, 0)
        switch ($result)
            {
                0 {
                    Select-AzureSubscription $OTASubscription -Current

                    Write-Host "OTA Subscription selected" -foregroundcolor green
                }
                1 {
                    Select-AzureSubscription $PRODSubscription -Current
                    Write-Host "PRODUCTION Subscription selected" -foregroundcolor green
                }
            }
}

Function PromptForStartStop
{
    $title = "Start or Stop environment?"
    $message = "Do you want to start or stop the VMs? (All except -AD01)"

    $start = New-Object System.Management.Automation.Host.ChoiceDescription "&Start", "Start"
    $stop = New-Object System.Management.Automation.Host.ChoiceDescription "S&top", "Stop"
    $cancel = New-Object System.Management.Automation.Host.ChoiceDescription "&Cancel", "Do nothing"

    $options = [System.Management.Automation.Host.ChoiceDescription[]]($start, $stop, $cancel)

    $result = $host.ui.PromptForChoice($title, $message, $options, 0)

    $vmsToStop = Get-AzureVM | where {$_.Name -notlike "*AD01" }
    switch ($result)
        {
            0 {
                # Starting VMs
                for($i = 0; $i -le $vmsToStop.count -1; $i++) {
                    $tuple = $vmsToStop[$i]
                    Write-Host "Starting box named: " $tuple.Name -foregroundcolor green
                    Write-Progress -Activity "Starting VM" -status $tuple.Name -percentComplete ($i / $vmsToStop.count*100)

                    Start-AzureVM -ServiceName $tuple.ServiceName -Name $tuple.Name
                }
            }
            1 {
                for($i = 0; $i -le $vmsToStop.count -1; $i++) {
                    $tuple = $vmsToStop[$i]
                    Write-Host "Stopping box named: " $tuple.Name -foregroundcolor green
                    Write-Progress -Activity "Stopping VM" -status $tuple.Name -percentComplete ($i / $vmsToStop.count*100)
                   
                    Stop-AzureVM -ServiceName $tuple.ServiceName -Name $tuple.Name -StayProvisioned
                }
            }
            2 {
                # Do nothing
                Write-Host "Doing nothing ..." -foregroundcolor green
            }
        }
}

cls
PromptForAzureLogin
SelectSubscription
Write-Host "Here are the boxes in the current subscription:" -foregroundcolor green
Get-AzureVM
PromptForStartStop
Write-Host "Here's the situation now:" -foregroundcolor green
Get-AzureVM


1 comment: