From 19d85ccf501096c4a7c1feb808fdd2342d577664 Mon Sep 17 00:00:00 2001 From: Harald Pfeiffer Date: Wed, 30 Jul 2025 18:20:41 +0200 Subject: Rename Start and Stop Cmdlets to avoid collisions with Az.Compute --- AzureHelpers/Public/Start-AzVm.ps1 | 60 ------------------------- AzureHelpers/Public/Start-AzureVm.ps1 | 60 +++++++++++++++++++++++++ AzureHelpers/Public/Stop-AzVm.ps1 | 84 ----------------------------------- AzureHelpers/Public/Stop-AzureVm.ps1 | 84 +++++++++++++++++++++++++++++++++++ 4 files changed, 144 insertions(+), 144 deletions(-) delete mode 100644 AzureHelpers/Public/Start-AzVm.ps1 create mode 100644 AzureHelpers/Public/Start-AzureVm.ps1 delete mode 100644 AzureHelpers/Public/Stop-AzVm.ps1 create mode 100644 AzureHelpers/Public/Stop-AzureVm.ps1 diff --git a/AzureHelpers/Public/Start-AzVm.ps1 b/AzureHelpers/Public/Start-AzVm.ps1 deleted file mode 100644 index 49dd55a..0000000 --- a/AzureHelpers/Public/Start-AzVm.ps1 +++ /dev/null @@ -1,60 +0,0 @@ -function Start-AzVm { - # Supply a VM name (or an unambiguous part of it) and start the machine. - <# - .SYNOPSIS - Start an Azure VM whose name contains the input string. - - .DESCRIPTION - If we can unambiguously determine a single VM name inside the subscription - 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 names or unambiguous parts of the names of the VMs we intend to - start. - - .OUTPUTS - String. Status messages and the actual AzureCLI outputs. - #> - Param ( - [Parameter( - Mandatory=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Strings that are VM names or are part of single unambiguous VMs", - Position=0 - ) - ] - [ValidateLength(1,64)] - [string[]] - $VmName - ) - $ErrorActionPreference = 'Stop' - $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 - } - } - 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/Start-AzureVm.ps1 b/AzureHelpers/Public/Start-AzureVm.ps1 new file mode 100644 index 0000000..258eb06 --- /dev/null +++ b/AzureHelpers/Public/Start-AzureVm.ps1 @@ -0,0 +1,60 @@ +function Start-AzureVm { + # Supply a VM name (or an unambiguous part of it) and start the machine. + <# + .SYNOPSIS + Start an Azure VM whose name contains the input string. + + .DESCRIPTION + If we can unambiguously determine a single VM name inside the subscription + 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 names or unambiguous parts of the names of the VMs we intend to + start. + + .OUTPUTS + String. Status messages and the actual AzureCLI outputs. + #> + Param ( + [Parameter( + Mandatory=$true, + ValueFromPipelineByPropertyName=$true, + HelpMessage="Strings that are VM names or are part of single unambiguous VMs", + Position=0 + ) + ] + [ValidateLength(1,64)] + [string[]] + $VmName + ) + $ErrorActionPreference = 'Stop' + $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 + } + } + 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 deleted file mode 100644 index f284ec8..0000000 --- a/AzureHelpers/Public/Stop-AzVm.ps1 +++ /dev/null @@ -1,84 +0,0 @@ -function Stop-AzVm { - # Supply a VM name (or an unambiguous part of it), stop, and deallocate the machine. - <# - .SYNOPSIS - Stop **and** deallocate an Azure VM whose name contains the input string. - - .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 - the machine. - - This function was built as we want to have terse input and we do not want to - be billed for machines we stop, the latter requiring two actual commands. - - .INPUTS - String. The names or unambiguous parts of the names of the VMs we intend to - stop and deallocate. - - .OUTPUTS - String. Status messages and the actual AzureCLI outputs. - #> - [Alias( - 'azvmstop', - 'azvmd', - 'Deallocate-AzVm' - )] - Param ( - [Parameter( - Mandatory=$true, - ValueFromPipelineByPropertyName=$true, - HelpMessage="Strings that are VM names or are part of single unambiguous VMs", - Position=0 - ) - ] - [ValidateLength(1,64)] - [string[]] - $VmName - ) - $ErrorActionPreference = 'Stop' - $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 - } - } - } - 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 -} diff --git a/AzureHelpers/Public/Stop-AzureVm.ps1 b/AzureHelpers/Public/Stop-AzureVm.ps1 new file mode 100644 index 0000000..2c1ce0f --- /dev/null +++ b/AzureHelpers/Public/Stop-AzureVm.ps1 @@ -0,0 +1,84 @@ +function Stop-AzureVm { + # Supply a VM name (or an unambiguous part of it), stop, and deallocate the machine. + <# + .SYNOPSIS + Stop **and** deallocate an Azure VM whose name contains the input string. + + .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 + the machine. + + This function was built as we want to have terse input and we do not want to + be billed for machines we stop, the latter requiring two actual commands. + + .INPUTS + String. The names or unambiguous parts of the names of the VMs we intend to + stop and deallocate. + + .OUTPUTS + String. Status messages and the actual AzureCLI outputs. + #> + [Alias( + 'azvmstop', + 'azvmd', + 'Deallocate-AzVm' + )] + Param ( + [Parameter( + Mandatory=$true, + ValueFromPipelineByPropertyName=$true, + HelpMessage="Strings that are VM names or are part of single unambiguous VMs", + Position=0 + ) + ] + [ValidateLength(1,64)] + [string[]] + $VmName + ) + $ErrorActionPreference = 'Stop' + $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 + } + } + } + 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 +} -- cgit v1.2.3