Skip to content

Commit 4fed05d

Browse files
chore: streamline logging in apim, appservice & az active dir (#348)
* chore: streamline logging in apim, appservice & az active dir * pr-fix: correct exception messages in unit test assertions * Update src/Arcus.Scripting.ActiveDirectory/Scripts/Add-AzADAppRoleAssignment.ps1 Co-authored-by: Pim Simons <32359437+pim-simons@users.noreply.github.com> * pr-sug: streamline logging parameters * pr-fix: update w/ more streamlined logging params * pr-sug: use green color for all succesful operations Co-authored-by: Pim Simons <32359437+pim-simons@users.noreply.github.com>
1 parent 145fc22 commit 4fed05d

18 files changed

Lines changed: 103 additions & 99 deletions

src/Arcus.Scripting.ActiveDirectory/Scripts/Add-AzADAppRoleAssignment.ps1

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ if (!$adServicePrincipalRoleAssignTo) {
2424

2525
try {
2626
if ($adApplication.AppRole.Value -notcontains $Role) {
27-
Write-Host "Active Directory Application '$($adApplication.DisplayName)' does not contain the role '$Role', adding the role"
27+
Write-Verbose "Active Directory Application '$($adApplication.DisplayName)' does not contain the role '$Role', adding the role"
2828

2929
$newRole = @{
3030
"DisplayName" = $Role
@@ -38,11 +38,11 @@ try {
3838
$adApplication.AppRole += $newRole
3939

4040
Update-AzADApplication -ObjectId $adApplication.Id -AppRole $adApplication.AppRole
41-
Write-Host "Added role '$Role' to Active Directory Application '$($adApplication.DisplayName)'"
41+
Write-Host "Added role '$Role' to Active Directory Application '$($adApplication.DisplayName)'" -ForegroundColor White
4242

4343
$currentAppRole = $newRole
4444
} else {
45-
Write-Host "Active Directory Application '$($adApplication.DisplayName)' already contains the role '$Role'"
45+
Write-Host "Active Directory Application '$($adApplication.DisplayName)' already contains the role '$Role'" -ForegroundColor Yellow
4646
$currentAppRole = $adApplication.AppRole | Where-Object Value -eq $Role
4747
}
4848

@@ -51,7 +51,7 @@ try {
5151
$updatedAdServicePrincipal = Get-MgServicePrincipal -ServicePrincipalId $adServicePrincipal.Id
5252

5353
while ($updatedAdServicePrincipal.AppRoles.Value -notcontains $Role -and $counter -lt 10) {
54-
Write-Host "Role '$Role' has been added to Active Directory Application '$($adApplication.DisplayName)' but not yet available for use, waiting 10 seconds to retry..."
54+
Write-Versbose "Role '$Role' has been added to Active Directory Application '$($adApplication.DisplayName)' but not yet available for use, waiting 10 seconds to retry..."
5555
Start-Sleep -Seconds 10
5656
$counter++
5757
$updatedAdServicePrincipal = Get-MgServicePrincipal -ServicePrincipalId $adServicePrincipal.Id
@@ -62,9 +62,9 @@ try {
6262
}
6363

6464
$newRoleAssignment = New-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $adServicePrincipalRoleAssignTo.Id -PrincipalId $adServicePrincipalRoleAssignTo.Id -ResourceId $adServicePrincipal.Id -AppRoleId $currentAppRole.Id
65-
Write-Host "Role Assignment for the role '$Role' added to the Active Directory Application '$($adApplicationRoleAssignTo.DisplayName)'"
65+
Write-Host "Role Assignment for the role '$Role' added to the Active Directory Application '$($adApplicationRoleAssignTo.DisplayName)'" -ForegroundColor Green
6666
} else {
67-
Write-Host "Active Directory Application '$($adApplicationRoleAssignTo.DisplayName)' already contains a role assignment for the role '$Role'"
67+
Write-Host "Active Directory Application '$($adApplicationRoleAssignTo.DisplayName)' already contains a role assignment for the role '$Role'" -ForegroundColor Yellow
6868
}
6969
} catch {
7070
throw "Adding the role '$Role' for the Active Directory Application with ClientId '$ClientId' failed. Details: $($_.Exception.Message)"

src/Arcus.Scripting.ActiveDirectory/Scripts/List-AzADAppRoleAssignments.ps1

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@ param(
55

66
$adApplication = Get-AzADApplication -Filter "AppId eq '$ClientId'"
77
if (!$adApplication) {
8-
throw "Active Directory Application for the ClientId '$ClientId' could not be found"
8+
throw "Active Directory Application for the ClientId '$ClientId' could not be found"
99
}
1010
$adServicePrincipal = Get-AzADServicePrincipal -Filter "AppId eq '$ClientId'"
1111
if (!$adServicePrincipal) {
12-
throw "Active Directory Service Principal for the ClientId '$ClientId' could not be found"
12+
throw "Active Directory Service Principal for the ClientId '$ClientId' could not be found"
1313
}
1414

1515
if ($RolesAssignedToClientId -ne '') {
16-
$adApplicationRolesAssignedTo = Get-AzADApplication -Filter "AppId eq '$RolesAssignedToClientId'"
16+
$adApplicationRolesAssignedTo = Get-AzADApplication -Filter "AppId eq '$RolesAssignedToClientId'"
1717
if (!$adApplicationRolesAssignedTo) {
18-
throw "Active Directory Application for the ClientId '$RolesAssignedToClientId' could not be found"
18+
throw "Active Directory Application for the ClientId '$RolesAssignedToClientId' could not be found"
1919
}
2020
$adServicePrincipalRolesAssignedTo = Get-AzADServicePrincipal -Filter "AppId eq '$RolesAssignedToClientId'"
2121
if (!$adServicePrincipalRolesAssignedTo) {
22-
throw "Active Directory Service Principal for the ClientId '$RolesAssignedToClientId' could not be found"
22+
throw "Active Directory Service Principal for the ClientId '$RolesAssignedToClientId' could not be found"
2323
}
2424
}
2525

2626
try {
2727
if ($adApplication.AppRole.Count -eq 0)
2828
{
29-
Write-Host "No roles found"
29+
Write-Host "No roles found in Active Directory Application '$($adApplication.DisplayName)'" -ForegroundColor Yellow
3030
}
3131

3232
foreach ($appRole in $adApplication.AppRole) {
@@ -41,11 +41,11 @@ try {
4141
foreach ($serviceAppRoleAssignment in $appRoleAssignments) {
4242
$servicePrincipal = Get-AzADServicePrincipal -ObjectId $serviceAppRoleAssignment.PrincipalId
4343
if ($servicePrincipal -ne $null) {
44-
Write-Host "Role '$($appRole.Value)' is assigned to the Active Directory Application '$($serviceAppRoleAssignment.PrincipalDisplayName)' with ID '$($servicePrincipal.AppId)'" -ForegroundColor White
44+
Write-Host "Role '$($appRole.Value)' is assigned to the Active Directory Application '$($serviceAppRoleAssignment.PrincipalDisplayName)' with ID '$($servicePrincipal.AppId)'" -ForegroundColor Green
4545
}
4646
}
4747
} else {
48-
Write-Host "No role assignments found"
48+
Write-Host "No role assignments found in Active Directory Application '$($adApplication.DisplayName)'" -ForegroundColor Yellow
4949
}
5050
}
5151
} catch {

src/Arcus.Scripting.ActiveDirectory/Scripts/Remove-AzADAppRoleAssignment.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ if (!$adServicePrincipalRoleRemoveFrom) {
2525

2626
try {
2727
if ($adApplication.AppRole.Value -notcontains $Role) {
28-
Write-Host "Active Directory Application '$($adApplication.DisplayName)' does not contain the role '$Role', skipping removal"
28+
Write-Host "Active Directory Application '$($adApplication.DisplayName)' does not contain the role '$Role', skipping removal" -ForegroundColor Yellow
2929
} else {
3030
$appRole = $adApplication.AppRole | Where-Object {($_.DisplayName -eq $Role)}
3131
$appRoleAssignment = Get-MgServicePrincipalAppRoleAssignedTo -ServicePrincipalId $adServicePrincipal.Id | Where-Object {($_.AppRoleId -eq $appRole.Id) -and ($_.PrincipalId -eq $adServicePrincipalRoleRemoveFrom.Id)}
@@ -34,7 +34,7 @@ try {
3434
Remove-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $adServicePrincipalRoleRemoveFrom.Id -AppRoleAssignmentId $appRoleAssignment.Id
3535
Write-Host "Role assignment for '$Role' has been removed from Active Directory Application '$($adApplicationRoleRemoveFrom.DisplayName)'"
3636
} else {
37-
Write-Host "Role '$Role' is not assigned to Active Directory Application '$($adApplicationRoleRemoveFrom.DisplayName)', skipping role assignment removal"
37+
Write-Host "Role '$Role' is not assigned to Active Directory Application '$($adApplicationRoleRemoveFrom.DisplayName)', skipping role assignment removal" -ForegroundColor Yellow
3838
}
3939

4040
if ($RemoveRoleIfNoAssignmentsAreLeft) {
@@ -49,7 +49,7 @@ try {
4949
$appRoles = $adApplication.AppRole | Where-Object Id -ne $appRole.Id
5050
if ($appRoles) {
5151
Update-AzADApplication -ObjectId $adApplication.Id -AppRole $appRoles
52-
Write-Host "Role '$Role' removed from Active Directory Application '$($adApplication.DisplayName)' as no more role assignments were left and the option 'RemoveRoleIfNoAssignmentsAreLeft' is set"
52+
Write-Host "Role '$Role' with App Role '$appRoles' removed from Active Directory Application '$($adApplication.DisplayName)' as no more role assignments were left and the option 'RemoveRoleIfNoAssignmentsAreLeft' is set"
5353
} else {
5454
Update-AzADApplication -ObjectId $adApplication.Id -AppRole @()
5555
Write-Host "Role '$Role' removed from Active Directory Application '$($adApplication.DisplayName)' as no more role assignments were left and the option 'RemoveRoleIfNoAssignmentsAreLeft' is set"

src/Arcus.Scripting.ApiManagement/Scripts/Backup-AzApiManagementService.ps1

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ param(
99
[Parameter(Mandatory = $false)][Microsoft.Azure.Commands.Common.Authentication.Abstractions.Core.IAzureContextContainer] $DefaultProfile = $null
1010
)
1111

12-
Write-Host "Getting Azure storage account key..."
12+
Write-Verbose "Getting Azure storage account key for storage account '$($StorageAccountName)' in resource group '$($StorageAccountResourceGroupName)'..."
1313
$storageKeys = Get-AzStorageAccountKey -ResourceGroupName $StorageAccountResourceGroupName -StorageAccountName $StorageAccountName
1414

1515
if ($storageKeys -eq $null -or $storageKeys.count -eq 0) {
16-
Write-Error "Cannot backup API Management service because no access keys found for storage account '$StorageAccountName'"
16+
Write-Error "Cannot backup API Management service because no access keys found for storage account '$StorageAccountName' in resource group '$($StorageAccountResourceGroupName)'"
1717
} else {
18-
Write-Host "Got Azure storage key!"
18+
Write-Host "Got Azure storage key for storage account '$($StorageAccountName)' in resource group '$($StorageAccountResourceGroupName)'!" -ForegroundColor Green
1919
$storageKey = $storageKeys[0]
2020

21-
Write-Host "Create new Azure storage context with storage key..."
21+
Write-Verbose "Create new Azure storage context for storage account '$($StorageAccountName)' with storage key..."
2222
$storageContext = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $storageKey.Value
23-
Write-Host "New Azure storage context with storage key created!"
23+
Write-Host "New Azure storage context for storage account '$($StorageAccountName)' with storage key created!" -ForegroundColor Green
2424

25-
Write-Host "Start backing up API management service..."
25+
Write-Verbose "Start backing up Azure API management service '$($ServiceName)' in resource group '$($ResourceGroupName)'..."
2626
if ($BlobName -ne $null) {
2727
if ($PassThru) {
2828
if ($DefaultProfile -ne $null) {
@@ -53,5 +53,5 @@ if ($storageKeys -eq $null -or $storageKeys.count -eq 0) {
5353
}
5454
}
5555

56-
Write-Host "API management service is backed-up!"
56+
Write-Host "Azure API management service '$($ServiceName)' in resource group '$($ResourceGroupName)' is backed-up!" -ForegroundColor Green
5757
}

src/Arcus.Scripting.ApiManagement/Scripts/Create-AzApiManagementApiOperation.ps1

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@ param(
1212

1313
$apim = Get-AzApiManagement -ResourceGroupName $ResourceGroupName -Name $ServiceName
1414
if ($apim -eq $null) {
15-
throw "Unable to find the Azure API Management Instance $ServiceName in resource group $ResourceGroupName"
15+
throw "Unable to find the Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'"
1616
}
1717
$apimContext = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName
1818

1919
New-AzApiManagementOperation -Context $apimContext -ApiId $ApiId -OperationId $OperationId -Name $OperationName -Method $Method -UrlTemplate $UrlTemplate -Description $Description
20-
Write-Host "New API operation '$OperationName' on API Management instance was added."
20+
Write-Host "New API operation '$OperationName' was added on Azure API Management service '$($ServiceName)' in resource group '$($ResourceGroupName)'"
2121

2222
if($OperationId -eq "" -or $PolicyFilePath -eq "")
2323
{
24-
Write-Host "No policy has been defined."
24+
Write-Host "No policy has been defined for Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'" -ForegroundColor Yellow
2525
}
2626
else
2727
{
28-
Write-Host "Updating policy of the operation '$OperationId' in API '$ApiId'"
28+
Write-Verbose "Updating policy of the operation '$OperationId' in API '$ApiId' of the Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'..."
2929
Set-AzApiManagementPolicy -Context $apimContext -ApiId $ApiId -OperationId $OperationId -PolicyFilePath $PolicyFilePath
30+
Write-Host "Updated policy of the operation '$OperationId' in API '$ApiId' of the Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'" -ForegroundColor Green
3031
}

src/Arcus.Scripting.ApiManagement/Scripts/Create-AzApiManagementUserAccount.ps1

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ param(
1616

1717
$apimContext = Get-AzApiManagement -ResourceGroupName $ResourceGroupName -Name $ServiceName
1818
if ($apimContext -eq $null) {
19-
throw "Unable to find the Azure API Management Instance $ServiceName in resource group $ResourceGroupName"
19+
throw "Unable to find the Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'"
2020
}
2121

2222
if ($SubscriptionId -eq "" -or $AccessToken -eq "") {
@@ -34,11 +34,11 @@ try
3434
{
3535
if($ConfirmationType -eq 'invite')
3636
{
37-
Write-Host "Attempting to invite $FirstName $LastName ($mailAddress)"
37+
Write-Verbose "Attempting to invite $FirstName $LastName ($mailAddress) for Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'"
3838
}
3939
else
4040
{
41-
Write-Host "Attempting to create account for $FirstName $LastName ($mailAddress)"
41+
Write-Verbose "Attempting to create account for $FirstName $LastName ($mailAddress) for Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'"
4242
}
4343

4444
$jsonRequest = ConvertTo-Json -Depth 3 @{
@@ -67,19 +67,19 @@ try
6767

6868
if($ConfirmationType -eq 'invite')
6969
{
70-
Write-Host "Invitation has been sent to $FirstName $LastName ($mailAddress)"
70+
Write-Host "Invitation has been sent to $FirstName $LastName ($mailAddress) for Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'" -ForegroundColor Green
7171
}
7272
else
7373
{
74-
Write-Host "Account has been created for $FirstName $LastName ($mailAddress)"
74+
Write-Host "Account has been created for $FirstName $LastName ($mailAddress) for Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'" -ForegroundColor Green
7575
if($Password -eq $null -or $Password -eq ""){
76-
Write-Host "Since no password was provided, one has been generated. Please advise the user to change this password the first time logging in"
76+
Write-Host "Since no password was provided, one has been generated. Please advise the user to change this password the first time logging in for the Azure API Management service '$($ServiceName)' in resource group '$($ResourceGroupName)'" -ForegroundColor Yellow
7777
}
7878
}
7979

8080
return $UserId
8181
}
8282
catch {
8383
Write-Host $_
84-
throw "Failed to create an account for $FirstName $LastName ($MailAddress) in the APIM instance $ServiceName"
84+
throw "Failed to create an account for $FirstName $LastName ($MailAddress) for Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'"
8585
}

src/Arcus.Scripting.ApiManagement/Scripts/Import-AzApiManagementApiPolicy.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ param(
77

88
$apim = Get-AzApiManagement -ResourceGroupName $ResourceGroupName -Name $ServiceName
99
if ($apim -eq $null) {
10-
throw "Unable to find the Azure API Management Instance $ServiceName in resource group $ResourceGroupName"
10+
throw "Unable to find the Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'"
1111
}
1212
$apimContext = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName
1313

14-
Write-Host "Updating policy of API '$ApiId'"
14+
Write-Verbose "Updating policy of API '$ApiId' for the Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'"
1515
$result = Set-AzApiManagementPolicy -Context $apimContext -ApiId $ApiId -PolicyFilePath $PolicyFilePath -PassThru
1616
if ($result) {
17-
Write-Host "Successfully updated API policy"
17+
Write-Host "Successfully updated API policy for the Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'" -ForegroundColor Green
1818
} else {
19-
throw "Failed to update API policy, please check parameters"
19+
throw "Failed to update API policy for the Azure API Management service '$ServiceName' in resource group '$ResourceGroupName', please check parameters"
2020
}

src/Arcus.Scripting.ApiManagement/Scripts/Import-AzApiManagementOperationPolicy.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ param(
88

99
$apim = Get-AzApiManagement -ResourceGroupName $ResourceGroupName -Name $ServiceName
1010
if ($apim -eq $null) {
11-
throw "Unable to find the Azure API Management Instance $ServiceName in resource group $ResourceGroupName"
11+
throw "Unable to find the Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'"
1212
}
1313
$apimContext = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName
1414

15-
Write-Host "Updating policy of the operation '$OperationId' in API '$ApiId'"
15+
Write-Verbose "Updating policy of the operation '$OperationId' in API '$ApiId' for the Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'"
1616
$result = Set-AzApiManagementPolicy -Context $apimContext -ApiId $ApiId -OperationId $OperationId -PolicyFilePath $PolicyFilePath -PassThru
1717
if ($result) {
18-
Write-Host "Successfully updated the operation policy"
18+
Write-Host "Successfully updated the operation policy for the Azure API Management service $ServiceName in resource group $ResourceGroupName" -ForegroundColor Green
1919
} else {
20-
throw "Failed to update the operation policy, please check parameters"
20+
throw "Failed to update the operation policy for the Azure API Management service '$ServiceName' in resource group '$ResourceGroupName', please check parameters"
2121
}

src/Arcus.Scripting.ApiManagement/Scripts/Import-AzApiManagementProductPolicy.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ param(
77

88
$apim = Get-AzApiManagement -ResourceGroupName $ResourceGroupName -Name $ServiceName
99
if ($apim -eq $null) {
10-
throw "Unable to find the Azure API Management Instance $ServiceName in resource group $ResourceGroupName"
10+
throw "Unable to find the Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'"
1111
}
1212
$apimContext = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName
1313

14-
Write-Host "Updating policy of product '$ProductId'"
14+
Write-Verbose "Updating policy of product '$ProductId' for the Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'"
1515
$result = Set-AzApiManagementPolicy -Context $apimContext -ProductId $ProductId -PolicyFilePath $PolicyFilePath -PassThru
1616
if ($result) {
17-
Write-Host "Successfully updated the product policy"
17+
Write-Host "Successfully updated the product policy for the Azure API Management service $ServiceName in resource group $ResourceGroupName" -ForegroundColor Green
1818
} else {
19-
throw "Failed to update the product policy"
19+
throw "Failed to update the product policy for the Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'"
2020
}

0 commit comments

Comments
 (0)