Skip to content
This repository was archived by the owner on May 3, 2024. It is now read-only.

Commit 349d66f

Browse files
addressed all pending issues in 3-1
1 parent 8772549 commit 349d66f

File tree

4 files changed

+244
-98
lines changed

4 files changed

+244
-98
lines changed

3-Authorization-II/1-call-api/AppCreationScripts/Cleanup.ps1

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
22
[CmdletBinding()]
33
param(
44
[Parameter(Mandatory=$False, HelpMessage='Tenant ID (This is a GUID which represents the "Directory ID" of the AzureAD tenant into which you want to create the apps')]
@@ -7,6 +7,7 @@ param(
77
[string] $azureEnvironmentName
88
)
99

10+
1011
Function Cleanup
1112
{
1213
if (!$azureEnvironmentName)
@@ -24,11 +25,13 @@ Function Cleanup
2425

2526
# Connect to the Microsoft Graph API
2627
Write-Host "Connecting to Microsoft Graph"
27-
if ($tenantId -eq "") {
28+
if ($tenantId -eq "")
29+
{
2830
Connect-MgGraph -Scopes "Application.ReadWrite.All" -Environment $azureEnvironmentName
2931
$tenantId = (Get-MgContext).TenantId
3032
}
31-
else {
33+
else
34+
{
3235
Connect-MgGraph -TenantId $tenantId -Scopes "Application.ReadWrite.All" -Environment $azureEnvironmentName
3336
}
3437

@@ -38,15 +41,17 @@ Function Cleanup
3841
Write-Host "Removing 'service' (msal-node-api) if needed"
3942
try
4043
{
41-
Get-MgApplication -Filter "DisplayName eq 'msal-node-api'" | ForEach-Object {Remove-MgApplication -ApplicationId $_.Id }
44+
Get-MgApplication -Filter "DisplayName eq 'msal-node-api'" | ForEach-Object {Remove-MgApplication -ApplicationId $_.Id }
4245
}
4346
catch
4447
{
45-
Write-Host "Unable to remove the application 'msal-node-api' . Try deleting manually." -ForegroundColor White -BackgroundColor Red
48+
$message = $_
49+
Write-Warning $Error[0]
50+
Write-Host "Unable to remove the application 'msal-node-api'. Error is $message. Try deleting manually." -ForegroundColor White -BackgroundColor Red
4651
}
4752

4853
Write-Host "Making sure there are no more (msal-node-api) applications found, will remove if needed..."
49-
$apps = Get-MgApplication -Filter "DisplayName eq 'msal-node-api'"
54+
$apps = Get-MgApplication -Filter "DisplayName eq 'msal-node-api'" | Format-List Id, DisplayName, AppId, SignInAudience, PublisherDomain
5055

5156
if ($apps)
5257
{
@@ -62,24 +67,28 @@ Function Cleanup
6267
# also remove service principals of this app
6368
try
6469
{
65-
Get-MgServicePrincipal -filter "DisplayName eq 'msal-node-api'" | ForEach-Object {Remove-MgServicePrincipal -ApplicationId $_.Id -Confirm:$false}
70+
Get-MgServicePrincipal -filter "DisplayName eq 'msal-node-api'" | ForEach-Object {Remove-MgServicePrincipal -ServicePrincipalId $_.Id -Confirm:$false}
6671
}
6772
catch
6873
{
69-
Write-Host "Unable to remove ServicePrincipal 'msal-node-api' . Try deleting manually from Enterprise applications." -ForegroundColor White -BackgroundColor Red
74+
$message = $_
75+
Write-Warning $Error[0]
76+
Write-Host "Unable to remove ServicePrincipal 'msal-node-api'. Error is $message. Try deleting manually from Enterprise applications." -ForegroundColor White -BackgroundColor Red
7077
}
7178
Write-Host "Removing 'client' (msal-react-spa) if needed"
7279
try
7380
{
74-
Get-MgApplication -Filter "DisplayName eq 'msal-react-spa'" | ForEach-Object {Remove-MgApplication -ApplicationId $_.Id }
81+
Get-MgApplication -Filter "DisplayName eq 'msal-react-spa'" | ForEach-Object {Remove-MgApplication -ApplicationId $_.Id }
7582
}
7683
catch
7784
{
78-
Write-Host "Unable to remove the application 'msal-react-spa' . Try deleting manually." -ForegroundColor White -BackgroundColor Red
85+
$message = $_
86+
Write-Warning $Error[0]
87+
Write-Host "Unable to remove the application 'msal-react-spa'. Error is $message. Try deleting manually." -ForegroundColor White -BackgroundColor Red
7988
}
8089

8190
Write-Host "Making sure there are no more (msal-react-spa) applications found, will remove if needed..."
82-
$apps = Get-MgApplication -Filter "DisplayName eq 'msal-react-spa'"
91+
$apps = Get-MgApplication -Filter "DisplayName eq 'msal-react-spa'" | Format-List Id, DisplayName, AppId, SignInAudience, PublisherDomain
8392

8493
if ($apps)
8594
{
@@ -95,11 +104,13 @@ Function Cleanup
95104
# also remove service principals of this app
96105
try
97106
{
98-
Get-MgServicePrincipal -filter "DisplayName eq 'msal-react-spa'" | ForEach-Object {Remove-MgServicePrincipal -ApplicationId $_.Id -Confirm:$false}
107+
Get-MgServicePrincipal -filter "DisplayName eq 'msal-react-spa'" | ForEach-Object {Remove-MgServicePrincipal -ServicePrincipalId $_.Id -Confirm:$false}
99108
}
100109
catch
101110
{
102-
Write-Host "Unable to remove ServicePrincipal 'msal-react-spa' . Try deleting manually from Enterprise applications." -ForegroundColor White -BackgroundColor Red
111+
$message = $_
112+
Write-Warning $Error[0]
113+
Write-Host "Unable to remove ServicePrincipal 'msal-react-spa'. Error is $message. Try deleting manually from Enterprise applications." -ForegroundColor White -BackgroundColor Red
103114
}
104115
}
105116

@@ -110,7 +121,17 @@ Import-Module Microsoft.Graph.Applications
110121
$ErrorActionPreference = "Stop"
111122

112123

113-
Cleanup -tenantId $tenantId -environment $azureEnvironmentName
124+
try
125+
{
126+
Cleanup -tenantId $tenantId -environment $azureEnvironmentName
127+
}
128+
catch
129+
{
130+
$_.Exception.ToString() | out-host
131+
$message = $_
132+
Write-Warning $Error[0]
133+
Write-Host "Unable to register apps. Error is $message." -ForegroundColor White -BackgroundColor Red
134+
}
114135

115136
Write-Host "Disconnecting from tenant"
116137
Disconnect-MgGraph

0 commit comments

Comments
 (0)