Skip to content

Instantly share code, notes, and snippets.

@PatrickTerlisten
Last active December 5, 2015 08:26
Show Gist options
  • Save PatrickTerlisten/123b20f0b16a9d7b00d8 to your computer and use it in GitHub Desktop.
Save PatrickTerlisten/123b20f0b16a9d7b00d8 to your computer and use it in GitHub Desktop.
#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
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment