Skip to content

Commit 6bfffd7

Browse files
author
BradleyBartlett
committed
Updated Azure registration script.
1 parent 64d6b8b commit 6bfffd7

1 file changed

Lines changed: 80 additions & 107 deletions

File tree

Registration/RegisterWithAzure.ps1

Lines changed: 80 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -22,172 +22,145 @@ Activate Azure Stack: final step in connecting Azure Stack to be able to call ou
2222
2323
Azure subscription ID that you want to register your Azure Stack with. This parameter is mandatory.
2424
25-
.PARAMETER azureDirectory
25+
.PARAMETER azureDirectoryTenantName
2626
2727
Name of your AAD Tenant which your Azure subscription is a part of. This parameter is mandatory.
2828
29-
.PARAMETER azureSubscriptionOwner
29+
.PARAMETER azureAccountId
3030
3131
Username for an owner/contributor of the azure subscription. This user must not be an MSA or 2FA account. This parameter is mandatory.
3232
33-
.PARAMETER azureSubscriptionPassword
33+
.PARAMETER azureCredentialPassword
3434
3535
Password for the Azure subscription. You will be prompted to type in this password. This parameter is mandatory.
3636
37-
.PARAMETER marketplaceSyndication
37+
.PARAMETER azureEnvironment
3838
39-
Flag (ON/OFF) whether to enable downloading items from the Azure marketplace on this environment. Defaults to "ON".
39+
Environment name for use in retrieving tenant details and running several of the activation scripts. Defaults to "AzureCloud".
4040
41-
.PARAMETER reportUsage
42-
43-
Flag (ON/OFF) whether to enable pushing usage data to Azure on this environment. Defaults to "ON".
41+
.PARAMETER azureResourceManagerEndpoint
4442
43+
URI used for ActivateBridge.ps1 that refers to the endpoint for Azure Resource Manager. Defaults to "https://management.azure.com"
4544
4645
.EXAMPLE
4746
4847
This script must be run from the Host machine of the POC.
49-
.\RegisterWithAzure.ps1 -azureSubscriptionId "5e0ae55d-0b7a-47a3-afbc-8b372650abd3" -azureDirectory "contoso.onmicrosoft.com" -azureSubscriptionOwner "serviceadmin@contoso.onmicrosoft.com"
48+
.\RegisterWithAzure.ps1 -azureSubscriptionId "5e0ae55d-0b7a-47a3-afbc-8b372650abd3" -azureDirectoryTenantId "contoso.onmicrosoft.com" -azureAccountId "serviceadmin@contoso.onmicrosoft.com" -azureCredentialPassword "password"
5049
5150
5251
.NOTES
5352
Ensure that you have an Azure subscription
5453
#>
5554

5655
[CmdletBinding()]
57-
Param (
58-
[Parameter(Mandatory = $true)]
59-
[string]$azureDirectory,
56+
param(
57+
58+
[Parameter(Mandatory=$true)]
59+
[String] $azureCredentialPassword,
6060

61-
[Parameter(Mandatory = $true)]
62-
[String]$azureSubscriptionId,
61+
[Parameter(Mandatory=$true)]
62+
[String] $azureAccountId,
6363

64-
[Parameter(Mandatory = $true)]
65-
[String]$azureSubscriptionOwner,
64+
[Parameter(Mandatory=$true)]
65+
[String] $azureSubscriptionId,
66+
67+
[Parameter(Mandatory=$true)]
68+
[String] $AzureDirectoryTenantName,
6669

67-
[Parameter(Mandatory = $true)]
68-
[securestring]$azureSubscriptionPassword,
69-
7070
[Parameter(Mandatory=$false)]
71-
[ValidateSet('On', 'Off')]
72-
[string] $marketplaceSyndication = 'On',
71+
[String] $AzureEnvironment = "AzureCloud",
7372

7473
[Parameter(Mandatory=$false)]
75-
[ValidateSet('On', 'Off')]
76-
[string] $reportUsage = 'On'
77-
)
74+
[String] $AzureResourceManagerEndpoint = "https://management.azure.com"
75+
76+
)
77+
78+
79+
#
80+
# Wrapper script to test registration and activation
81+
# Uses refresh tokens and supports 2fa, MSA accounts
82+
#
7883

79-
#requires -Module AzureRM.Profile
80-
#requires -Module AzureRM.Resources
81-
#requires -RunAsAdministrator
84+
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
85+
$VerbosePreference = [System.Management.Automation.ActionPreference]::Continue
8286

83-
Import-Module C:\CloudDeployment\ECEngine\EnterpriseCloudEngine.psm1 -Force
87+
Import-Module C:\CloudDeployment\ECEngine\EnterpriseCloudEngine.psd1 -Force
8488
cd C:\CloudDeployment\Setup\Activation\Bridge
8589

86-
Read-Host "This script will turn marketplace syndication $marketplaceSyndication and usage reporting $reportUsage. You can change this by running the script using different flags. Press Enter to continue."
87-
88-
# Determine version of TP3 and set values accordingly
89-
$versionInfo = [xml] (Get-Content -Path C:\CloudDeployment\Configuration\Version\version.xml)
90-
if($versionInfo.Version -ge "1.0.170308.2")
91-
{
92-
Write-Verbose -Message "Using TP3.N release" -Verbose
93-
$bridgeAppConfigFile = "\\SU1FileServer\SU1_Infrastructure_1\ASResourceProvider\Config\AzureBridge.IdentityApplication.Configuration.json"
94-
}
95-
elseif($versionInfo.Version -eq "1.0.170225.2")
96-
{
97-
Write-Verbose -Message "Using TP3.O release" -Verbose
98-
$bridgeAppConfigFile = "\\SU1FileServer\SU1_Infrastructure_1\ASResourceProvider\Config\ConnectionAzureArm.IdentityApplication.Configuration.json"
99-
}
100-
else
101-
{
102-
Write-Error -Message "Unsupported TP3 release" -Verbose
103-
}
90+
#
91+
# Pre-req: Obtain refresh token for Azure identity
92+
#
93+
Import-Module C:\CloudDeployment\Setup\Common\AzureADConfiguration.psm1 -ErrorAction Stop
94+
95+
$AzureCredential = New-Object System.Management.Automation.PSCredential($AzureAccountId, (ConvertTo-SecureString -String $AzureCredentialPassword -AsPlainText -Force))
96+
$AzureDirectoryTenantId = Get-TenantIdFromName -azureEnvironment $AzureEnvironment -tenantName $AzureDirectoryTenantName
97+
98+
if($AzureCredential)
99+
{
100+
Write-Verbose "Using provided Azure Credentials to get refresh token"
101+
$tenantDetails = Get-AzureADTenantDetails -AzureEnvironment $AzureEnvironment -AADDirectoryTenantName $AzureDirectoryTenantName -AADAdminCredential $AzureCredential
102+
}
103+
else
104+
{
105+
Write-Verbose "Prompt user to enter Azure Credentials to get refresh token"
106+
$tenantDetails = Get-AzureADTenantDetails -AzureEnvironment $AzureEnvironment -AADDirectoryTenantName $AzureDirectoryTenantName
107+
}
104108

109+
$refreshToken = (ConvertTo-SecureString -string $tenantDetails["RefreshToken"] -AsPlainText -Force)
105110

106111
#
107112
# Step 1: Configure Bridge identity
108113
#
109114

110-
$azureCreds = New-Object System.Management.Automation.PSCredential($azureSubscriptionOwner, $azureSubscriptionPassword)
111-
.\Configure-BridgeIdentity.ps1 -AzureCredential $azureCreds -AzureDirectoryTenantId $azureDirectory -AzureEnvironment AzureCloud -Verbose
112-
Write-Host "STEP 1: Configure local identity completed"
115+
.\Configure-BridgeIdentity.ps1 -RefreshToken $refreshToken -AzureAccountId $AzureAccountId -AzureDirectoryTenantName $AzureDirectoryTenantName -AzureEnvironment "AzureCloud" -Verbose
116+
Write-Verbose "Configure Bridge identity completed"
113117

114118
#
115119
# Step 2: Create new registration request
116120
#
117121

122+
$bridgeAppConfigFile = "\\SU1FileServer\SU1_Infrastructure_1\ASResourceProvider\Config\AzureBridge.IdentityApplication.Configuration.json"
118123
$registrationOutputFile = "c:\temp\registration.json"
119124
.\New-RegistrationRequest.ps1 -BridgeAppConfigFile $bridgeAppConfigFile -RegistrationRequestOutputFile $registrationOutputFile -Verbose
120-
Read-Host "STEP 2: Registration request completed. Re-enter your Azure subscription credentials in the next step. Note: Step 3 can be run from a different machine that is connected to Azure. Press Enter to continue and run step 3 from this machine."
125+
Write-Verbose "New registration request completed"
121126

122127
#
123128
# Step 3: Register Azure Stack with Azure
124129
#
125-
New-Item -ItemType Directory -Force -Path "C:\temp"
130+
126131
$registrationRequestFile = "c:\temp\registration.json"
127132
$registrationOutputFile = "c:\temp\registrationOutput.json"
128-
129-
Login-AzureRmAccount -EnvironmentName AzureCloud
130-
Select-AzureRmSubscription -SubscriptionId $azureSubscriptionId
131-
132-
# Ensure subscription is registered to Microsoft.AzureStack namespace in Azure
133-
Register-AzureRmResourceProvider -ProviderNamespace 'microsoft.azurestack'
134-
$result = $null
135-
$attempts = 0
136-
$maxAttempts = 20
137-
$delayInSecondsBetweenAttempts = 10
138-
do
139-
{
140-
$attempts++
141-
Write-Verbose "[CHECK] Checking for resource provider registration... (attempt $attempts of $maxAttempts)"
142-
$result = $(Get-AzureRmResourceProvider -ProviderNamespace 'microsoft.azurestack')[0].RegistrationState -EQ 'Registered'
143-
$result
144-
if ((-not $result) -and ($attempts -lt $maxAttempts))
145-
{
146-
Write-Verbose "[DELAY] Attempt $attempts failed to see provider registration, delaying for $delayInSecondsBetweenAttempts seconds before retry"
147-
Start-Sleep -Seconds $delayInSecondsBetweenAttempts
148-
}
149-
}
150-
while ((-not $result) -and ($attempts -lt $maxAttempts))
151-
152-
if (-not $result)
153-
{
154-
throw New-Object System.InvalidOperationException("Azure Bridge Resource Provider was registered but did not become routable within the alloted time")
155-
}
156-
157-
.\Register-AzureStack.ps1 -BillingModel Consumption -Syndication $marketplaceSyndication -ReportUsage $reportUsage -SubscriptionId $azureSubscriptionId -AzureAdTenantId $azureDirectory `
158-
-AzureCredential $azureCreds -AzureEnvironmentName AzureCloud -RegistrationRequestFile $registrationRequestFile -RegistrationOutputFile $registrationOutputFile -Location "westcentralus" -Verbose
159-
Read-Host "STEP 3: Register Azure Stack with Azure completed. Press Enter to continue."
133+
.\Register-AzureStack.ps1 -BillingModel PayAsYouUse -EnableSyndication -ReportUsage -SubscriptionId $azureSubscriptionId -AzureAdTenantId $AzureDirectoryTenantId `
134+
-RefreshToken $refreshToken -AzureAccountId $AzureAccountId -AzureEnvironmentName AzureCloud -RegistrationRequestFile $registrationRequestFile `
135+
-RegistrationOutputFile $registrationOutputFile -Location "westcentralus" -Verbose
136+
Write-Verbose "Register Azure Stack with Azure completed"
160137

161138
#
162-
# Step 4: Activate Azure Stack
139+
# workaround to enable syndication and usage
163140
#
164141

165-
# temporary step to adjust registration output to expected format
142+
$activationDataFile = "c:\temp\regOutput2.json"
166143
$reg = Get-Content $registrationOutputFile | ConvertFrom-Json
144+
$enablesyndication = $true
145+
$reportusage = $true
167146
$newProps = @{
168-
ObjectId = $reg.properties.ObjectId
169-
ProvisioningState = $reg.properties.provisioningState
170-
syndication = $marketplaceSyndication
171-
usagebridge = $reportUsage
147+
ObjectId = $reg.properties.ObjectId
148+
ProvisioningState = $reg.properties.provisioningState
149+
enablesyndication = $enablesyndication
150+
reportusage = $reportusage
172151
}
152+
173153
$reg.properties = $newProps
174-
$reg | ConvertTo-Json -Depth 4 | Out-File -FilePath $registrationOutputFile
154+
$reg | ConvertTo-Json -Depth 4 | Out-File -FilePath $activationDataFile
175155

176-
$regResponse = Get-Content -path $registrationOutputFile
177-
$AzureResourceManagerEndpoint = "https://management.azure.com"
156+
Write-Verbose "Activation file is at : $activationDataFile"
178157

179-
if($versionInfo.Version -ge "1.0.170308.2")
180-
{
181-
$bytes = [System.Text.Encoding]::UTF8.GetBytes($regResponse)
182-
$activationCode = [Convert]::ToBase64String($bytes)
183-
.\Activate-Bridge.ps1 -activationCode $activationCode -AzureResourceManagerEndpoint $AzureResourceManagerEndpoint -Verbose
184-
}
185-
elseif($versionInfo.Version -eq "1.0.170225.2")
186-
{
187-
$bytes = [System.Text.Encoding]::Unicode.GetBytes($regResponse)
188-
$activationCode = [Convert]::ToBase64String($bytes)
189-
.\Activate-Bridge.ps1 -activationCode $activationCode -Verbose
190-
}
191-
192-
Write-Host "STEP 4: Activate Azure Stack completed"
193-
Write-Host "Registration complete. Close and re-open the Marketplace Management blade in the admin portal."
158+
#
159+
# Step 4: Activate Azure Stack
160+
#
161+
$regResponse = Get-Content -path $activationDataFile
162+
$bytes = [System.Text.Encoding]::UTF8.GetBytes($regResponse)
163+
$activationCode = [Convert]::ToBase64String($bytes)
164+
165+
.\Activate-Bridge.ps1 -activationCode $activationCode -AzureResourceManagerEndpoint $AzureResourceManagerEndpoint -Verbose
166+
Write-Verbose "Azure Stack activation completed"

0 commit comments

Comments
 (0)