Skip to content

Commit 219c7ed

Browse files
author
BradleyBartlett
committed
Improve registration logging
1 parent 483f1e6 commit 219c7ed

File tree

1 file changed

+113
-23
lines changed

1 file changed

+113
-23
lines changed

Registration/RegisterWithAzure.psm1

Lines changed: 113 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,33 @@ You must also have access to an account / directory that is an owner or contribu
99
1010
#>
1111

12-
# Create log folder / prevent duplicate logs
13-
$LogFolder = "$env:SystemDrive\MASLogs"
14-
if (-not (Test-Path $LogFolder))
12+
# Generate log file(s)
13+
function New-RegistrationLogFile
1514
{
16-
New-Item -Path $LogFolder -ItemType Directory -Force | Out-Null
17-
}
18-
if(-not $AzureRegistrationLog)
19-
{
20-
$AzureRegistrationLog = "$LogFolder\AzureStack.AzureRegistration.$(Get-Date -Format yyyy-MM-dd.HH-mm-ss).log"
21-
$null = New-Item -Path $AzureRegistrationLog -ItemType File -Force
15+
[CmdletBinding()]
16+
param(
17+
[Parameter(Mandatory=$false)]
18+
[String] $LogDate = (Get-Date -Format yyyy-MM-dd),
19+
20+
[Parameter(Mandatory=$false)]
21+
[String] $RegistrationFunction = 'RegistrationOperation'
22+
)
23+
24+
# Create log folder
25+
$LogFolder = "$env:SystemDrive\MASLogs\Registration"
26+
if (-not (Test-Path $LogFolder))
27+
{
28+
New-Item -Path $LogFolder -ItemType Directory -Force | Out-Null
29+
}
30+
31+
$logFilePath = "$LogFolder\AzureStack.Activation.$RegistrationFunction-$LogDate.log"
32+
Write-Verbose "Writing logs to file: $logFilePath"
33+
if (-not (Test-Path $logFilePath -PathType Leaf))
34+
{
35+
$null = New-Item -Path $logFilePath -ItemType File -Force
36+
}
37+
38+
$Script:registrationLog = $logFilePath
2239
}
2340

2441
################################################################
@@ -169,6 +186,8 @@ function Set-AzsRegistration{
169186
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
170187
$VerbosePreference = [System.Management.Automation.ActionPreference]::Continue
171188

189+
New-RegistrationLogFile -RegistrationFunction $PSCmdlet.MyInvocation.MyCommand.Name
190+
172191
Log-Output "*********************** Begin log: $($PSCmdlet.MyInvocation.MyCommand.Name) ***********************`r`n"
173192

174193
$azureAccountInfo = Get-AzureAccountInfo -AzureContext $AzureContext
@@ -201,7 +220,7 @@ function Set-AzsRegistration{
201220
New-RBACAssignment -SubscriptionId $AzureContext.Subscription.SubscriptionId -ResourceGroupName $ResourceGroupName -RegistrationName $RegistrationName -ServicePrincipal $servicePrincipal
202221

203222
# Activate AzureStack syndication / usage reporting features
204-
$activationKey = Get-AzsActivationkey -ResourceGroupName $ResourceGroupName -RegistrationName $RegistrationName
223+
$activationKey = Get-AzsActivationkey -ResourceGroupName $ResourceGroupName -RegistrationName $RegistrationName -ConnectedScenario
205224
Log-Output "Activating Azure Stack (this may take up to 10 minutes to complete)."
206225
Activate-AzureStack -Session $session -ActivationKey $ActivationKey
207226

@@ -278,6 +297,8 @@ function Remove-AzsRegistration{
278297
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
279298
$VerbosePreference = [System.Management.Automation.ActionPreference]::Continue
280299

300+
New-RegistrationLogFile -RegistrationFunction $PSCmdlet.MyInvocation.MyCommand.Name
301+
281302
Log-Output "*********************** Begin log: $($PSCmdlet.MyInvocation.MyCommand.Name) ***********************`r`n"
282303

283304
$azureAccountInfo = Get-AzureAccountInfo -AzureContext $AzureContext
@@ -390,6 +411,8 @@ Function Get-AzsRegistrationToken{
390411
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
391412
$VerbosePreference = [System.Management.Automation.ActionPreference]::Continue
392413

414+
New-RegistrationLogFile -RegistrationFunction $PSCmdlet.MyInvocation.MyCommand.Name
415+
393416
if(($BillingModel -eq 'Capacity') -and ([String]::IsNullOrEmpty($AgreementNumber)))
394417
{
395418
Log-Throw -Message "Agreement number is null or empty when BillingModel is set to Capacity" -CallingFunction $PSCmdlet.MyInvocation.MyCommand.Name
@@ -502,6 +525,8 @@ Function Register-AzsEnvironment{
502525
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
503526
$VerbosePreference = [System.Management.Automation.ActionPreference]::Continue
504527

528+
New-RegistrationLogFile -RegistrationFunction $PSCmdlet.MyInvocation.MyCommand.Name
529+
505530
Log-Output "*********************** Begin log: $($PSCmdlet.MyInvocation.MyCommand.Name) ***********************`r`n"
506531

507532
$azureAccountInfo = Get-AzureAccountInfo -AzureContext $AzureContext
@@ -587,6 +612,8 @@ Function UnRegister-AzsEnvironment{
587612
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
588613
$VerbosePreference = [System.Management.Automation.ActionPreference]::Continue
589614

615+
New-RegistrationLogFile -RegistrationFunction $PSCmdlet.MyInvocation.MyCommand.Name
616+
590617
Log-Output "*********************** Begin log: $($PSCmdlet.MyInvocation.MyCommand.Name) ***********************`r`n"
591618

592619
if (-not $RegistrationName)
@@ -662,9 +689,12 @@ Function Get-AzsRegistrationName{
662689
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
663690
$VerbosePreference = [System.Management.Automation.ActionPreference]::Continue
664691

692+
New-RegistrationLogFile -RegistrationFunction $PSCmdlet.MyInvocation.MyCommand.Name
693+
665694
Log-Output "*********************** Begin log: $($PSCmdlet.MyInvocation.MyCommand.Name) ***********************`r`n"
666695
$session = Initialize-PrivilegedEndpointSession -PrivilegedEndpoint $PrivilegedEndpoint -PrivilegedEndpointCredential $PrivilegedEndpointCredential -Verbose
667696
$registrationName = Get-RegistrationName -Session $session
697+
Log-Output "Registration name: $registrationName"
668698
Log-Output "*********************** End log: $($PSCmdlet.MyInvocation.MyCommand.Name) ***********************`r`n`r`n"
669699
return $registrationName
670700
}
@@ -703,12 +733,20 @@ Function Get-AzsActivationKey{
703733
[String] $ResourceGroupName = 'azurestack',
704734

705735
[Parameter(Mandatory = $false)]
706-
[String] $KeyOutputFilePath
736+
[String] $KeyOutputFilePath,
737+
738+
[Parameter(Mandatory = $false)]
739+
[Switch] $ConnectedScenario
707740
)
708741

709742
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
710743
$VerbosePreference = [System.Management.Automation.ActionPreference]::Continue
711744

745+
if (-not $ConnectedScenario)
746+
{
747+
New-RegistrationLogFile -RegistrationFunction $PSCmdlet.MyInvocation.MyCommand.Name
748+
}
749+
712750
Log-Output "*********************** Begin log: $($PSCmdlet.MyInvocation.MyCommand.Name) ***********************`r`n"
713751

714752
$azureAccountInfo = Get-AzureAccountInfo -AzureContext $AzureContext
@@ -799,6 +837,8 @@ param(
799837
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
800838
$VerbosePreference = [System.Management.Automation.ActionPreference]::Continue
801839

840+
New-RegistrationLogFile -RegistrationFunction $PSCmdlet.MyInvocation.MyCommand.Name
841+
802842
Log-Output "*********************** Begin log: $($PSCmdlet.MyInvocation.MyCommand.Name) ***********************`r`n"
803843

804844
$session = Initialize-PrivilegedEndpointSession -PrivilegedEndpoint $PrivilegedEndpoint -PrivilegedEndpointCredential $PrivilegedEndpointCredential -Verbose
@@ -842,13 +882,16 @@ Function Remove-AzsActivationResource{
842882
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
843883
$VerbosePreference = [System.Management.Automation.ActionPreference]::Continue
844884

885+
New-RegistrationLogFile -RegistrationFunction $PSCmdlet.MyInvocation.MyCommand.Name
886+
845887
Log-Output "*********************** Begin log: $($PSCmdlet.MyInvocation.MyCommand.Name) ***********************`r`n"
846888

847889
$session = Initialize-PrivilegedEndpointSession -PrivilegedEndpoint $PrivilegedEndpoint -PrivilegedEndpointCredential $PrivilegedEndpointCredential -Verbose
848890

849891
try
850892
{
851893
$AzureStackStampInfo = Invoke-Command -Session $session -ScriptBlock { Get-AzureStackStampInformation }
894+
Log-Output "Logging in to AzureStack administrator account. TenantId: $($AzureStackStampInfo.AADTenantID) Environment: 'AzureStack'"
852895
Login-AzureRmAccount -TenantId $AzureStackStampInfo.AADTenantID -Environment 'AzureStack'
853896
$azureStackContext = Get-AzureRmContext
854897

@@ -1358,13 +1401,46 @@ function Get-AzureAccountInfo{
13581401
$AzureSubscription = $AzureContext.Subscription
13591402

13601403
$tokens = @()
1361-
try{$tokens += [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.TokenCache.ReadItems()}catch{}
1362-
try{$tokens += [Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache]::DefaultShared.ReadItems()}catch{}
1363-
try{$tokens += $AzureContext.TokenCache.ReadItems()}catch{}
1404+
$exceptions = @()
1405+
try{
1406+
$tokens += $AzureContext.TokenCache.ReadItems()
1407+
if ($tokens.Count -eq 0)
1408+
{
1409+
throw "No Tokens collected."
1410+
}
1411+
}
1412+
catch{
1413+
Log-Warning "Tokens not collected using method: `$AzureContext.TokenCache.ReadItems() `r`n$_"
1414+
$exceptions += $_.Exception
1415+
}
13641416

1365-
if (-not $tokens -or ($tokens.Count -le 0))
1417+
try{
1418+
$tokens += [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.TokenCache.ReadItems()
1419+
if ($tokens.Count -eq 0)
1420+
{
1421+
throw "No Tokens collected."
1422+
}
1423+
}
1424+
catch{
1425+
Log-Warning "Tokens not collected using method: [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.TokenCache.ReadItems() `r`n$_"
1426+
$exceptions += $_.Exception
1427+
}
1428+
1429+
try{
1430+
$tokens += [Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache]::DefaultShared.ReadItems()
1431+
if ($tokens.Count -eq 0)
1432+
{
1433+
throw "No Tokens collected."
1434+
}
1435+
}
1436+
catch{
1437+
Log-Warning "Tokens not collected using method: [Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache]::DefaultShared.ReadItems() `r`n$_"
1438+
$exceptions += $_.Exception
1439+
}
1440+
1441+
if ($tokens.Count -lt 1)
13661442
{
1367-
Log-Throw -Message "Token cache is empty `r`n$($_)" -CallingFunction $PSCmdlet.MyInvocation.MyCommand.Name
1443+
Log-Throw -Message "Token cache is empty `r`n" -CallingFunction $PSCmdlet.MyInvocation.MyCommand.Name -ExceptionObject $exceptions
13681444
}
13691445

13701446
$token = $tokens |
@@ -1611,7 +1687,7 @@ function Log-Output{
16111687
[object] $Message
16121688
)
16131689

1614-
"$(Get-Date -Format yyyy-MM-dd.hh-mm-ss): $Message" | Out-File $AzureRegistrationLog -Append
1690+
"$(Get-Date -Format yyyy-MM-dd.hh-mm-ss): $Message" | Out-File $Script:registrationLog -Append
16151691
Write-Verbose "$(Get-Date -Format yyyy-MM-dd.hh-mm-ss): $Message"
16161692
}
16171693

@@ -1631,7 +1707,7 @@ function Log-Warning{
16311707

16321708
# Write Error: line seperately otherwise out message will not contain stack trace
16331709
Log-Output "*** WARNING ***"
1634-
"$(Get-Date -Format yyyy-MM-dd.hh-mm-ss): $Message" | Out-File $AzureRegistrationLog -Append
1710+
"$(Get-Date -Format yyyy-MM-dd.hh-mm-ss): $Message" | Out-File $Script:registrationLog -Append
16351711
Write-Warning "$(Get-Date -Format yyyy-MM-dd.hh-mm-ss): $Message"
16361712
Log-Output "*** End WARNING ***"
16371713
}
@@ -1650,22 +1726,36 @@ function Log-Throw{
16501726
[Object] $Message,
16511727

16521728
[Parameter(Mandatory=$true)]
1653-
[String] $CallingFunction
1729+
[String] $CallingFunction,
1730+
1731+
[Parameter(Mandatory=$false)]
1732+
[PSObject] $ExceptionObject
16541733
)
16551734

16561735
$errorLine = "************************ Error ************************"
16571736

16581737
# Write Error line seperately otherwise out message will not contain stack trace
1659-
"$(Get-Date -Format yyyy-MM-dd.hh-mm-ss): $errorLine" | Out-File $AzureRegistrationLog -Append
1738+
"$(Get-Date -Format yyyy-MM-dd.hh-mm-ss): $errorLine" | Out-File $Script:registrationLog -Append
16601739
Write-Verbose "$(Get-Date -Format yyyy-MM-dd.hh-mm-ss): $errorLine"
16611740

16621741
Log-Output $Message
1663-
Log-Output $Message.ScriptStacktrace
1742+
if ($Message.ScriptStacktrace)
1743+
{
1744+
Log-Output $Message.ScriptStacktrace
1745+
}
1746+
1747+
if ($ExceptionObject)
1748+
{
1749+
for ($exCount = 0; $exCount -lt $ExceptionObject.Count; $exCount++)
1750+
{
1751+
Log-Output "Exception #$exCount`: $($ExceptionObject[$exCount])"
1752+
}
1753+
}
16641754

16651755
Log-OutPut "*********************** Ending registration action during $CallingFunction ***********************`r`n"
16661756

1667-
"$(Get-Date -Format yyyy-MM-dd.hh-mm-ss): Logs can be found at: $AzureRegistrationLog and \\$PrivilegedEndpoint\c$\maslogs `r`n" | Out-File $AzureRegistrationLog -Append
1668-
Write-Verbose "$(Get-Date -Format yyyy-MM-dd.hh-mm-ss): Logs can be found at: $AzureRegistrationLog and \\$PrivilegedEndpoint\c$\maslogs `r`n"
1757+
"$(Get-Date -Format yyyy-MM-dd.hh-mm-ss): Logs can be found at: $Script:registrationLog and \\$PrivilegedEndpoint\c$\maslogs `r`n" | Out-File $Script:registrationLog -Append
1758+
Write-Verbose "$(Get-Date -Format yyyy-MM-dd.hh-mm-ss): Logs can be found at: $Script:registrationLog and \\$PrivilegedEndpoint\c$\maslogs `r`n"
16691759

16701760
throw $Message
16711761
}

0 commit comments

Comments
 (0)