Repository navigation
Expand file tree
/
Copy pathCreate-AzureAppRegistration.ps1
More file actions
134 lines (111 loc) · 4.29 KB
/
Copy pathCreate-AzureAppRegistration.ps1
File metadata and controls
134 lines (111 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
$ver = '1.00'
<#
Created By: Kristopher Roy
Last Updated By: BTL
Created On: 19APR2023
Last Updated On: 19APR2023
#>
# Function to check if a module is installed
function Check-Module {
param (
[string]$Name,
[switch]$Import = $true
)
# Check if the module is installed
if (Get-Module -ListAvailable -Name $Name -ErrorAction SilentlyContinue) {
# The module is installed, so import it if the switch is enabled
if ($Import) {
Import-Module -Name $Name
}
} else {
# Register the WindowsPowerShell Gallery as a trusted repository
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
# The module is not installed, so install it
Install-Module -Name $Name
}
}
# Increase the function capacity
$MaximumFunctionCount = 32768
# Import the required modules
Check-Module -Name PowerShellGet
# Import the Microsoft.Graph module
Check-Module -Name Microsoft.Graph
#Verify most recent version being used
$curver = $ver
$data = Invoke-RestMethod -Method Get -Uri https://raw.githubusercontent.com/BellTechlogix/Create-AzureAppRegistration/main/Create-AzureAppRegistration.ps1
Invoke-Expression ($data.substring(0,13))
if($curver -ge $ver){powershell -Command "& {[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); [System.Windows.Forms.MessageBox]::Show('You are running the most current script version $ver')}"}
ELSEIF($curver -lt $ver){powershell -Command "& {[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); [System.Windows.Forms.MessageBox]::Show('You are running $curver the most current script version is $ver. Ending')}"
start-sleep -seconds 10
EXIT}
# Import the Microsoft.Graph module
Check-Module -Name Microsoft.Graph
# Force authentication
Connect-MgGraph
# Create a new form
$form = New-Object System.Windows.Forms.Form
# Add a label to the form
$label = New-Object System.Windows.Forms.Label
$label.Text = "Please select an Azure directory:"
$form.Controls.Add($label)
# Add a drop-down list to the form
$dropdown = New-Object System.Windows.Forms.ComboBox
$directories = Get-MgDirectory
Foreach ($directory in $directories) {
$dropdown.Items.Add($directory.DisplayName)
}
$form.Controls.Add($dropdown)
# Add a button to the form
$button = New-Object System.Windows.Forms.Button
$button.Text = "Create App Registration"
$button.Click += {
# Get the selected directory
$selected_directory = $dropdown.SelectedItem
# If the user did not select a directory, exit the script
If ($selected_directory -eq "") {
Write-Host "No directory selected. Exiting."
Exit
}
# Prompt the user for the app registration details
Write-Host "Please enter the app registration details:"
$display_name = Read-Host -Prompt "Display name"
$application_id = Read-Host -Prompt "Application ID"
$client_secret = Read-Host -Prompt "Client secret"
# Create the app registration
$app_registration = New-MgAppRegistration `
-DisplayName $display_name `
-ApplicationId $application_id `
-ClientSecret $client_secret `
-Directory $selected_directory
# Verify the app registration details
Write-Host "Please verify the app registration details:"
Write-Host $display_name
Write-Host $application_id
Write-Host $client_secret
# Prompt the user to confirm
$confirm = Read-Host -Prompt "Is this correct? (Y/N)"
# If the user did not confirm, exit the script
If ($confirm -ne "Y") {
Write-Host "Cancelled."
Exit
}
# Save the app registration details in a JSON file
$json_file = "app_registration.json"
$content = @{
"ApplicationId" = $app_registration.ApplicationId
"ClientSecret" = $app_registration.ClientSecret
"DisplayName" = $app_registration.DisplayName
}
$json = $content | ConvertTo-Json -Depth 10
$file = Get-Content $json_file
$file | Set-Content $json_file
# Write-Host "App registration created successfully!"
# Authenticate against Microsoft Graph
$context = New-Object Microsoft.Graph.AuthenticationContext
$context.ClientId = $application_id
$context.ClientSecret = $client_secret
$context.Connect()
# Write a message to the console indicating that the app registration was created successfully and authenticated against Microsoft Graph
Write-Host "App registration created successfully and authenticated against Microsoft Graph!"
}
$form.ShowDialog()