Last active
July 28, 2016 17:36
-
-
Save jeffpatton1971/06d2937d7e498612d80a784eb80676ef to your computer and use it in GitHub Desktop.
A modification of the AzureAutomation Start-AzureV2VM script (https://github.com/azureautomation/runbooks/blob/master/Utility/Start-AzureV2VM.ps1). This script takes PowerState as a parameter, and checks to see if the vm is in the properstate before attempting the operation. Added a Workflow version of the same script that should run faster, the…
This file contains hidden or 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
param | |
( | |
[Parameter(Mandatory=$false)] | |
[string]$AzureConnectionAssetName = 'AzureRunAsConnection', | |
[Parameter(Mandatory=$false)] | |
[string]$ResourceGroupName, | |
[Parameter(Mandatory=$true)] | |
[ValidateSet('PowerState/running','PowerState/deallocated')] | |
[string]$PowerState | |
) | |
try | |
{ | |
$ErrorActionPreference = 'Stop'; | |
$Error.Clear(); | |
$AutomationConnection = Get-AutomationConnection -Name $AzureConnectionAssetName; | |
$AzureAccount = Add-AzureRmAccount -ServicePrincipal -TenantId $AutomationConnection.TenantId -ApplicationId $AutomationConnection.ApplicationId -CertificateThumbprint $AutomationConnection.CertificateThumbprint; | |
} | |
catch | |
{ | |
if (!($AutomationConnection)) | |
{ | |
throw "Azure Automation Connection $($AzureConnectionAssetName) not found."; | |
} | |
else | |
{ | |
throw $Error.Exception; | |
} | |
} | |
if ($ResourceGroupName) | |
{ | |
$VMs = Get-AzureRmVM -ResourceGroupName $ResourceGroupName; | |
} | |
else | |
{ | |
$VMs = Get-AzureRmVM; | |
} | |
foreach ($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 | |
{ | |
switch ($PowerState) | |
{ | |
'PowerState/running' | |
{ | |
# | |
# This VM needs to be running | |
# | |
if (!(($VmStatus.Statuses |Select-Object -Property Code) -match $PowerState)) | |
{ | |
$Result = Start-AzureRmVM -Name $VM.Name -ResourceGroupName $VM.ResourceGroupName; | |
} | |
} | |
'PowerState/deallocated' | |
{ | |
# | |
# This VM needs to be stopped | |
# | |
if (!(($VmStatus.Statuses |Select-Object -Property Code) -match $PowerState)) | |
{ | |
$Result = Stop-AzureRmVM -ResourceGroupName $VM.ResourceGroupName -Name $VM.Name -Force; | |
} | |
} | |
} | |
} | |
if (!($Result.IsSuccessStatusCode)) | |
{ | |
Write-Output "$($VM.Name) PowerState ($($PowerState)) operation failed ($($Result.Error)) at $($Result.StartTime)."; | |
} | |
else | |
{ | |
Write-Output "$($VM.Name) PowerState ($($PowerState)) operation succeeded."; | |
} | |
} |
This file contains hidden or 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
workflow Update-PowerState | |
{ | |
param | |
( | |
[Parameter(Mandatory=$false)] | |
[string]$AzureConnectionAssetName = 'AzureRunAsConnection', | |
[Parameter(Mandatory=$true)] | |
[string]$ResourceGroupName, | |
[Parameter(Mandatory=$true)] | |
[ValidateSet('PowerState/running','PowerState/deallocated')] | |
[string]$PowerState | |
) | |
try | |
{ | |
$ErrorActionPreference = 'Stop'; | |
$AutomationConnection = Get-AutomationConnection -Name $AzureConnectionAssetName; | |
$AzureAccount = Add-AzureRmAccount -ServicePrincipal -TenantId $AutomationConnection.TenantId -ApplicationId $AutomationConnection.ApplicationId -CertificateThumbprint $AutomationConnection.CertificateThumbprint; | |
} | |
catch | |
{ | |
if (!($AutomationConnection)) | |
{ | |
throw "Azure Automation Connection $($AzureConnectionAssetName) not found."; | |
} | |
else | |
{ | |
throw $Error.Exception; | |
} | |
} | |
$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