|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# See LICENSE.txt in the project root for license information. |
| 3 | + |
| 4 | +<# |
| 5 | +
|
| 6 | +This module contains utility functions for working with registration resources |
| 7 | +#> |
| 8 | + |
| 9 | + |
| 10 | +<# |
| 11 | +
|
| 12 | +.SYNOPSIS |
| 13 | +
|
| 14 | +Uses the current Azure Powershell context to retrieve registration resources in Azure from the default resource group |
| 15 | +and with the default resource name (if $AzureStackStampCloudId is provided) |
| 16 | +
|
| 17 | +#> |
| 18 | +function Get-AzureRegistrationResource{ |
| 19 | +[CmdletBinding()] |
| 20 | +param( |
| 21 | + [Parameter(Mandatory = $false)] |
| 22 | + [String] $AzureStackStampCloudId, |
| 23 | + |
| 24 | + [Parameter(Mandatory = $false)] |
| 25 | + [String] $ResourceGroupName = "AzureStack", |
| 26 | + |
| 27 | + [Parameter(Mandatory = $false)] |
| 28 | + [String] $ResourceName = "AzureStack" |
| 29 | +) |
| 30 | + |
| 31 | +$VerbosePreference = "Continue" |
| 32 | +$ErrorActionPreference = "Stop" |
| 33 | + |
| 34 | +Write-Verbose "Searching for registration resource using the provided parameters" |
| 35 | +$registrationResources = Find-AzureRmResource -ResourceNameContains $ResourceName -ResourceType 'Microsoft.AzureStack/registrations' -ResourceGroupNameEquals $ResourceGroupName |
| 36 | +$registrations = @() |
| 37 | +foreach ($resource in $registrationResources) |
| 38 | +{ |
| 39 | + $resource = Get-AzureRmResource -ResourceId $resource.ResourceId |
| 40 | + if($AzureStackStampCloudId) |
| 41 | + { |
| 42 | + if ($resource.Properties.CloudId -eq $AzureStackStampCloudId) |
| 43 | + { |
| 44 | + Write-Verbose "Registration resource found:`r`n$(ConvertTo-Json $resource)" |
| 45 | + return $resource |
| 46 | + } |
| 47 | + } |
| 48 | + else |
| 49 | + { |
| 50 | + $registrations += $resource |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +if ($registrations.Count -gt 0) |
| 55 | +{ |
| 56 | + Write-Verbose "Registrations: $registrations" |
| 57 | +} |
| 58 | +else |
| 59 | +{ |
| 60 | + Write-Verbose "Registration resource(s) could not be located with the provided parameters." |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | +<# |
| 68 | +
|
| 69 | +.SYNOPSIS |
| 70 | +
|
| 71 | +If the context is set to the Azure Stack environment administrator this will retrieve the activation record in the Azure Stack |
| 72 | +if it has been created via successful registration run. |
| 73 | +
|
| 74 | +#> |
| 75 | +function Get-AzureStackActivationRecord{ |
| 76 | + |
| 77 | +$currentContext = Get-AzureRmContext |
| 78 | +$contextDetails = @{ |
| 79 | + Account = $currentContext.Account |
| 80 | + Environment = $currentContext.Environment |
| 81 | + Subscription = $currentContext.Subscription |
| 82 | + Tenant = $currentContext.Tenant |
| 83 | +} |
| 84 | + |
| 85 | +if (-not($currentContext.Subscription)) |
| 86 | +{ |
| 87 | + Write-Verbose "Current Azure context:`r`n$(ConvertTo-Json $ContextDetails)" |
| 88 | + Throw "Current Azure context is not currently set. Please call Login-AzureRmAccount to set the Powershell context to Azure Stack service administrator." |
| 89 | +} |
| 90 | + |
| 91 | +$subscriptions = Get-AzureRmSubscription |
| 92 | +if ($subscriptions.Count -eq 1) |
| 93 | +{ |
| 94 | + if ($subscriptions.Name -eq 'Default Provider Subscription') |
| 95 | + { |
| 96 | + try |
| 97 | + { |
| 98 | + $activation = Get-AzureRmResource -ResourceId "/subscriptions/$($subscriptions.Id)/resourceGroups/azurestack-activation/providers/Microsoft.AzureBridge.Admin/activations/default" |
| 99 | + return $activation |
| 100 | + } |
| 101 | + catch |
| 102 | + { |
| 103 | + Write-Warning "Activation record not found. Please register your Azure Stack with Azure: `r`nhttps://docs.microsoft.com/en-us/azure/azure-stack/azure-stack-register`r`n$_" |
| 104 | + } |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + Write-Warning "Unable to retrieve activation record using the current Azure Powershell context." |
| 109 | + } |
| 110 | +} |
| 111 | +else |
| 112 | +{ |
| 113 | + foreach ($sub in $subscriptions) |
| 114 | + { |
| 115 | + try |
| 116 | + { |
| 117 | + Get-AzureRmResource -ResourceId "/subscriptions/$($sub.Id)/resourceGroups/azurestack-activation/providers/Microsoft.AzureBridge.Admin/activations/default" |
| 118 | + } |
| 119 | + catch |
| 120 | + { |
| 121 | + Write-Warning "Activation record not found. $_" |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | + |
| 127 | +} |
| 128 | + |
| 129 | + |
| 130 | +<# |
| 131 | +
|
| 132 | +.SYNOPSIS |
| 133 | +
|
| 134 | +Sets the current azure powershell context to that of the Azure Stack environment administrator |
| 135 | +
|
| 136 | +#> |
| 137 | +function Set-AzureStackPowershellContext{ |
| 138 | +[CmdletBinding()] |
| 139 | +param( |
| 140 | + [Parameter(Mandatory = $true)] |
| 141 | + [String] $ServiceAdminUsername, |
| 142 | + |
| 143 | + [Parameter(Mandatory = $true)] |
| 144 | + [String] $ServiceAdminPassword, |
| 145 | + |
| 146 | + [Parameter(Mandatory = $true)] |
| 147 | + [String] $ExternalDomain, |
| 148 | + |
| 149 | + [Parameter(Mandatory = $true)] |
| 150 | + [String] $ArmEndpoint, |
| 151 | + |
| 152 | + [Parameter(Mandatory = $false)] |
| 153 | + [String] $AadTenantId |
| 154 | +) |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + $endpoints = Get-ResourceManagerMetaDataEndpoints -ArmEndpoint $ArmEndpoint |
| 159 | + |
| 160 | + $aadAuthorityEndpoint = $endpoints.authentication.loginEndpoint |
| 161 | + $aadResource = $endpoints.authentication.audiences[0] |
| 162 | + $galleryEndpoint =$endpoints.galleryEndpoint |
| 163 | + $graphEndpoint = $endpoints.graphEndpoint |
| 164 | + |
| 165 | + $azureEnvironmentParams = @{ |
| 166 | + Name = "AzureStack" |
| 167 | + ActiveDirectoryEndpoint = $($aadAuthorityEndpoint.TrimEnd("/") + "/") |
| 168 | + ActiveDirectoryServiceEndpointResourceId = $aadResource |
| 169 | + ResourceManagerEndpoint = $ArmEndpoint |
| 170 | + GalleryEndpoint = $galleryEndpoint |
| 171 | + GraphEndpoint = $graphEndpoint |
| 172 | + GraphAudience = $graphEndpoint |
| 173 | + AzureKeyVaultDnsSuffix = "adminvault.$ExternalDomain".ToLowerInvariant() |
| 174 | + EnableAdfsAuthentication = $aadAuthorityEndpoint.TrimEnd("/").EndsWith("/adfs", [System.StringComparison]::OrdinalIgnoreCase) |
| 175 | + } |
| 176 | + |
| 177 | + $environment = Add-AzureRmEnvironment @azureEnvironmentParams |
| 178 | + $environment = Get-AzureRmEnvironment -Name "AzureStack" |
| 179 | + |
| 180 | + $Credential = New-Object System.Management.Automation.PSCredential ($ServiceAdminUsername,(ConvertTo-SecureString -String $ServiceAdminPassword -AsPlainText -Force)) |
| 181 | + |
| 182 | + if ($AadTenantId) |
| 183 | + { |
| 184 | + Add-AzureRmAccount -Environment $environment -Credential $Credential -TenantId $AadTenantId |
| 185 | + } |
| 186 | + else |
| 187 | + { |
| 188 | + Add-AzureRmAccount -Environment $environment -Credential $Credential |
| 189 | + } |
| 190 | + |
| 191 | + $adminSubscription = Get-AzureRmSubscription -SubscriptionName "Default Provider Subscription" |
| 192 | + Set-AzureRmContext -SubscriptionId $adminSubscription.SubscriptionId |
| 193 | +} |
| 194 | + |
| 195 | +################################################################ |
| 196 | +# Helper Functions |
| 197 | +################################################################ |
| 198 | + |
| 199 | +<# |
| 200 | +
|
| 201 | +.SYNOPSIS |
| 202 | +
|
| 203 | +Gets the resource manager endpoints for use in the Set-AzureStackPowershellContext function |
| 204 | +
|
| 205 | +#> |
| 206 | +function Get-ResourceManagerMetaDataEndpoints{ |
| 207 | +param |
| 208 | +( |
| 209 | + [Parameter(Mandatory=$true)] |
| 210 | + [String] $ArmEndpoint |
| 211 | +) |
| 212 | + |
| 213 | +$endpoints = Invoke-RestMethod -Method Get -Uri "$($ArmEndpoint.TrimEnd('/'))/metadata/endpoints?api-version=2015-01-01" -Verbose |
| 214 | +Write-Verbose -Message "Endpoints: $(ConvertTo-Json $endpoints)" -Verbose |
| 215 | + |
| 216 | +Write-Output $endpoints |
| 217 | +} |
| 218 | + |
| 219 | +Export-ModuleMember Get-AzureRegistrationResource |
| 220 | +Export-ModuleMember Get-AzureStackActivationRecord |
| 221 | +Export-ModuleMember Set-AzureStackPowershellContext |
0 commit comments