Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: streamline logging across modules #351

Merged
14 changes: 7 additions & 7 deletions src/Arcus.Scripting.ARM/Scripts/Inject-ArmContent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function InjectFile {
[string] $filePath
)

Write-Host "Checking file $filePath"
Write-Verbose "Checking ARM template file '$filePath' for injection tokens..."

$replaceContentDelegate = {
param($match)
Expand All @@ -58,7 +58,7 @@ function InjectFile {
# Inject content recursively first
InjectFile($fullPathOfFileToInject)

Write-Host "`t Injecting content of $fullPathOfFileToInject into $filePath"
Write-Verbose "`t Injecting content of '$fullPathOfFileToInject' into '$filePath'"

$newString = Get-Content -Path $fullPathOfFileToInject -Raw

Expand Down Expand Up @@ -98,13 +98,13 @@ function InjectFile {
if ($optionParts.Contains("InjectAsJsonObject")) {
try {
# Test if content is valid JSON
Write-Host "Test if valid JSON: $newString"
Write-Verbose "Test if valid JSON: $newString"
ConvertFrom-Json $newString

$surroundContentWithDoubleQuotes = $False
}
catch {
Write-Warning "Content to inject cannot be parsed as a JSON object!"
Write-Warning "Content to inject into ARM template file '$filePath' cannot be parsed as a JSON object!"
}
}
}
Expand All @@ -122,7 +122,7 @@ function InjectFile {
$injectionInstructionRegex = [regex] '"?\${(.+)}\$"?';
$injectionInstructionRegex.Replace($rawContents, $replaceContentDelegate) | Set-Content $filePath -NoNewline -Encoding UTF8

Write-Host "Done checking file $filePath"
Write-Host "Done checking ARM template file '$filePath' for injection tokens" -ForegroundColor Green
}


Expand All @@ -133,9 +133,9 @@ if ($false -eq $PathIsFound) {
throw "Passed along path '$Path' doesn't point to valid file path"
}

Write-Host "Starting $psScriptFileName script on path $Path"
Write-Verbose "Starting '$psScriptFileName' script on path '$Path'..."

$armTemplates = Get-ChildItem -Path $Path -Recurse -Include *.json
$armTemplates | ForEach-Object { InjectFile($_.FullName) }

Write-Host "Finished script $psScriptFileName"
Write-Host "Finished script '$psScriptFileName' on path '$Path'" -ForegroundColor Green
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ try {
$adApplication.AppRole += $newRole

Update-AzADApplication -ObjectId $adApplication.Id -AppRole $adApplication.AppRole
Write-Host "Added role '$Role' to Active Directory Application '$($adApplication.DisplayName)'" -ForegroundColor White
Write-Host "Added role '$Role' to Active Directory Application '$($adApplication.DisplayName)e'"
stijnmoreels marked this conversation as resolved.
Show resolved Hide resolved

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

Expand All @@ -64,7 +64,7 @@ try {
$newRoleAssignment = New-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $adServicePrincipalRoleAssignTo.Id -PrincipalId $adServicePrincipalRoleAssignTo.Id -ResourceId $adServicePrincipal.Id -AppRoleId $currentAppRole.Id
Write-Host "Role Assignment for the role '$Role' added to the Active Directory Application '$($adApplicationRoleAssignTo.DisplayName)'" -ForegroundColor Green
} else {
Write-Host "Active Directory Application '$($adApplicationRoleAssignTo.DisplayName)' already contains a role assignment for the role '$Role'" -ForegroundColor Yellow
Write-Warning "Active Directory Application '$($adApplicationRoleAssignTo.DisplayName)' already contains a role assignment for the role '$Role'"
}
} catch {
throw "Adding the role '$Role' for the Active Directory Application with ClientId '$ClientId' failed. Details: $($_.Exception.Message)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ if ($RolesAssignedToClientId -ne '') {
try {
if ($adApplication.AppRole.Count -eq 0)
{
Write-Host "No roles found in Active Directory Application '$($adApplication.DisplayName)'" -ForegroundColor Yellow
Write-Warning "No roles found in Active Directory Application '$($adApplication.DisplayName)'"
}

foreach ($appRole in $adApplication.AppRole) {
Expand All @@ -45,7 +45,7 @@ try {
}
}
} else {
Write-Host "No role assignments found in Active Directory Application '$($adApplication.DisplayName)'" -ForegroundColor Yellow
Write-Warning "No role assignments found in Active Directory Application '$($adApplication.DisplayName)'"
}
}
} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ if (!$adServicePrincipalRoleRemoveFrom) {

try {
if ($adApplication.AppRole.Value -notcontains $Role) {
Write-Host "Active Directory Application '$($adApplication.DisplayName)' does not contain the role '$Role', skipping removal" -ForegroundColor Yellow
Write-Warning "Active Directory Application '$($adApplication.DisplayName)' does not contain the role '$Role', skipping removal"
} else {
$appRole = $adApplication.AppRole | Where-Object {($_.DisplayName -eq $Role)}
$appRoleAssignment = Get-MgServicePrincipalAppRoleAssignedTo -ServicePrincipalId $adServicePrincipal.Id | Where-Object {($_.AppRoleId -eq $appRole.Id) -and ($_.PrincipalId -eq $adServicePrincipalRoleRemoveFrom.Id)}

if ($appRoleAssignment) {
Remove-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $adServicePrincipalRoleRemoveFrom.Id -AppRoleAssignmentId $appRoleAssignment.Id
Write-Host "Role assignment for '$Role' has been removed from Active Directory Application '$($adApplicationRoleRemoveFrom.DisplayName)'"
Write-Host "Role assignment for '$Role' has been removed from Active Directory Application '$($adApplicationRoleRemoveFrom.DisplayName)'" -ForegroundColor Green
} else {
Write-Host "Role '$Role' is not assigned to Active Directory Application '$($adApplicationRoleRemoveFrom.DisplayName)', skipping role assignment removal" -ForegroundColor Yellow
Write-Warning "Role '$Role' is not assigned to Active Directory Application '$($adApplicationRoleRemoveFrom.DisplayName)', skipping role assignment removal"
}

if ($RemoveRoleIfNoAssignmentsAreLeft) {
Expand All @@ -49,10 +49,10 @@ try {
$appRoles = $adApplication.AppRole | Where-Object Id -ne $appRole.Id
if ($appRoles) {
Update-AzADApplication -ObjectId $adApplication.Id -AppRole $appRoles
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"
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" -ForegroundColor Green
} else {
Update-AzADApplication -ObjectId $adApplication.Id -AppRole @()
Write-Host "Role '$Role' removed from Active Directory Application '$($adApplication.DisplayName)' as no more role assignments were left and the option 'RemoveRoleIfNoAssignmentsAreLeft' is set"
Write-Host "Role '$Role' removed from Active Directory Application '$($adApplication.DisplayName)' as no more role assignments were left and the option 'RemoveRoleIfNoAssignmentsAreLeft' is set" -ForegroundColor Green
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ if ($storageKeys -eq $null -or $storageKeys.count -eq 0) {
Write-Host "Got Azure storage key for storage account '$($StorageAccountName)' in resource group '$($StorageAccountResourceGroupName)'!" -ForegroundColor Green
$storageKey = $storageKeys[0]

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Write-Host "New API operation '$OperationName' was added on Azure API Management

if($OperationId -eq "" -or $PolicyFilePath -eq "")
{
Write-Host "No policy has been defined for Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'" -ForegroundColor Yellow
Write-Warning "No policy has been defined for Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'"
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ try
{
Write-Host "Account has been created for $FirstName $LastName ($mailAddress) for Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'" -ForegroundColor Green
if($Password -eq $null -or $Password -eq ""){
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
Write-Warning "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)'"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ $apimContext = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName
$exceptionOccurred = $false
$failedActions = @()

Write-Host "Checking if 'echo' API exists in the Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'..."
Write-Verbose "Checking if 'echo' API exists in the Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'..."
$echoExists = $true
try {
Get-AzApiManagementApi -Context $apimContext -ApiId 'echo-api' -ErrorAction Stop | Out-Null
Expand Down Expand Up @@ -70,7 +70,7 @@ if ($starterExists) {
}
}

Write-Host "Checking if 'unlimited' product exists..."
Write-Verbose "Checking if 'unlimited' product exists in the Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'..."
$unlimitedExists = $true
try {
Get-AzApiManagementProduct -Context $apimContext -ProductId 'unlimited' -ErrorAction Stop | Out-Null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ try {
Remove-AzApiManagementUser -Context $apimContext -UserId $apimUserId
Write-Host "Removed the user account with e-mail '$MailAddress' and ID '$apimUserId' for the Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'" -ForegroundColor Green
} else {
Write-Host "User account with e-mail '$MailAddress' not found in the Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'"
Write-Warning "User account with e-mail '$MailAddress' not found in the Azure API Management service '$ServiceName' in resource group '$ResourceGroupName'"
}
}
catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ if ($appService -eq $null) {
throw "No Azure App Service with name '$AppServiceName' could be found in the resource group '$ResourceGroupName'"
}

Write-Host "Azure App service has been found for name '$AppServiceName' in the resource group '$ResourceGroupName'" -ForegroundColor Green
Write-Host "Azure App service has been found for name '$AppServiceName' in the resource group '$ResourceGroupName'"
Write-Verbose "Extracting the existing application settings from the Azure App Service '$AppServiceName' in the resource group '$ResourceGroupName'..."
$appServiceSettings = $appService.SiteConfig.AppSettings

Expand Down Expand Up @@ -47,4 +47,4 @@ try
throw "The Azure App Service settings could not be updated for the Azure App Service '$AppServiceName' in the resource group '$ResourceGroupName'. Details: $($_.Exception.Message)"
}

Write-Host "Successfully set the application setting '$AppServiceSettingName' of the Azure App Service '$AppServiceName' in resource group '$ResourceGroupName'"
Write-Host "Successfully set the application setting '$AppServiceSettingName' of the Azure App Service '$AppServiceName' in resource group '$ResourceGroupName'" -ForegroundColor Green
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ catch
}
else
{
Write-Host $message
Write-Host "Skipping the '$Action'-operation."
Write-Warning $message
Write-verbose "Skipping the '$Action'-operation."
return
}
}
Expand All @@ -41,12 +41,12 @@ if($Action -eq "Start")
if($null -ne $DataFactoryTrigger)
{
$succeeded = Start-AzDataFactoryV2Trigger -ResourceGroupName $ResourceGroupName -DataFactoryName $DataFactoryName -Name $DataFactoryTriggerName -Force -ErrorAction Stop
Write-Host "The trigger '$DataFactoryTriggerName' has been started."
Write-Host "Started Azure Data Factory trigger '$DataFactoryTriggerName' of data factory '$DataFactoryName' in resource group '$ResourceGroupName'" -ForegroundColor Green
}
}
catch
{
throw "Error starting trigger '$DataFactoryTriggerName' in data factory '$DataFactoryName'"
throw "Error starting Azure Data Factory trigger '$DataFactoryTriggerName' of data factory '$DataFactoryName' in resource group '$ResourceGroupName'"
}
}

Expand All @@ -57,11 +57,11 @@ if($Action -eq "Stop")
if($null -ne $DataFactoryTrigger)
{
$succeeded = Stop-AzDataFactoryV2Trigger -ResourceGroupName $ResourceGroupName -DataFactoryName $DataFactoryName -Name $DataFactoryTriggerName -Force -ErrorAction Stop
Write-Host "The trigger '$DataFactoryTriggerName' has been stopped."
Write-Host "Stopped Azure Data Factory trigger '$DataFactoryTriggerName' of data factory '$DataFactoryName' in resource group '$ResourceGroupName'" -ForegroundColor Green
}
}
catch
{
throw "Error stopping trigger '$DataFactoryTrigger' in data factory '$DataFactoryName'"
throw "Error stopping Azure Data Factory trigger '$DataFactoryTrigger' of data factory '$DataFactoryName' in resource group '$ResourceGroupName'"
}
}
6 changes: 3 additions & 3 deletions src/Arcus.Scripting.DevOps/Scripts/Save-AzDevOpsBuild.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ if ($collectionUri.EndsWith('/') -eq $false) {

$requestUri = "$collectionUri" + "$ProjectId/_apis/build/builds/" + $BuildId + "?api-version=6.0"

Write-Verbose "Saving Azure DevOps build with build ID $BuildId in project $ProjectId by posting $requestBody to $requestUri"
Write-Verbose "Saving Azure DevOps build with build ID $BuildId in project $ProjectId by posting '$requestBody' to '$requestUri'..."
$response = Invoke-WebRequest -Uri $requestUri -Method Patch -Body $requestBody -ContentType "application/json" -Headers @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" }

if ($response.StatusCode -ne 200) {
throw "Unable to retain build indefinetely. API request returned statuscode $($response.StatusCode)"
throw "Unable to retain Azure DevOps build indefinetely with build ID $BuildId in project $ProjectId. API request returned statuscode $($response.StatusCode)"
}

Write-Host "Azure DevOps build with build ID $BuildId in project $ProjectId saved"
Write-Host "Saved Azure DevOps build with build ID $BuildId in project $ProjectId" -ForegroundColor Green
Loading