diff options
Diffstat (limited to 'AzureHelpers/Public/Start-AzVm.ps1')
-rw-r--r-- | AzureHelpers/Public/Start-AzVm.ps1 | 44 |
1 files changed, 30 insertions, 14 deletions
diff --git a/AzureHelpers/Public/Start-AzVm.ps1 b/AzureHelpers/Public/Start-AzVm.ps1 index 408fb0c..49dd55a 100644 --- a/AzureHelpers/Public/Start-AzVm.ps1 +++ b/AzureHelpers/Public/Start-AzVm.ps1 @@ -6,14 +6,14 @@ function Start-AzVm { .DESCRIPTION If we can unambiguously determine a single VM name inside the subscription - we are logged into through the input string, we will stop and deallocate + we are logged into through the input string, we will start the machine. This function was built as we want to have terse input and a counterpart to Stop-AzVm. .INPUTS - String. The name or unambiguous part of the name of the VM we intend to + String. The names or unambiguous parts of the names of the VMs we intend to start. .OUTPUTS @@ -23,22 +23,38 @@ function Start-AzVm { [Parameter( Mandatory=$true, ValueFromPipelineByPropertyName=$true, - HelpMessage="String that is a VM name or is part of one unambiguous VM", + HelpMessage="Strings that are VM names or are part of single unambiguous VMs", Position=0 ) ] [ValidateLength(1,64)] - [string] - $VmName + [string[]] + $VmName ) $ErrorActionPreference = 'Stop' - $myvm = azvmidentify -VmName $VmName - # Since az vm start is taking quite its time even when the machine is started, we should check ourselves whether the machine is running: - if ( (az vm show -d -g $myvm.resourceGroup -n $myvm.name -o json | COnvertFrom-Json).powerState.Contains('running')) { - Write-Host "VM $($myvm.name) (RG: $($myvm.resourceGroup)) is already running." - } else { - Write-Host "Starting $($myvm.name) (RG: $($myvm.resourceGroup)):" - az vm start -g $myvm.resourceGroup -n $myvm.name - Write-Host "...done." + $resVms = @() + Write-Host "Checking state of VM(s)..." + foreach ($singlevm in $VmName) { + $myvm = azvmidentify -VmName $singlevm + # Since az vm start is taking quite its time even when the machine is started, we should check ourselves whether the machine is running: + if ( (az vm show -d -g $myvm.resourceGroup -n $myvm.name -o json | COnvertFrom-Json).powerState.Contains('running')) { + Write-Host "VM $($myvm.name) (RG: $($myvm.resourceGroup)) is already running." + } else { + Write-Host "VM $($myvm.name) (RG: $($myvm.resourceGroup)) is not started." + $resVms += $myvm + } } -}
\ No newline at end of file + Write-Host "Starting remaining $($resVms.Count) machine(s)..." + $jobs = ( + $resVms | ForEach-Object { + # Write-Host "Starting $($_.name) (RG: $($_.resourceGroup)):" + $myvm = $_ + Start-ThreadJob -ScriptBlock { + $this = $using:myvm + az vm start -o jsonc -g $this.resourceGroup -n $this.name + } + # Write-Host "...done." + } + ) + $jobs | Receive-Job -Wait -AutoRemoveJob +} |