Stop & Start Your VM’s on Azure with Powershell

This is a very simple script…. :)  Please use it as a framework for you to create something better to include input parameters if you want etc..

Script Below… just change the variables! :)

function stopvm($azurecloud,$vmname)
{
    if ($vmname -eq $null)
    {
        Get-AzureVM -ServiceName $azurecloud |Foreach-object {Stop-AzureVM -ServiceName $_.ServiceName -Name $_.Name -Force}
    }
    else
    {
        Stop-AzureVM -ServiceName $azurecloud -Name $vmname
    }
}
function startvm($azurecloud,$vmname)
{
    if ($vmname -eq $null)
    {
        Get-AzureVM -ServiceName $azurecloud |Foreach-object {Start-AzureVM -ServiceName $_.ServiceName -Name $_.Name -Force}
    }
    else
    {
        Start-AzureVM -ServiceName $azurecloud -Name $vmname
    }
}

################################################################
# Please Change These Variables to Suit Your Environment
#
$azuresettings = "C:\Azure\AzureseetingsFile"
$azurecloud = "CloudServiceName"
#
#
#
################################################################

write-host "Importing Azure Settings"
Import-AzurePublishSettingsFile $azuresettings

write-host "Choose the options to Start and Stop your Azure VMS"
write-host "1. Start All VMs"
write-host "2. Stop All VMs"
write-host "3. Start One VM"
write-host "4. Stop One VM"
$answer = read-host "Please Select Your Choice"

Switch($answer)
{
    1{ $vmname = $null;StartVM $azurecloud $vmname}
    2{ $vmname = $null;StopVM $azurecloud $vmname}
    3{ $vmname = read-host "Please Enter VM Name";StartVM $azurecloud $vmname}
    4{ $vmname = read-host "Please Enter VM Name";StopVM $azurecloud $vmname}
}

Comments

  • Anonymous
    December 20, 2014
    Hello John,

    This is script is really useful for my project.
    In the script what is $azurecloud = "CloudServiceName" ?
    If i am giving Vm name, able to start or stop single vm.
    I have multiple Vm's where i need to start and stop.
    I am using microsoft azure cloud.
    please help me out. i need asap.

    Thanks ,Hari.
  • Anonymous
    March 17, 2015
    thanks a lot for the easy and usefull script!