-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kalyan Krishna
committed
Sep 2, 2022
1 parent
4b88455
commit 77ba4ff
Showing
7 changed files
with
440 additions
and
430 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,87 @@ | ||
|
||
[CmdletBinding()] | ||
param( | ||
[PSCredential] $Credential, | ||
[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')] | ||
[string] $tenantId | ||
[string] $tenantId, | ||
[Parameter(Mandatory=$False, HelpMessage='Azure environment to use while running the script. Default = Global')] | ||
[string] $azureEnvironmentName | ||
) | ||
|
||
if ((Get-Module -ListAvailable -Name "AzureAD") -eq $null) { | ||
Install-Module "AzureAD" -Scope CurrentUser | ||
} | ||
Import-Module AzureAD | ||
$ErrorActionPreference = 'Stop' | ||
|
||
Function Cleanup | ||
{ | ||
<# | ||
.Description | ||
This function removes the Azure AD applications for the sample. These applications were created by the Configure.ps1 script | ||
#> | ||
if (!$azureEnvironmentName) | ||
{ | ||
$azureEnvironmentName = "Global" | ||
} | ||
|
||
<# | ||
.Description | ||
This function removes the Azure AD applications for the sample. These applications were created by the Configure.ps1 script | ||
#> | ||
|
||
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant | ||
# into which you want to create the apps. Look it up in the Azure portal in the "Properties" of the Azure AD. | ||
|
||
# Login to Azure PowerShell (interactive if credentials are not already provided: | ||
# you'll need to sign-in with creds enabling your to create apps in the tenant) | ||
if (!$Credential -and $TenantId) | ||
# Connect to the Microsoft Graph API | ||
Write-Host "Connecting to Microsoft Graph" | ||
if ($tenantId -eq "") { | ||
Connect-MgGraph -Scopes "Application.ReadWrite.All" -Environment $azureEnvironmentName | ||
$tenantId = (Get-MgContext).TenantId | ||
} | ||
else { | ||
Connect-MgGraph -TenantId $tenantId -Scopes "Application.ReadWrite.All" -Environment $azureEnvironmentName | ||
} | ||
|
||
# Removes the applications | ||
Write-Host "Cleaning-up applications from tenant '$tenantId'" | ||
|
||
Write-Host "Removing 'webApp' (WebApp) if needed" | ||
try | ||
{ | ||
$creds = Connect-AzureAD -TenantId $tenantId | ||
Get-MgApplication -Filter "DisplayName eq 'WebApp'" | ForEach-Object {Remove-MgApplication -ApplicationId $_.Id } | ||
} | ||
else | ||
catch | ||
{ | ||
if (!$TenantId) | ||
{ | ||
$creds = Connect-AzureAD -Credential $Credential | ||
} | ||
else | ||
{ | ||
$creds = Connect-AzureAD -TenantId $tenantId -Credential $Credential | ||
} | ||
$message = $_ | ||
Write-Warning $Error[0] | ||
Write-Host "Unable to remove the application 'WebApp'. Error is $message. Try deleting manually." -ForegroundColor White -BackgroundColor Red | ||
} | ||
|
||
if (!$tenantId) | ||
Write-Host "Making sure there are no more (WebApp) applications found, will remove if needed..." | ||
$apps = Get-MgApplication -Filter "DisplayName eq 'WebApp'" | Format-List Id, DisplayName, AppId, SignInAudience, PublisherDomain | ||
|
||
if ($apps) | ||
{ | ||
$tenantId = $creds.Tenant.Id | ||
Remove-MgApplication -ApplicationId $apps.Id | ||
} | ||
$tenant = Get-AzureADTenantDetail | ||
$tenantName = ($tenant.VerifiedDomains | Where { $_._Default -eq $True }).Name | ||
|
||
# Removes the applications | ||
Write-Host "Cleaning-up applications from tenant '$tenantName'" | ||
|
||
Write-Host "Removing 'webApp' (WebApp) if needed" | ||
$app=Get-AzureADApplication -Filter "DisplayName eq 'WebApp'" | ||
foreach ($app in $apps) | ||
{ | ||
Remove-MgApplication -ApplicationId $app.Id -Debug | ||
Write-Host "Removed WebApp.." | ||
} | ||
|
||
if ($app) | ||
# also remove service principals of this app | ||
try | ||
{ | ||
Remove-AzureADApplication -ObjectId $app.ObjectId | ||
Write-Host "Removed WebApp." | ||
Get-MgServicePrincipal -filter "DisplayName eq 'WebApp'" | ForEach-Object {Remove-MgServicePrincipal -ServicePrincipalId $_.Id -Confirm:$false} | ||
} | ||
catch | ||
{ | ||
$message = $_ | ||
Write-Warning $Error[0] | ||
Write-Host "Unable to remove ServicePrincipal 'WebApp'. Error is $message. Try deleting manually from Enterprise applications." -ForegroundColor White -BackgroundColor Red | ||
} | ||
} | ||
|
||
if ($null -eq (Get-Module -ListAvailable -Name "Microsoft.Graph.Applications")) { | ||
Install-Module "Microsoft.Graph.Applications" -Scope CurrentUser | ||
} | ||
Import-Module Microsoft.Graph.Applications | ||
$ErrorActionPreference = "Stop" | ||
|
||
|
||
Cleanup -tenantId $tenantId -environment $azureEnvironmentName | ||
|
||
Cleanup -Credential $Credential -tenantId $TenantId | ||
Write-Host "Disconnecting from tenant" | ||
Disconnect-MgGraph |
Oops, something went wrong.