Skip to content

Commit c40dd00

Browse files
author
BradleyBartlett
committed
confirm connection to JeaComputerName before attempting registration. Wrap AAD calls in retries
1 parent 0f6a3a4 commit c40dd00

1 file changed

Lines changed: 31 additions & 8 deletions

File tree

Registration/RegisterWithAzure.ps1

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ param(
139139

140140

141141
#requires -Version 4.0
142-
#requires -Modules @{ModuleName = "AzureRM.Profile" ; ModuleVersion = "2.7"}
143-
#requires -Modules @{ModuleName = "AzureRM.Resources" ; ModuleVersion = "3.7"}
142+
#requires -Modules @{ModuleName = "AzureRM.Profile" ; ModuleVersion = "1.0.4.4"}
143+
#requires -Modules @{ModuleName = "AzureRM.Resources" ; ModuleVersion = "1.0.4.4"}
144144

145145
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
146146
$VerbosePreference = [System.Management.Automation.ActionPreference]::Continue
@@ -209,8 +209,30 @@ function Connect-AzureAccount
209209
Write-Verbose "Logging in to Azure."
210210
$connection = Connect-AzureAccount -SubscriptionId $AzureSubscriptionId -AzureEnvironment $AzureEnvironmentName
211211

212-
Write-Verbose "Initializing privileged JEA session."
213-
$session = New-PSSession -ComputerName $JeaComputerName -ConfigurationName PrivilegedEndpoint -Credential $CloudAdminCredential
212+
$currentAttempt = 0
213+
$maxAttempts = 3
214+
$sleepSeconds = 10
215+
$opSuccessful = $false
216+
do{
217+
try
218+
{
219+
Write-Verbose "Initializing privileged JEA session."
220+
$session = New-PSSession -ComputerName $JeaComputerName -ConfigurationName PrivilegedEndpoint -Credential $CloudAdminCredential
221+
$opSuccessful = $true
222+
}
223+
catch
224+
{
225+
Write-Verbose "Creation of session with $JeaComputerName failed:`r`n$($_.Exception.Message)"
226+
Write-Verbose "Waiting $sleepSeconds seconds and trying again..."
227+
$currentAttempt++
228+
Start-Sleep -Seconds $sleepSeconds
229+
if ($currentAttempt -eq $maxAttempts)
230+
{
231+
throw $_.Exception
232+
}
233+
}
234+
}while ((-not $opSuccessful) -and ($currentAttempt -lt $maxAttempts))
235+
214236

215237
try
216238
{
@@ -223,8 +245,7 @@ try
223245

224246
Write-Verbose -Message "Running registration on build $($stampInfo.StampVersion). Cloud Id: $($stampInfo.CloudID), Deployment Id: $($stampInfo.DeploymentID)"
225247

226-
$tenantId = $connection.TenantId
227-
Write-Verbose "Creating Azure Active Directory service principal in tenant: $tenantId."
248+
$tenantId = $connection.TenantId
228249
$refreshToken = $connection.Token.RefreshToken
229250

230251
$currentAttempt = 0
@@ -234,6 +255,7 @@ try
234255
do{
235256
try
236257
{
258+
Write-Verbose "Creating Azure Active Directory service principal in tenant: $tenantId."
237259
$servicePrincipal = Invoke-Command -Session $session -ScriptBlock { New-AzureBridgeServicePrincipal -RefreshToken $using:refreshToken -AzureEnvironment $using:AzureEnvironmentName -TenantId $using:tenantId }
238260
$opSuccessful = $true
239261
}
@@ -251,20 +273,21 @@ try
251273
}while ((-not $opSuccessful) -and ($currentAttempt -lt $maxAttempts))
252274

253275

254-
Write-Verbose "Creating registration token."
276+
255277
$currentAttempt = 0
256278
$maxAttempts = 3
257279
$opSuccessful = $false
258280
$sleepSeconds = 10
259281
do{
260282
try
261283
{
284+
Write-Verbose "Creating registration token."
262285
$registrationToken = Invoke-Command -Session $session -ScriptBlock { New-RegistrationToken -BillingModel $using:BillingModel -MarketplaceSyndicationEnabled:$using:MarketplaceSyndicationEnabled -UsageReportingEnabled:$using:UsageReportingEnabled -AgreementNumber $using:AgreementNumber }
263286
$opSuccessful = $true
264287
}
265288
catch
266289
{
267-
Write-Verbose "Creation of service principal failed:`r`n$($_.Exception.Message)"
290+
Write-Verbose "Creation of registration token failed:`r`n$($_.Exception.Message)"
268291
Write-Verbose "Waiting $sleepSeconds seconds and trying again..."
269292
$currentAttempt++
270293
Start-Sleep -Seconds $sleepSeconds

0 commit comments

Comments
 (0)