From 1241b0b73bccbfc11b0a576d1c6180997a834f07 Mon Sep 17 00:00:00 2001 From: Jeff Brown Date: Sun, 13 Apr 2025 09:58:23 -0700 Subject: [PATCH] Modify Subscription logic in Get-VnetAddressSpace --- PowerAzPlus-help.xml | 95 ++++++++++++++++++++++++------- PowerAzPlus.psd1 | 2 +- PowerAzPlus.psm1 | 22 +++++-- docs/Export-AllKeyVaultSecrets.md | 20 ++++--- docs/Get-VnetAddressSpace.md | 68 ++++++++++++++++------ docs/Import-AllKeyVaultSecrets.md | 16 ++++-- 6 files changed, 165 insertions(+), 58 deletions(-) diff --git a/PowerAzPlus-help.xml b/PowerAzPlus-help.xml index f5eccc8..6025e6c 100644 --- a/PowerAzPlus-help.xml +++ b/PowerAzPlus-help.xml @@ -42,7 +42,7 @@ ProgressAction - + @{Text=} ActionPreference @@ -81,7 +81,7 @@ ProgressAction - + @{Text=} ActionPreference @@ -125,7 +125,7 @@ - -------------------------- EXAMPLE 2 -------------------------- + -------------------------- Example 2 -------------------------- PS C:\> Export-AllKeyVaultSecrets -VaultName "MyKeyVault" -OutputPath "C:\Backup\Secrets" Exports all secrets from "MyKeyVault" and saves them in the specified directory. @@ -332,7 +332,7 @@ ProgressAction - + @{Text=} ActionPreference @@ -344,6 +344,18 @@ Get-VnetAddressSpace + + SubscriptionId + + Specifies one or more Azure subscriptions by Subscription ID to retrieve VNets from. If omitted, the default subscription is used. + + String[] + + String[] + + + None + SortByIp @@ -355,10 +367,25 @@ False + + ProgressAction + + @{Text=} + + ActionPreference + + ActionPreference + + + None + + + + Get-VnetAddressSpace - Subscription + SubscriptionName - Specifies one or more Azure subscriptions by Subscription ID to retrieve VNets from. If omitted, the default subscription is used. + Specifies one or more Azure subscriptions by Subscription Name to retrieve VNets from. If omitted, the default subscription is used. String[] @@ -367,10 +394,21 @@ None + + SortByIp + + Sorts the output based on the numerical value of the first IP address in each address prefix. + + + SwitchParameter + + + False + ProgressAction - + @{Text=} ActionPreference @@ -395,21 +433,21 @@ False - SortByIp + SubscriptionId - Sorts the output based on the numerical value of the first IP address in each address prefix. + Specifies one or more Azure subscriptions by Subscription ID to retrieve VNets from. If omitted, the default subscription is used. - SwitchParameter + String[] - SwitchParameter + String[] - False + None - Subscription + SubscriptionName - Specifies one or more Azure subscriptions by Subscription ID to retrieve VNets from. If omitted, the default subscription is used. + Specifies one or more Azure subscriptions by Subscription Name to retrieve VNets from. If omitted, the default subscription is used. String[] @@ -418,10 +456,22 @@ None + + SortByIp + + Sorts the output based on the numerical value of the first IP address in each address prefix. + + SwitchParameter + + SwitchParameter + + + False + ProgressAction - + @{Text=} ActionPreference @@ -459,14 +509,21 @@ -------------------------- Example 1 -------------------------- + PS C:\> Get-VnetAddressSpace + + Retrieves the address space of all VNets in the current signed in tenant and subscription. + + + + -------------------------- Example 2 -------------------------- PS C:\> Get-VnetAddressSpace -AllSubscriptions Retrieves the address space of all VNets across all enabled Azure subscriptions. - -------------------------- Example 2 -------------------------- - PS C:\> Get-VnetAddressSpace -Subscription "12345678-90ab-cdef-1234-567890abcdef","abcdef12-3456-7890-abcd-ef1234567890" -SortByIp + -------------------------- Example 3 -------------------------- + PS C:\> Get-VnetAddressSpace -SubscriptionId "12345678-90ab-cdef-1234-567890abcdef","abcdef12-3456-7890-abcd-ef1234567890" -SortByIp Retrieves the address space of all VNets in the specified subscriptions and sorts the output by IP address. @@ -516,7 +573,7 @@ ProgressAction - + @{Text=} ActionPreference @@ -555,7 +612,7 @@ ProgressAction - + @{Text=} ActionPreference diff --git a/PowerAzPlus.psd1 b/PowerAzPlus.psd1 index 2dddfa5..85838c4 100644 --- a/PowerAzPlus.psd1 +++ b/PowerAzPlus.psd1 @@ -12,7 +12,7 @@ RootModule = 'PowerAzPlus.psm1' # Version number of this module. -ModuleVersion = '1.2.0' +ModuleVersion = '1.2.1' # Supported PSEditions # CompatiblePSEditions = @() diff --git a/PowerAzPlus.psm1 b/PowerAzPlus.psm1 index 9b63bf7..da95b13 100644 --- a/PowerAzPlus.psm1 +++ b/PowerAzPlus.psm1 @@ -97,7 +97,7 @@ function Import-LogicAppDefinition { function Get-VnetAddressSpace { [CmdletBinding( - DefaultParameterSetName = "Subscription" + DefaultParameterSetName = "SubscriptionId" )] param( @@ -105,9 +105,13 @@ function Get-VnetAddressSpace { [switch] $AllSubscriptions, - [Parameter(ParameterSetName = "Subscription")] + [Parameter(ParameterSetName = "SubscriptionId")] [string[]] - $Subscription, + $SubscriptionId, + + [Parameter(ParameterSetName = "SubscriptionName")] + [string[]] + $SubscriptionName, [Parameter()] [switch] @@ -115,15 +119,21 @@ function Get-VnetAddressSpace { ) # Get subscriptions based on parameter set - $allSubs = if ($AllSubscriptions) { + $allSubs = if ($PSCmdlet.ParameterSetName -eq "AllSubscriptions") { Get-AzSubscription -WarningAction SilentlyContinue | Where-Object State -EQ 'Enabled' } + elseif ($PSCmdlet.ParameterSetName -eq "SubscriptionId") { + $SubscriptionId | ForEach-Object { Get-AzSubscription -SubscriptionId $_ -WarningAction SilentlyContinue } + } + elseif ($PSCmdlet.ParameterSetName -eq "SubscriptionName") { + $SubscriptionName | ForEach-Object { Get-AzSubscription -SubscriptionName $_ -WarningAction SilentlyContinue } + } else { - $Subscription | ForEach-Object { Get-AzSubscription -SubscriptionId $_ -WarningAction SilentlyContinue } + (Get-AzContext).Subscription.Id } foreach ($sub in $allSubs) { - Set-AzContext -SubscriptionObject $sub | Out-Null + Set-AzContext -SubscriptionObject $sub -WarningAction SilentlyContinue | Out-Null $allVnets = Get-AzVirtualNetwork $results = [System.Collections.Generic.List[PSCustomObject]]::new() diff --git a/docs/Export-AllKeyVaultSecrets.md b/docs/Export-AllKeyVaultSecrets.md index 3102b95..0e0eae1 100644 --- a/docs/Export-AllKeyVaultSecrets.md +++ b/docs/Export-AllKeyVaultSecrets.md @@ -18,19 +18,20 @@ Export-AllKeyVaultSecrets [-VaultName] [[-OutputPath] ] [-Progr ``` ## DESCRIPTION -The Export-AllKeyVaultSecrets function retrieves all secrets from the specified Azure Key Vault and exports them as backup files. The function ensures the Key Vault exists before proceeding and generates timestamped filenames for each exported secret. +The Export-AllKeyVaultSecrets function retrieves all secrets from the specified Azure Key Vault and exports them as backup files. +The function ensures the Key Vault exists before proceeding and generates timestamped filenames for each exported secret. ## EXAMPLES ### Example 1 -```powershell +``` PS C:\> Export-AllKeyVaultSecrets -VaultName "MyKeyVault" ``` Exports all secrets from "MyKeyVault" and saves them in the current working directory. ### Example 2 -```powersell +``` PS C:\> Export-AllKeyVaultSecrets -VaultName "MyKeyVault" -OutputPath "C:\Backup\Secrets" ``` @@ -39,7 +40,9 @@ Exports all secrets from "MyKeyVault" and saves them in the specified directory. ## PARAMETERS ### -OutputPath -Specifies the directory where the exported secrets will be saved. If not provided, the function uses the current working directory. The provided path must exist. +Specifies the directory where the exported secrets will be saved. +If not provided, the function uses the current working directory. +The provided path must exist. ```yaml Type: String @@ -54,7 +57,8 @@ Accept wildcard characters: False ``` ### -VaultName -Specifies the name of the Azure Key Vault from which secrets will be exported. This parameter is required. +Specifies the name of the Azure Key Vault from which secrets will be exported. +This parameter is required. ```yaml Type: String @@ -69,6 +73,7 @@ Accept wildcard characters: False ``` ### -ProgressAction +@{Text=} ```yaml Type: ActionPreference @@ -88,14 +93,13 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable ## INPUTS ### None - ## OUTPUTS ### System.Object ## NOTES -Requires Azure PowerShell (`Az` module). +Requires Azure PowerShell (\`Az\` module). The target and destination Key Vaults must be in the same subscription and an Azure region within the same geography. -Each exported secret file is named using the format: `-.`. +Each exported secret file is named using the format: \`\-\.\\`. The function checks if the Key Vault exists before proceeding. If no secrets are found in the Key Vault, a warning message is displayed. diff --git a/docs/Get-VnetAddressSpace.md b/docs/Get-VnetAddressSpace.md index 6a14aa0..8b0f7a9 100644 --- a/docs/Get-VnetAddressSpace.md +++ b/docs/Get-VnetAddressSpace.md @@ -12,9 +12,9 @@ Retrieves the address space of virtual networks (VNets) across one or multiple A ## SYNTAX -### Subscription (Default) +### SubscriptionId (Default) ``` -Get-VnetAddressSpace [-Subscription ] [-SortByIp] [-ProgressAction ] +Get-VnetAddressSpace [-SubscriptionId ] [-SortByIp] [-ProgressAction ] [] ``` @@ -23,21 +23,36 @@ Get-VnetAddressSpace [-Subscription ] [-SortByIp] [-ProgressAction ] [] ``` +### SubscriptionName +``` +Get-VnetAddressSpace [-SubscriptionName ] [-SortByIp] [-ProgressAction ] + [] +``` + ## DESCRIPTION -The Get-VnetAddressSpace function retrieves all virtual networks and their associated address spaces in the specified Azure subscriptions. It supports querying a single subscription, multiple subscriptions, or all enabled subscriptions. The output can be sorted by IP address if desired. +The Get-VnetAddressSpace function retrieves all virtual networks and their associated address spaces in the specified Azure subscriptions. +It supports querying a single subscription, multiple subscriptions, or all enabled subscriptions. +The output can be sorted by IP address if desired. ## EXAMPLES ### Example 1 -```powershell +``` +PS C:\> Get-VnetAddressSpace +``` + +Retrieves the address space of all VNets in the current signed in tenant and subscription. + +### Example 2 +``` PS C:\> Get-VnetAddressSpace -AllSubscriptions ``` Retrieves the address space of all VNets across all enabled Azure subscriptions. -### Example 2 -```powershell -PS C:\> Get-VnetAddressSpace -Subscription "12345678-90ab-cdef-1234-567890abcdef","abcdef12-3456-7890-abcd-ef1234567890" -SortByIp +### Example 3 +``` +PS C:\> Get-VnetAddressSpace -SubscriptionId "12345678-90ab-cdef-1234-567890abcdef","abcdef12-3456-7890-abcd-ef1234567890" -SortByIp ``` Retrieves the address space of all VNets in the specified subscriptions and sorts the output by IP address. @@ -54,17 +69,18 @@ Aliases: Required: False Position: Named -Default value: None +Default value: False Accept pipeline input: False Accept wildcard characters: False ``` -### -SortByIp -Sorts the output based on the numerical value of the first IP address in each address prefix. +### -SubscriptionId +Specifies one or more Azure subscriptions by Subscription ID to retrieve VNets from. +If omitted, the default subscription is used. ```yaml -Type: SwitchParameter -Parameter Sets: (All) +Type: String[] +Parameter Sets: SubscriptionId Aliases: Required: False @@ -74,12 +90,13 @@ Accept pipeline input: False Accept wildcard characters: False ``` -### -Subscription -Specifies one or more Azure subscriptions by Subscription ID to retrieve VNets from. If omitted, the default subscription is used. +### -SubscriptionName +Specifies one or more Azure subscriptions by Subscription Name to retrieve VNets from. +If omitted, the default subscription is used. ```yaml Type: String[] -Parameter Sets: Subscription +Parameter Sets: SubscriptionName Aliases: Required: False @@ -89,7 +106,23 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -SortByIp +Sorts the output based on the numerical value of the first IP address in each address prefix. + +```yaml +Type: SwitchParameter +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: False +Accept pipeline input: False +Accept wildcard characters: False +``` + ### -ProgressAction +@{Text=} ```yaml Type: ActionPreference @@ -109,13 +142,12 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable ## INPUTS ### None - ## OUTPUTS ### System.Object ## NOTES -Requires Azure PowerShell (`Az` module). +Requires Azure PowerShell (\`Az\` module). The function sets the subscription context before retrieving VNets. -The `SortByIp` switch converts the first octet of the IP address to an integer for sorting. +The \`SortByIp\` switch converts the first octet of the IP address to an integer for sorting. ## RELATED LINKS diff --git a/docs/Import-AllKeyVaultSecrets.md b/docs/Import-AllKeyVaultSecrets.md index 541ddc7..ebe27ae 100644 --- a/docs/Import-AllKeyVaultSecrets.md +++ b/docs/Import-AllKeyVaultSecrets.md @@ -18,12 +18,14 @@ Import-AllKeyVaultSecrets [-VaultName] [-InputFilePath] [-Prog ``` ## DESCRIPTION -The Import-AllKeyVaultSecrets function restores all Key Vault secrets from backup files stored in a specified directory. It ensures that the target Key Vault exists before proceeding. The function processes all backup files in the provided directory and restores them to the specified Key Vault. +The Import-AllKeyVaultSecrets function restores all Key Vault secrets from backup files stored in a specified directory. +It ensures that the target Key Vault exists before proceeding. +The function processes all backup files in the provided directory and restores them to the specified Key Vault. ## EXAMPLES ### Example 1 -```powershell +``` PS C:\> Import-AllKeyVaultSecrets -VaultName "MyKeyVault" -InputFilePath "C:\Backup\Secrets" ``` @@ -32,7 +34,8 @@ Restores all secrets from the specified directory to the "MyKeyVault" Key Vault. ## PARAMETERS ### -InputFilePath -Specifies the directory containing the backup files to be restored. This parameter is required and must be a valid path. +Specifies the directory containing the backup files to be restored. +This parameter is required and must be a valid path. ```yaml Type: String @@ -47,7 +50,8 @@ Accept wildcard characters: False ``` ### -VaultName -Specifies the name of the Azure Key Vault where secrets will be imported. This parameter is required. +Specifies the name of the Azure Key Vault where secrets will be imported. +This parameter is required. ```yaml Type: String @@ -62,6 +66,7 @@ Accept wildcard characters: False ``` ### -ProgressAction +@{Text=} ```yaml Type: ActionPreference @@ -81,12 +86,11 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable ## INPUTS ### None - ## OUTPUTS ### System.Object ## NOTES -Requires Azure PowerShell (`Az` module). +Requires Azure PowerShell (\`Az\` module). The target and destination Key Vaults must be in the same subscription and an Azure region within the same geography. The function verifies that the Key Vault exists before proceeding. If no backup files are found in the specified directory, a warning message is displayed.