Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A workflow to poweroff/on vm's in a resourcegroup
workflow Stop-AzureVM
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
[string[]]$VMList,
[Parameter(Mandatory=$true)]
[string]$ResourceGroupName,
[Parameter(Mandatory=$true)]
[ValidateSet('on','off')]
[string]$State
)
foreach -Parallel ($VMName in $VMList)
{
InlineScript
{
$Vm = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMName;
if ($State -eq 'on')
{
Start-AzureRmVM -Name $VM.Name -ResourceGroupName $VM.ResourceGroupName;
}
else
{
Stop-AzureRmVM -ResourceGroupName $VM.ResourceGroupName -Name $VM.Name -Force;
}
}
}
}
workflow Update-PowerState
{
param
(
[Parameter(Mandatory=$true)]
[string[]]$VMList,
[Parameter(Mandatory=$true)]
[string]$ResourceGroupName,
[Parameter(Mandatory=$true)]
[ValidateSet('PowerState/running','PowerState/deallocated')]
[string]$PowerState
)
foreach -Parallel ($VM in $VMList)
{
#
# Check PowerState
#
$VmStatus = Get-AzureRmVM -ResourceGroupname $ResourceGroupname -Name $VM -Status;
#
# Check if ProvisioningState -eq updating, if so check powerstate
# PowerState/deallocating vm is going down
# There is no PowerState on a vm that is off, as it's coming on there are the following states
# PowerState/stopped this is different from PowerState/deallocated (perhaps if the machine is powered off from the OS side)
# PowerState/starting vm is starting up
# PowerState/running vm is running
#
if (($VmStatus.Statuses |Select-Object -Property Code) -match 'ProvisioningState/updating')
{
#
# VM is updating
#
$Status = ($VmStatus.Statuses |Where-Object -Property Code -Like 'PowerState*' |Select-Object -ExpandProperty Code);
Write-Output "$($VM.Name) is updating current status is $($Status).";
}
else
{
if ($PowerState -eq 'PowerState/running')
{
#
# This VM needs to be running
#
if (!(($VmStatus.Statuses |Select-Object -Property Code) -match $PowerState))
{
Start-AzureRmVM -Name $VM.Name -ResourceGroupName $VM.ResourceGroupName;
}
}
elseif ($PowerState -eq 'PowerState/deallocated')
{
#
# This VM needs to be stopped
#
if (!(($VmStatus.Statuses |Select-Object -Property Code) -match $PowerState))
{
Stop-AzureRmVM -ResourceGroupName $VM.ResourceGroupName -Name $VM.Name -Force;
}
}
}
}
}
workflow Update-PowerState
{
param
(
[Parameter(Mandatory=$true)]
[string]$ResourceGroupName,
[Parameter(Mandatory=$true)]
[ValidateSet('PowerState/running','PowerState/deallocated')]
[string]$PowerState
)
$VMs = Get-AzureRmVM -ResourceGroupName $ResourceGroupName;
foreach -Parallel ($VM in $VMs)
{
#
# Check PowerState
#
$VmStatus = Get-AzureRmVM -ResourceGroupname $VM.ResourceGroupname -Name $VM.Name -Status;
#
# Check if ProvisioningState -eq updating, if so check powerstate
# PowerState/deallocating vm is going down
# There is no PowerState on a vm that is off, as it's coming on there are the following states
# PowerState/stopped this is different from PowerState/deallocated (perhaps if the machine is powered off from the OS side)
# PowerState/starting vm is starting up
# PowerState/running vm is running
#
if (($VmStatus.Statuses |Select-Object -Property Code) -match 'ProvisioningState/updating')
{
#
# VM is updating
#
$Status = ($VmStatus.Statuses |Where-Object -Property Code -Like 'PowerState*' |Select-Object -ExpandProperty Code);
Write-Output "$($VM.Name) is updating current status is $($Status).";
}
else
{
if ($PowerState -eq 'PowerState/running')
{
#
# This VM needs to be running
#
if (!(($VmStatus.Statuses |Select-Object -Property Code) -match $PowerState))
{
Start-AzureRmVM -Name $VM.Name -ResourceGroupName $VM.ResourceGroupName;
}
}
elseif ($PowerState -eq 'PowerState/deallocated')
{
#
# This VM needs to be stopped
#
if (!(($VmStatus.Statuses |Select-Object -Property Code) -match $PowerState))
{
Stop-AzureRmVM -ResourceGroupName $VM.ResourceGroupName -Name $VM.Name -Force;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.