Created
November 24, 2021 19:42
-
-
Save kevinblumenfeld/9fcc75db87b3519c922403a4ca5f8ae0 to your computer and use it in GitHub Desktop.
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
function Add-GraphConfig { | |
<# | |
.SYNOPSIS | |
Create the encrypted file needed to connect to Microsoft Graph using: Connect-Graph | |
.DESCRIPTION | |
Create the encrypted file needed to connect to Microsoft Graph using: Connect-Graph | |
.PARAMETER Tenant | |
use NameOfTenant if the tenant domain is NameOfTenant.onmicrosoft.com | |
don't use the entire domain, just NameOfTenant | |
.PARAMETER Workload | |
Anything simple and descriptive that represents the app or permissions you are connecting to | |
.EXAMPLE | |
Add-GraphConfig -Tenant NameOfTenant -Workload DEMO | |
NOTE: use NameOfTenant if the tenant domain is NameOfTenant.onmicrosoft.com | |
don't use the entire domain, just NameOfTenant | |
.NOTES | |
The GUI will output how to connect to Graph now that you have encrypted and saved the configuration | |
In this example you will see in the GUIs textbox: | |
Connect-Graph -Tenant NameOfTenant -Workload DEMO | |
NOTE: Add -Delegated only if you specified a username and password in this GUI | |
for example: | |
Connect-Graph -Tenant NameOfTenant -Workload DEMO -Delegated | |
Created by Kevin Blumenfeld | |
#> | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory)] | |
[string] | |
$Tenant, | |
[Parameter(Mandatory)] | |
[string] | |
$Workload | |
) | |
$Path = "$env:USERPROFILE\.GraphTools\Tenants\$Tenant\$Workload" | |
$null = New-Item -Path $Path -Type Directory -Force | |
Get-Job -State Completed | Remove-Job -force | |
$null = Start-Job -ArgumentList $Tenant, $Workload, $Path -ScriptBlock { | |
param($Tenant, $Workload, $Path) | |
Add-Type -AssemblyName System.Windows.Forms | |
[System.Windows.Forms.Application]::EnableVisualStyles() | |
$Form = New-Object system.Windows.Forms.Form | |
$Form.ClientSize = '400,450' | |
$Form.text = "Microsoft Graph Credential Export Tool" | |
$Form.BackColor = "#354CA1" | |
$Form.TopMost = $true | |
$Form.FormBorderStyle = "FixedDialog" | |
$Form.ShowInTaskbar = $true | |
$Form.StartPosition = "centerscreen" | |
$TenantPanel = New-Object system.Windows.Forms.Panel | |
$TenantPanel.height = 290 | |
$TenantPanel.width = 388 | |
$TenantPanel.location = New-Object System.Drawing.Point(5, 4) | |
$TenantConfigExportButton = New-Object system.Windows.Forms.Button | |
$TenantConfigExportButton.text = "Export Tenant Config" | |
$TenantConfigExportButton.width = 375 | |
$TenantConfigExportButton.height = 30 | |
$TenantConfigExportButton.Anchor = 'top,right,left' | |
$TenantConfigExportButton.location = New-Object System.Drawing.Point(13, 136) | |
$TenantConfigExportButton.Font = 'Microsoft Sans Serif,10,style=Bold' | |
$TenantConfigExportButton.ForeColor = "#cecece" | |
$TenantConfigExportButton.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat | |
$TenantConfigExportButton.FlatAppearance.BorderColor = [System.Drawing.Color]::black | |
$TenantConfigExportButton.FlatAppearance.BorderSize = 0 | |
$TenantConfigExportButton.BackColor = "#171717" | |
$TenantCredsExportButton = New-Object system.Windows.Forms.Button | |
$TenantCredsExportButton.text = "Export Tenant Credentials" | |
$TenantCredsExportButton.width = 375 | |
$TenantCredsExportButton.height = 30 | |
$TenantCredsExportButton.Anchor = 'top,right,left' | |
$TenantCredsExportButton.location = New-Object System.Drawing.Point(13, 240) | |
$TenantCredsExportButton.Font = 'Microsoft Sans Serif,10,style=Bold' | |
$TenantCredsExportButton.ForeColor = "#cecece" | |
$TenantCredsExportButton.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat | |
$TenantCredsExportButton.FlatAppearance.BorderColor = [System.Drawing.Color]::black | |
$TenantCredsExportButton.FlatAppearance.BorderSize = 0 | |
$TenantCredsExportButton.BackColor = "#171717" | |
$ClientInfoTextBox = New-Object system.Windows.Forms.TextBox | |
$ClientInfoTextBox.multiline = $true | |
$ClientInfoTextBox.width = 390 | |
$ClientInfoTextBox.height = 130 | |
$ClientInfoTextBox.Anchor = 'top,right,left' | |
$ClientInfoTextBox.location = New-Object System.Drawing.Point(5, 300) | |
$ClientInfoTextBox.Font = 'Microsoft Sans Serif,10' | |
$ClientInfoTextBox.BackColor = "#000000" | |
$ClientInfoTextBox.ForeColor = "#5bde09" | |
$ClientInfoTextBox.BorderStyle = "none" | |
$TenantLabel = New-Object system.Windows.Forms.Label | |
$TenantLabel.text = $Tenant | |
$TenantLabel.AutoSize = $true | |
$TenantLabel.width = 25 | |
$TenantLabel.height = 10 | |
$TenantLabel.location = New-Object System.Drawing.Point(173, 9) | |
$TenantLabel.Font = 'Microsoft Sans Serif,14,style=Bold' | |
$TenantLabel.ForeColor = "#cecece" | |
$ClientIDLabel = New-Object system.Windows.Forms.Label | |
$ClientIDLabel.text = "Client ID:" | |
$ClientIDLabel.AutoSize = $true | |
$ClientIDLabel.width = 25 | |
$ClientIDLabel.height = 10 | |
$ClientIDLabel.location = New-Object System.Drawing.Point(13, 36) | |
$ClientIDLabel.Font = 'Microsoft Sans Serif,10' | |
$ClientIDLabel.ForeColor = "#cecece" | |
$ClientIDTextBox = New-Object system.Windows.Forms.TextBox | |
$ClientIDTextBox.multiline = $false | |
$ClientIDTextBox.BackColor = "#171717" | |
$ClientIDTextBox.width = 300 | |
$ClientIDTextBox.height = 30 | |
$ClientIDTextBox.location = New-Object System.Drawing.Point(93, 36) | |
$ClientIDTextBox.Font = 'Microsoft Sans Serif,10' | |
$ClientIDTextBox.ForeColor = "#ffffff" | |
$ClientIDTextBox.BorderStyle = "None" | |
$TenantIDLabel = New-Object system.Windows.Forms.Label | |
$TenantIDLabel.text = "Tenant ID:" | |
$TenantIDLabel.AutoSize = $true | |
$TenantIDLabel.width = 25 | |
$TenantIDLabel.height = 10 | |
$TenantIDLabel.location = New-Object System.Drawing.Point(13, 73) | |
$TenantIDLabel.Font = 'Microsoft Sans Serif,10' | |
$TenantIDLabel.ForeColor = "#cecece" | |
$TenantIDTextBox = New-Object system.Windows.Forms.TextBox | |
$TenantIDTextBox.multiline = $false | |
$TenantIDTextBox.width = 294 | |
$TenantIDTextBox.height = 30 | |
$TenantIDTextBox.location = New-Object System.Drawing.Point(93, 73) | |
$TenantIDTextBox.Font = 'Microsoft Sans Serif,10' | |
$TenantIDTextBox.BackColor = "#171717" | |
$TenantIDTextBox.ForeColor = "#ffffff" | |
$TenantIDTextBox.BorderStyle = "None" | |
$ClientSecLabel = New-Object system.Windows.Forms.Label | |
$ClientSecLabel.text = "Secret:" | |
$ClientSecLabel.AutoSize = $true | |
$ClientSecLabel.width = 25 | |
$ClientSecLabel.height = 10 | |
$ClientSecLabel.location = New-Object System.Drawing.Point(13, 105) | |
$ClientSecLabel.Font = 'Microsoft Sans Serif,10' | |
$ClientSecLabel.ForeColor = "#cecece" | |
$ClientSecTextBox = New-Object system.Windows.Forms.MaskedTextBox | |
$ClientsecTextBox.PasswordChar = '*' | |
$ClientSecTextBox.multiline = $false | |
$ClientSecTextBox.width = 294 | |
$ClientSecTextBox.height = 30 | |
$ClientSecTextBox.location = New-Object System.Drawing.Point(93, 107) | |
$ClientSecTextBox.Font = 'Microsoft Sans Serif,10' | |
$ClientSecTextBox.BackColor = "#171717" | |
$ClientSecTextBox.ForeColor = "#ffffff" | |
$ClientSecTextBox.BorderStyle = "None" | |
$UsernameLabel = New-Object system.Windows.Forms.Label | |
$UsernameLabel.text = "Username:" | |
$UsernameLabel.AutoSize = $true | |
$UsernameLabel.width = 25 | |
$UsernameLabel.height = 10 | |
$UsernameLabel.location = New-Object System.Drawing.Point(13, 180) | |
$UsernameLabel.Font = 'Microsoft Sans Serif,10' | |
$UsernameLabel.ForeColor = "#cecece" | |
$UsernameTextBox = New-Object system.Windows.Forms.TextBox | |
$UsernameTextBox.multiline = $false | |
$UsernameTextBox.width = 290 | |
$UsernameTextBox.height = 30 | |
$UsernameTextBox.location = New-Object System.Drawing.Point(96, 180) | |
$UsernameTextBox.Font = 'Microsoft Sans Serif,10' | |
$UsernameTextBox.BackColor = "#171717" | |
$UsernameTextBox.ForeColor = "#ffffff" | |
$UsernameTextBox.BorderStyle = "None" | |
$PasswordLabel = New-Object system.Windows.Forms.Label | |
$PasswordLabel.text = "Password:" | |
$PasswordLabel.AutoSize = $true | |
$PasswordLabel.width = 25 | |
$PasswordLabel.height = 10 | |
$PasswordLabel.location = New-Object System.Drawing.Point(13, 210) | |
$PasswordLabel.Font = 'Microsoft Sans Serif,10' | |
$PasswordLabel.ForeColor = "#cecece" | |
$PasswordMTextBox = New-Object system.Windows.Forms.MaskedTextBox | |
$PasswordMTextBox.multiline = $false | |
$PasswordMTextBox.width = 291 | |
$PasswordMTextBox.height = 30 | |
$PasswordMTextBox.location = New-Object System.Drawing.Point(95, 210) | |
$PasswordMTextBox.Font = 'Microsoft Sans Serif,10' | |
$PasswordMTextBox.PasswordChar = '*' | |
$PasswordMTextBox.BackColor = "#171717" | |
$PasswordMTextBox.ForeColor = "#ffffff" | |
$PasswordMTextBox.BorderStyle = "None" | |
$Form.controls.AddRange(@($ConnectionPanel, $TenantPanel, $ClientInfoTextBox)) | |
$TenantPanel.controls.AddRange(@( | |
$TenantLabel, $ClientIDLabel, $ClientIDTextBox, $TenantIDLabel, $TenantIDTextBox | |
$ClientSecLabel, $ClientSecTextBox, $UsernameLabel, $UsernameTextBox, $PasswordLabel | |
$PasswordMTextBox, $TenantConfigExportButton, $TenantCredsExportButton | |
)) | |
$TenantConfigExportButton.Add_Click( | |
{ | |
try { | |
$Script:TenantObject = [PSCustomObject]@{ | |
TenantClientID = $ClientIDTextBox.text | |
TenantTenantID = $TenantIDTextBox.text | |
TenantSecret = $ClientSecTextBox.text | ConvertTo-SecureString -AsPlainText -Force | |
} | |
$TenantConfig = Join-Path -Path $Path -ChildPath 'Config.xml' | |
[PSCustomObject]@{ | |
Cred = [PSCredential]::new($TenantObject.TenantTenantID, $TenantObject.TenantSecret) | |
ClientId = $TenantObject.TenantClientID | |
} | Export-Clixml -Path $TenantConfig | |
$ClientInfoTextBox.AppendText(('Connect-Graph -Tenant {0} -Workload {1} {2}' -f $Tenant, $Workload, [Environment]::NewLine)) | |
} | |
catch { | |
$ClientInfoTextBox.AppendText(($_.Exception.Message, [Environment]::NewLine)) | |
} | |
} | |
) | |
$TenantCredsExportButton.Add_Click( | |
{ | |
try { | |
$Script:TenantCredObj = [PSCustomObject]@{ | |
TenantUsername = $UsernameTextBox.text | |
TenantPassword = $PasswordMTextBox.text | ConvertTo-SecureString -AsPlainText -Force | |
} | |
$TenantCred = Join-Path -Path $Path -ChildPath 'Cred.xml' | |
[PSCredential]::new($TenantCredObj.TenantUsername, $TenantCredObj.TenantPassword) | Export-Clixml -Path $TenantCred | |
$ClientInfoTextBox.AppendText(('Connect-Graph -Tenant {0} -Workload {1} -Delegated {2}' -f $Tenant, $Workload, [Environment]::NewLine)) | |
} | |
catch { | |
$ClientInfoTextBox.AppendText(($_.Exception.Message, [Environment]::NewLine)) | |
} | |
} | |
) | |
[void]$Form.ShowDialog() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment