Created
October 8, 2018 17:49
-
-
Save JPRuskin/eb99029fd99b9b79d33811e17f08f553 to your computer and use it in GitHub Desktop.
Starts all VMs in a resource group, starting VMs matching *DC1 first (by default, uses -StartOrder)
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
#function Start-AzureLab { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)] | |
[ArgumentCompleter({(Get-AzureRmResourceGroup).ResourceGroupName})] | |
[string[]]$ResourceGroupName, | |
[ArgumentCompleter({(Get-AzureRmSubscription).Name | % {"'$_'"}})] | |
[string]$SubscriptionName = (Get-AzureRmContext).Subscription.Name, | |
[string[]]$StartOrder = @('*DC1'), | |
[switch]$Wait, | |
[switch]$PassThru | |
) | |
begin { | |
$CurrentSubscription = (Get-AzureRmContext).Subscription.Name | |
if ($SubscriptionName -ne $CurrentSubscription) { | |
$SubscriptionChanged = Select-AzureRmSubscription $SubscriptionName | |
} | |
} | |
process { | |
foreach ($Group in $ResourceGroupName) { | |
$VMs = Get-AzureRmVm -ResourceGroupName $Group | |
if ($VMs.Count -eq 0) { | |
Write-Warning "No VMs found in '$($Group)'. Please check that '$($SubscriptionName)' is the correct subscription." | |
} | |
foreach ($Filter in $StartOrder) { | |
$null = $VMs | Where Name -like $Filter | Start-AzureRmVM | |
} | |
$Jobs = $VMs | Start-AzureRmVM -AsJob | |
if ($PSBoundParameters.ContainsKey('Wait')) { | |
$Jobs | Wait-Job | |
} elseif ($PSBoundParameters.ContainsKey('PassThru')) { | |
$Jobs | |
} | |
} | |
} | |
end { | |
if ($SubscriptionChanged) { | |
$null = Select-AzureRmSubscription $CurrentSubscription | |
} | |
} | |
#} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment