Skip to content

Commit afdaddd

Browse files
author
BradleyBartlett
committed
Add version checking and optional utilities module
1 parent bed7a60 commit afdaddd

File tree

2 files changed

+266
-7
lines changed

2 files changed

+266
-7
lines changed

Registration/RegisterWithAzure.psm1

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,22 +1601,60 @@ function Confirm-StampVersion{
16011601
[Parameter(Mandatory=$true)]
16021602
[System.Management.Automation.Runspaces.PSSession] $PSSession
16031603
)
1604+
1605+
$registrationVersion = [Version]"1.1806.0.21"
16041606
try
16051607
{
16061608
Log-Output "Verifying stamp version."
16071609
$stampInfo = Invoke-Command -Session $PSSession -ScriptBlock { Get-AzureStackStampInformation -WarningAction SilentlyContinue }
1608-
$minVersion = [Version]"1.0.170828.1"
1609-
if ([Version]$stampInfo.StampVersion -lt $minVersion) {
1610-
Log-Throw -Message "Script only applicable for Azure Stack builds $minVersion or later." -CallingFunction $PSCmdlet.MyInvocation.MyCommand.Name
1611-
}
1612-
1613-
Log-Output -Message "Running registration actions on build $($stampInfo.StampVersion). Cloud Id: $($stampInfo.CloudID), Deployment Id: $($stampInfo.DeploymentID)"
1614-
return $stampInfo
16151610
}
16161611
Catch
16171612
{
16181613
Log-Throw "An error occurred checking stamp information: `r`n$($_)" -CallingFunction $PSCmdlet.MyInvocation.MyCommand.Name
16191614
}
1615+
1616+
$versionNumber = [Version]$stampInfo.StampVersion
1617+
$minVersion = [Version]"1.0.170928.1"
1618+
if ($versionNumber -lt $minVersion)
1619+
{
1620+
Log-Throw -Message "Script only applicable for Azure Stack builds $minVersion or later." -CallingFunction $PSCmdlet.MyInvocation.MyCommand.Name
1621+
}
1622+
1623+
if ($versionNumber -lt $registrationVersion)
1624+
{
1625+
switch ($versionNumber.Build)
1626+
{
1627+
"170928"
1628+
{
1629+
Log-Warning -Message "Running a newer version of registration with an older version of Azure Stack. Registration version: $registrationVersion Build version: $versionNumber"
1630+
Log-Warning -Message "NOTE: The below URL is NOT a module and does not need to be imported!"
1631+
Log-Throw -Message "Please download the correct version of the registration functions from the URL below and retry: `r`nhttps://github.com/Azure/AzureStack-Tools/blob/registration/v1709/Registration/RegisterWithAzure.ps1`r`n" -CallingFunction $PSCmdlet.MyInvocation.MyCommand.Name
1632+
}
1633+
"171020"
1634+
{
1635+
Log-Warning -Message "Running a newer version of registration with an older version of Azure Stack. Registration version: $registrationVersion Build version: $versionNumber"
1636+
Log-Throw -Message "Please download the correct version of the registration functions from the URL below and retry: `r`nhttps://github.com/Azure/AzureStack-Tools/blob/registration/v1710/Registration/RegisterWithAzure.psm1`r`n" -CallingFunction $PSCmdlet.MyInvocation.MyCommand.Name
1637+
}
1638+
"171201"
1639+
{
1640+
Log-Warning -Message "Running a newer version of registration with an older version of Azure Stack. Registration version: $registrationVersion Build version: $versionNumber"
1641+
Log-Throw -Message "Please download the correct version of the registration functions from the URL below and retry: `r`nhttps://github.com/Azure/AzureStack-Tools/blob/registration/v1710/Registration/RegisterWithAzure.psm1`r`n" -CallingFunction $PSCmdlet.MyInvocation.MyCommand.Name
1642+
}
1643+
"180106"
1644+
{
1645+
Log-Warning -Message "Running a newer version of registration with an older version of Azure Stack. Registration version: $registrationVersion Build version: $versionNumber"
1646+
Log-Throw -Message "Please download the correct version of the registration functions from the URL below and retry: `r`nhttps://github.com/Azure/AzureStack-Tools/blob/registration/v1712/Registration/RegisterWithAzure.psm1`r`n" -CallingFunction $PSCmdlet.MyInvocation.MyCommand.Name
1647+
}
1648+
}
1649+
}
1650+
elseif (($versionNumber.Build -gt "180106") -and ($versionNumber.Build -lt $registrationVersion))
1651+
{
1652+
Log-Warning -Message "Running a newer version of registration with an older version of Azure Stack. Registration version: $registrationVersion Build version: $versionNumber"
1653+
Log-Throw -Message "Please download the correct version of the registration functions from the URL below and retry: `r`nhttps://github.com/Azure/AzureStack-Tools/blob/registration/v1803/Registration/RegisterWithAzure.psm1`r`n" -CallingFunction $PSCmdlet.MyInvocation.MyCommand.Name
1654+
}
1655+
1656+
Log-Output -Message "Running registration actions on build $($stampInfo.StampVersion). Cloud Id: $($stampInfo.CloudID), Deployment Id: $($stampInfo.DeploymentID)"
1657+
return $stampInfo
16201658
}
16211659

16221660
<#
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
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

Comments
 (0)