Last active
June 7, 2022 00:55
-
-
Save JustinGrote/f79869bedbb24d73e9ab1aa7371ef708 to your computer and use it in GitHub Desktop.
Helper command to easily register an Azure Key Vault as a local SecretManagement Vault
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
| #requires -module Az.Keyvault,Microsoft.PowerShell.SecretManagement | |
| filter Register-AzKeyVault { | |
| <# | |
| .SYNOPSIS | |
| Registers an Azure Key vault as a local SecretManagement vault. | |
| .EXAMPLE | |
| Register-AzKeyVault 'MyVault' | |
| .EXAMPLE | |
| Register-AzKeyvault M<tab> | |
| .EXAMPLE | |
| Get-AzKeyVault | Register-AzKeyVault | |
| Registers all keyvaults in the current context as local SecretManagement vaults. | |
| .EXAMPLE | |
| Get-AzKeyVault | Register-AzKeyVault -WhatIf | |
| Preview the vaults that would be added. | |
| #> | |
| [CmdletBinding(SupportsShouldProcess)] | |
| param( | |
| #Enter the name of the vault to register. You can also pipe a vault object from Get-AzKeyVault to this command. Supports autocomplete for vaults in the current context. | |
| [ArgumentCompleter({ | |
| $errorActionPreference = 'stop' | |
| $WarningPreference = 'silentlycontinue' | |
| $wordToComplete = $args[2] #The autocompleter automatically supplies this based on what the user entered | |
| (Get-AzKeyVault).VaultName | Where-Object {$_ -like "$wordToComplete*"} | |
| })] | |
| [Parameter(Mandatory,ValueFromPipelineByPropertyName)][String]$VaultName, | |
| #The local name for this vault registration. Defaults to the same name as the vault. | |
| [String]$Name, | |
| #Enter the subscription ID or Name. Defaults to the current subscription context. Supports autocomplete. | |
| [ArgumentCompleter({ | |
| $errorActionPreference = 'stop' | |
| $WarningPreference = 'silentlycontinue' | |
| $wordToComplete = $args[2] #The autocompleter automatically supplies this based on what the user entered | |
| (Get-AzSubscription).Name | Where-Object { $_ -like "*$($wordToComplete -replace "'")*" } | Foreach-Object {"'$PSItem'"} #Args[2] is the word to complete | |
| })] | |
| [String]$Subscription = $((Get-AzContext).Subscription.Id), | |
| #Sets the vault as the default vault for the secret commands | |
| [Switch]$DefaultVault | |
| ) | |
| if ($Name -ne $VaultName) { | |
| $Name = $VaultName | |
| } | |
| $ErrorActionPreference = 'Stop' | |
| if (-not ($Subscription -as [Guid])) { | |
| $Subscription = Get-AzSubscription -SubscriptionName $Subscription | |
| } | |
| $registerParams = @{ | |
| Name = $Name | |
| ModuleName = 'Az.KeyVault' | |
| DefaultVault = $DefaultVault | |
| VaultParameters = @{ | |
| AZKVaultName = $VaultName | |
| SubscriptionId = $Subscription | |
| } | |
| } | |
| if ($PSCmdlet.ShouldProcess($Name, "Register Azure Key Vault $VaultName ($Subscription) as local SecretManagement Vault")) { | |
| Register-SecretVault @registerParams | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment