Skip to content

Commit

Permalink
improve error handling
Browse files Browse the repository at this point in the history
Remove-AzAdApplication does not return boolean
  • Loading branch information
Jelle Broekhuijsen committed Aug 18, 2023
1 parent 9237594 commit 94644f1
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions Remove-AzAdApplications.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
.DESCRIPTION
Script to delete application registrations that are not tagged with 'persistent'. This script is intended to be run on a schedule to clean up unused application registrations. Tags are checked in the application manifest.
.NOTES
Version: 1.0.0
Version: 1.0.1
Author: Jelle Broekhuijsen - jll.io Consultancy
Creation Date: 11/8/2023
Expand All @@ -32,7 +32,7 @@ $applicationForRemoval = [System.Collections.Concurrent.ConcurrentBag[PSObject]]
#Filter out applications that are not tagged with 'persistent'
$applications | ForEach-Object -Parallel {
$localApplicationForRemoval = $using:applicationForRemoval
$application = Get-AzADApplication -ObjectId $_.Id | Select-Object DisplayName,Id,AppId,Tag
$application = Get-AzADApplication -ObjectId $_.Id | Select-Object DisplayName, Id, AppId, Tag
if ($application.Tag -notcontains 'persistent') {
Write-Output "Marked application '$($application.DisplayName)' for removal"
$localApplicationForRemoval.Add($application)
Expand All @@ -44,8 +44,10 @@ Write-Output "Found $($applicationForRemoval.Count) applications to remove"
$applicationForRemoval | ForEach-Object -Parallel {
$localFailures = $using:failures
Write-Output "Removing application '$($_.DisplayName)'"
$result = Remove-AzADApplication -ObjectId $_.Id
if ($result -ne $true) {
try {
Remove-AzADApplication -ObjectId $_.Id -ErrorAction Stop
}
catch {
Write-Warning "Failed to remove application '$($_.DisplayName)'"
$localFailures.Add($_)
}
Expand Down

0 comments on commit 94644f1

Please sign in to comment.