Starting and stopping Azure VMs with Azure PowerShell
To be honest: I’m lazy and I have a wife and two kids. Therefore I have to minimize the costs of my lab. I have a physical lab at the office and some VMs running on Microsoft Azure. Azure is nice, because I only have to pay what I really use. And because I’m only paying the actual use, I start the VMs only when I need them. Inspired by this very handy Azure VM wakeup & shutdown script, I decided to write my own script (yes, I invented a wheel again…). Very simple, nothing fancy. Feel free to use and modify the script according to your needs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#requires -Version 3 -Modules Azure | |
Function Start-AllAzureVMs { | |
<# | |
.SYNOPSIS | |
Simply run Start-AllAzureVMs without any parameter. | |
.DESCRIPTION | |
This script starts all Microsoft Azure VMs that are in the "StoppedDeallocated" state. | |
.EXAMPLE | |
Start-AllAzureVMs | |
Start all Azure VMs that are in the "StoppedDeallocated" state. | |
.NOTES | |
Author: Patrick Terlisten, patrick@blazilla.de, Twitter @PTerlisten | |
This script doesn't include credentials for your Microsoft Azure account. | |
Please make sure that you've added your Azure account. | |
This script is provided "AS IS" with no warranty expressed or implied. Run at your own risk. | |
This work is licensed under a Creative Commons Attribution NonCommercial ShareAlike 4.0 | |
International License (https://creativecommons.org/licenses/by-nc-sa/4.0/). | |
.LINK | |
http://www.vcloudnine.de | |
#> | |
Process | |
{ | |
try | |
{ | |
# Get all stopped VMs | |
$AllStoppedVMs = Get-AzureVM | Where-Object -FilterScript {$_.Status -eq 'StoppedDeallocated'} | Select-Object -Property Name, Status, ServiceName | |
# Give a list of all VMs that will be started | |
Write-Host -Object "`n" | |
Write-Host -Object "Starting the following VMs`n" -ForegroundColor DarkGreen | |
$AllStoppedVMs | |
# Start the VMs | |
$AllStoppedVMs | Start-AzureVM | |
Write-Host -Object "`n" | |
Write-Host -Object "Finished starting the following VMs`n" -ForegroundColor DarkGreen | |
} | |
catch | |
{ | |
"Error was $_" | |
$line = $_.InvocationInfo.ScriptLineNumber | |
"Error was in Line $line" | |
} | |
finally | |
{ | |
# Give a list after all VMs has been started | |
Get-AzureVM | Where-Object -FilterScript {$_.Status -ne 'StoppedDeallocated'} | Select-Object -Property Name, Status, ServiceName -Wait | |
} | |
} | |
} | |
Function Stop-AllAzureVMs { | |
<# | |
.SYNOPSIS | |
Simply run Stop-AllAzureVMs without any parameter. | |
.DESCRIPTIOn | |
This script stops all Microsoft Azure VMs that are in the "ReadyRole" state. | |
.EXAMPLE | |
Stop-AllAzureVMs | |
Stop al Azure VMs that are in the "ReadyRole" state. | |
.NOTES | |
Author: Patrick Terlisten, patrick@blazilla.de, Twitter @PTerlisten | |
This script doesn't include credentials for your Microsoft Azure account. | |
Please make sure that you've added your Azure account. | |
In case of stopping the VMs, the VM will enter the state "StoppedDeallocated". | |
This means that the VM will loose its IP reservation, but it isn't billed until | |
it's started again. | |
.LINK | |
http://www.vcloudnine.de | |
#> | |
Process | |
{ | |
try | |
{ | |
# Get all running VMs | |
$AllRunningVMs = Get-AzureVM | Where-Object -FilterScript {$_.Status -eq 'ReadyRole'} | Select-Object -Property Name, Status, ServiceName | |
# Give a list of all VMs that will be stopped | |
Write-Host -Object "`n" | |
Write-Host -Object "Stopping the following VMs`n" -ForegroundColor DarkGreen | |
$AllRunningVMs | |
# Stop all running VMs | |
$AllRunningVMs | Stop-AzureVM -Force | |
Write-Host -Object "`n" | |
Write-Host -Object "Finished stopping the following VMs`n" -ForegroundColor DarkGreen | |
# Give a list after all VMs has been stopped | |
Get-AzureVM | Where-Object -FilterScript {$_.Status -eq 'StoppedDeallocated'} | Select-Object -Property Name, Status, ServiceName -Wait | |
} | |
catch | |
{ | |
"Error was $_" | |
$line = $_.InvocationInfo.ScriptLineNumber | |
"Error was in Line $line" | |
} | |
finally | |
{ | |
# Give a list after all VMs has been started | |
Get-AzureVM | Where-Object -FilterScript {$_.Status -ne 'StoppedDeallocated'} | Select-Object -Property Name, Status, ServiceName -Wait | |
} | |
} | |
} |