Skip to content

Commit 54809a3

Browse files
committed
initial commit
1 parent ca3427b commit 54809a3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

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

0 commit comments

Comments
 (0)