diff options
author | mail_redacted_for_web | 2025-07-30 18:09:17 +0200 |
---|---|---|
committer | mail_redacted_for_web | 2025-07-30 18:09:17 +0200 |
commit | 7f0c345d73fa0404da747bd02fb19f35fcb5e1aa (patch) | |
tree | dc99fb1737013b9b32308f5185859fafcc5fd09a | |
parent | 03fbf75c8e8eca13368b9e1545f2721a35af9e68 (diff) | |
download | azure-helpers-7f0c345d73fa0404da747bd02fb19f35fcb5e1aa.tar.bz2 |
Extension: allow the user to specify multiple VMs for parallel execution
-rw-r--r-- | AzureHelpers/Public/Start-AzVm.ps1 | 44 | ||||
-rw-r--r-- | AzureHelpers/Public/Stop-AzVm.ps1 | 69 |
2 files changed, 78 insertions, 35 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 +} diff --git a/AzureHelpers/Public/Stop-AzVm.ps1 b/AzureHelpers/Public/Stop-AzVm.ps1 index 1b3bd07..f284ec8 100644 --- a/AzureHelpers/Public/Stop-AzVm.ps1 +++ b/AzureHelpers/Public/Stop-AzVm.ps1 @@ -13,7 +13,7 @@ function Stop-AzVm { be billed for machines we stop, the latter requiring two actual commands. .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 stop and deallocate. .OUTPUTS @@ -28,30 +28,57 @@ function Stop-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 stop is taking quite its time even when the machine is started, we should check ourselves whether the machine is running: - $myPowerState = (az vm show -d -g $myvm.resourceGroup -n $myvm.name -o json | ConvertFrom-Json).powerState - if ( ($myPowerState -match '(stopped|deallocated)$') ) { - Write-Host "VM $($myvm.name) (RG: $($myvm.resourceGroup)) is already stopped." - } else { - Write-Host "Stopping $($myvm.name) (RG: $($myvm.resourceGroup)):" - az vm stop -g $myvm.resourceGroup -n $myvm.name - Write-Host "...done." + $stopVms = @() + $deallocVms = @() + Write-Host "Checking state of VM(s)..." + foreach ($singlevm in $VmName) { + $myvm = azvmidentify -VmName $singlevm + # Since az vm stop is taking quite its time even when the machine is started, we should check ourselves whether the machine is running: + $myPowerState = (az vm show -d -g $myvm.resourceGroup -n $myvm.name -o json | ConvertFrom-Json).powerState + if ( ($myPowerState -match 'stopped$') ) { + Write-Host "VM $($myvm.name) (RG: $($myvm.resourceGroup)) is already stopped." + $deallocVms += $myvm + } else { + if ( ($myPowerState -match 'deallocated$') ) { + Write-Host "VM $($myvm.name) (RG: $($myvm.resourceGroup)) is already deallocated." + } else { + $stopVms += $myvm + $deallocVms += $myvm + } + } } - if ( ($myPowerState -match 'deallocated$') ) { - Write-Host "VM $($myvm.name) (RG: $($myvm.resourceGroup)) is already deallocated." - } else { - Write-Host "Deallocating $($myvm.name) (RG: $($myvm.resourceGroup)):" - az vm deallocate -g $myvm.resourceGroup -n $myvm.name - Write-Host "...done." - } -}
\ No newline at end of file + Write-Host "Stopping $($stopVms.Count) machine(s)..." + $jobs = ( + $stopVms | ForEach-Object { + # Write-Host "Starting $($_.name) (RG: $($_.resourceGroup)):" + $myvm = $_ + Start-ThreadJob -ScriptBlock { + $this = $using:myvm + # Write-Host "Stopping $($myvm.name) (RG: $($myvm.resourceGroup)):" + az vm stop --output jsonc --resource-group $($this.resourceGroup) --name $($this.name) + # Write-Host "...done." + } + } + ) + $jobs | Receive-Job -Wait -AutoRemoveJob + Write-Host "Deallocating $($deallocVms.Count) machine(s)..." + $jobs = ( + $deallocVms | ForEach-Object { + $myvm = $_ + Start-ThreadJob -ScriptBlock { + $this = $using:myvm + az vm deallocate --output jsonc --resource-group $($this.resourceGroup) --name $($this.name) + } + } + ) + $jobs | Receive-Job -Wait -AutoRemoveJob +} |