forked from bwya77/GraphAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnect-Graph.ps1
76 lines (59 loc) · 2.28 KB
/
Connect-Graph.ps1
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
function Connect-Graph
{
$clientId = ""
$redirectUrl = [System.Uri]"urn:ietf:wg:oauth:2.0:oob" # This is the standard Redirect URI for Windows Azure PowerShell
$tenant = "DOMAIN.onmicrosoft.com"
$resource = "https://graph.microsoft.com/";
$serviceRootURL = "https://graph.microsoft.com//$tenant"
$authUrl = "https://login.microsoftonline.com/$tenant";
$postParams = @{ resource = "$resource"; client_id = "$clientId" }
$response = Invoke-RestMethod -Method POST -Uri "$authurl/oauth2/devicecode" -Body $postParams
Write-Host $response.message
#I got tired of manually copying the code, so I did string manipulation and stored the code in a variable and added to the clipboard automatically
$code = ($response.message -split "code " | Select-Object -Last 1) -split " to authenticate."
Set-Clipboard -Value $code
#Start-Process "https://microsoft.com/devicelogin"
Add-Type -AssemblyName System.Windows.Forms
$form = New-Object -TypeName System.Windows.Forms.Form -Property @{ Width = 440; Height = 640 }
$web = New-Object -TypeName System.Windows.Forms.WebBrowser -Property @{ Width = 440; Height = 600; Url = "https://www.microsoft.com/devicelogin" }
$web.Add_DocumentCompleted($DocComp)
$web.DocumentText
$form.Controls.Add($web)
$form.Add_Shown({ $form.Activate() })
$web.ScriptErrorsSuppressed = $true
$form.AutoScaleMode = 'Dpi'
$form.text = "Graph API Authentication"
$form.ShowIcon = $False
$form.AutoSizeMode = 'GrowAndShrink'
$Form.StartPosition = 'CenterScreen'
$form.ShowDialog() | Out-Null
$tokenParams = @{ grant_type = "device_code"; resource = "$resource"; client_id = "$clientId"; code = "$($response.device_code)" }
$global:tokenResponse = $null
try
{
$global:tokenResponse = Invoke-RestMethod -Method POST -Uri "$authurl/oauth2/token" -Body $tokenParams
}
catch [System.Net.WebException]
{
if ($_.Exception.Response -eq $null)
{
throw
}
$result = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($result)
$reader.BaseStream.Position = 0
$errBody = ConvertFrom-Json $reader.ReadToEnd();
if ($errBody.Error -ne "authorization_pending")
{
throw
}
}
If ($null -eq $global:tokenResponse)
{
Write-Warning "Not Connected"
}
Else
{
Write-Host -ForegroundColor Green "Connected"
}
}