|
| 1 | +#Requires -Version 7 |
| 2 | + |
| 3 | +[CmdletBinding()] |
| 4 | +param( |
| 5 | + [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')] |
| 6 | + [string] $tenantId, |
| 7 | + [Parameter(Mandatory=$False, HelpMessage='Azure environment to use while running the script. Default = Global')] |
| 8 | + [string] $azureEnvironmentName |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +Function Cleanup |
| 13 | +{ |
| 14 | + if (!$azureEnvironmentName) |
| 15 | + { |
| 16 | + $azureEnvironmentName = "Global" |
| 17 | + } |
| 18 | + |
| 19 | + <# |
| 20 | + .Description |
| 21 | + This function removes the Azure AD applications for the sample. These applications were created by the Configure.ps1 script |
| 22 | + #> |
| 23 | + |
| 24 | + # $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant |
| 25 | + # into which you want to create the apps. Look it up in the Azure portal in the "Properties" of the Azure AD. |
| 26 | + |
| 27 | + # Connect to the Microsoft Graph API |
| 28 | + Write-Host "Connecting to Microsoft Graph" |
| 29 | + |
| 30 | + |
| 31 | + if ($tenantId -eq "") |
| 32 | + { |
| 33 | + Connect-MgGraph -Scopes "User.Read.All Organization.Read.All Application.ReadWrite.All" -Environment $azureEnvironmentName |
| 34 | + } |
| 35 | + else |
| 36 | + { |
| 37 | + Connect-MgGraph -TenantId $tenantId -Scopes "User.Read.All Organization.Read.All Application.ReadWrite.All" -Environment $azureEnvironmentName |
| 38 | + } |
| 39 | + |
| 40 | + $context = Get-MgContext |
| 41 | + $tenantId = $context.TenantId |
| 42 | + |
| 43 | + # Get the user running the script |
| 44 | + $currentUserPrincipalName = $context.Account |
| 45 | + $user = Get-MgUser -Filter "UserPrincipalName eq '$($context.Account)'" |
| 46 | + |
| 47 | + # get the tenant we signed in to |
| 48 | + $Tenant = Get-MgOrganization |
| 49 | + $tenantName = $Tenant.DisplayName |
| 50 | + |
| 51 | + $verifiedDomain = $Tenant.VerifiedDomains | where {$_.Isdefault -eq $true} |
| 52 | + $verifiedDomainName = $verifiedDomain.Name |
| 53 | + $tenantId = $Tenant.Id |
| 54 | + |
| 55 | + Write-Host ("Connected to Tenant {0} ({1}) as account '{2}'. Domain is '{3}'" -f $Tenant.DisplayName, $Tenant.Id, $currentUserPrincipalName, $verifiedDomainName) |
| 56 | + |
| 57 | + # Removes the applications |
| 58 | + Write-Host "Cleaning-up applications from tenant '$tenantId'" |
| 59 | + |
| 60 | + Write-Host "Removing 'client' (msal-node-webapp) if needed" |
| 61 | + try |
| 62 | + { |
| 63 | + Get-MgApplication -Filter "DisplayName eq 'msal-node-webapp'" | ForEach-Object {Remove-MgApplication -ApplicationId $_.Id } |
| 64 | + } |
| 65 | + catch |
| 66 | + { |
| 67 | + $message = $_ |
| 68 | + Write-Warning $Error[0] |
| 69 | + Write-Host "Unable to remove the application 'msal-node-webapp'. Error is $message. Try deleting manually." -ForegroundColor White -BackgroundColor Red |
| 70 | + } |
| 71 | + |
| 72 | + Write-Host "Making sure there are no more (msal-node-webapp) applications found, will remove if needed..." |
| 73 | + $apps = Get-MgApplication -Filter "DisplayName eq 'msal-node-webapp'" | Format-List Id, DisplayName, AppId, SignInAudience, PublisherDomain |
| 74 | + |
| 75 | + if ($apps) |
| 76 | + { |
| 77 | + Remove-MgApplication -ApplicationId $apps.Id |
| 78 | + } |
| 79 | + |
| 80 | + foreach ($app in $apps) |
| 81 | + { |
| 82 | + Remove-MgApplication -ApplicationId $app.Id |
| 83 | + Write-Host "Removed msal-node-webapp.." |
| 84 | + } |
| 85 | + |
| 86 | + # also remove service principals of this app |
| 87 | + try |
| 88 | + { |
| 89 | + Get-MgServicePrincipal -filter "DisplayName eq 'msal-node-webapp'" | ForEach-Object {Remove-MgServicePrincipal -ServicePrincipalId $_.Id -Confirm:$false} |
| 90 | + } |
| 91 | + catch |
| 92 | + { |
| 93 | + $message = $_ |
| 94 | + Write-Warning $Error[0] |
| 95 | + Write-Host "Unable to remove ServicePrincipal 'msal-node-webapp'. Error is $message. Try deleting manually from Enterprise applications." -ForegroundColor White -BackgroundColor Red |
| 96 | + } |
| 97 | + # remove self-signed certificate |
| 98 | + Write-Host "Removing CN=msal-node-webapp certificate from Cert:/CurrentUser/My" |
| 99 | + Get-ChildItem -Path Cert:\CurrentUser\My | where { $_.subject -eq "CN=msal-node-webapp" } | Remove-Item |
| 100 | +} |
| 101 | + |
| 102 | +# Pre-requisites |
| 103 | +if ($null -eq (Get-Module -ListAvailable -Name "Microsoft.Graph")) { |
| 104 | + Install-Module "Microsoft.Graph" -Scope CurrentUser |
| 105 | +} |
| 106 | + |
| 107 | +#Import-Module Microsoft.Graph |
| 108 | + |
| 109 | +if ($null -eq (Get-Module -ListAvailable -Name "Microsoft.Graph.Authentication")) { |
| 110 | + Install-Module "Microsoft.Graph.Authentication" -Scope CurrentUser |
| 111 | +} |
| 112 | + |
| 113 | +Import-Module Microsoft.Graph.Authentication |
| 114 | + |
| 115 | +if ($null -eq (Get-Module -ListAvailable -Name "Microsoft.Graph.Identity.DirectoryManagement")) { |
| 116 | + Install-Module "Microsoft.Graph.Identity.DirectoryManagement" -Scope CurrentUser |
| 117 | +} |
| 118 | + |
| 119 | +Import-Module Microsoft.Graph.Identity.DirectoryManagement |
| 120 | + |
| 121 | +if ($null -eq (Get-Module -ListAvailable -Name "Microsoft.Graph.Applications")) { |
| 122 | + Install-Module "Microsoft.Graph.Applications" -Scope CurrentUser |
| 123 | +} |
| 124 | + |
| 125 | +Import-Module Microsoft.Graph.Applications |
| 126 | + |
| 127 | +if ($null -eq (Get-Module -ListAvailable -Name "Microsoft.Graph.Groups")) { |
| 128 | + Install-Module "Microsoft.Graph.Groups" -Scope CurrentUser |
| 129 | +} |
| 130 | + |
| 131 | +Import-Module Microsoft.Graph.Groups |
| 132 | + |
| 133 | +if ($null -eq (Get-Module -ListAvailable -Name "Microsoft.Graph.Users")) { |
| 134 | + Install-Module "Microsoft.Graph.Users" -Scope CurrentUser |
| 135 | +} |
| 136 | + |
| 137 | +Import-Module Microsoft.Graph.Users |
| 138 | + |
| 139 | +$ErrorActionPreference = "Stop" |
| 140 | + |
| 141 | + |
| 142 | +try |
| 143 | +{ |
| 144 | + Cleanup -tenantId $tenantId -environment $azureEnvironmentName |
| 145 | +} |
| 146 | +catch |
| 147 | +{ |
| 148 | + $_.Exception.ToString() | out-host |
| 149 | + $message = $_ |
| 150 | + Write-Warning $Error[0] |
| 151 | + Write-Host "Unable to register apps. Error is $message." -ForegroundColor White -BackgroundColor Red |
| 152 | +} |
| 153 | + |
| 154 | +Write-Host "Disconnecting from tenant" |
| 155 | +Disconnect-MgGraph |
0 commit comments