I have multiple Azure subscriptions linked to my account, so anytime I connect to Azure in PowerShell I have to make sure i am working in the right context. To simplify this I wrote a little helper function called Select-MyAzureRmSubscription.
After entering the -SubscriptionName parameter the script enumerates alll the subscriptions I have access to and generates a dynamic parameter option.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
function Select-MyAzureRmSubscription { <# .Synopsis Select-MyAzureRmSubscription .DESCRIPTION The Select-MyAzureRmSubscription cmdlet provides an easy way to select an Azure Subscription and sets authentication information for cmdlets that you run in the current session. The context includes tenant subscription, and environment information. .PARAMETER SubscriptionName A list of Subscriptions that the connected user has access to. The cmdlet dynamically builds a list of accessible subscriptions .EXAMPLE Select-MyAzureRmSubscription -SubscriptionName 'Visual Studio Professional with MSDN' -verbose Sets the context to the Subscription 'Visual Studio Professional with MSDN' VERBOSE: Selected SubscriptionName: Visual Studio Professional with MSDN VERBOSE: Setting Azure Context to Visual Studio Professional with MSDN Environment : AzureCloud Account : user1@outlook.com TenantId : f2108ecc-dd4a-4b24-9f58-aaaaaaaaaaaa SubscriptionId : 46327b72-b63c-48dd-b7f9-aaaaaaaaaaaa SubscriptionName : Visual Studio Professional with MSDN CurrentStorageAccount : .NOTES Connec to Azure using Login-AzureRmAccount prior using this function Version 1.0, 21.12.2016, Alex Verboon #> [CmdletBinding()] Param() DynamicParam { $attributes = new-object System.Management.Automation.ParameterAttribute $attributes.ParameterSetName = "__AllParameterSets" $attributes.Mandatory = $false $attributeCollection = new-object -Type System.Collections.ObjectModel.Collection[System.Attribute] $attributeCollection.Add($attributes) $_Values = ((Get-AzureRmSubscription | select-object SubscriptionName | Sort-object Name).SubscriptionName) If ([string]::IsNullOrEmpty($_Values)) { Write-Error "No Subscriptions found, check your connectivity to Azure" Throw } $ValidateSet = new-object System.Management.Automation.ValidateSetAttribute($_Values) $attributeCollection.Add($ValidateSet) $SubscriptionName = new-object -Type System.Management.Automation.RuntimeDefinedParameter("SubscriptionName", [string], $attributeCollection) $paramDictionary = new-object -Type System.Management.Automation.RuntimeDefinedParameterDictionary $paramDictionary.Add("SubscriptionName", $SubscriptionName) return $paramDictionary } Begin{ $SubscriptionName = $SubscriptionName.Value Write-Verbose "Selected SubscriptionName: $SubscriptionName" } Process{ Write-verbose "Setting Azure Context to $SubscriptionName" Select-AzureRmSubscription -SubscriptionName "$SubscriptionName" } End{} } |
Script location on GitHub: https://github.com/alexverboon/posh/blob/master/Azure/Utilities/select-MyAzureRmSubscription.ps1