-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest-O365Token.ps1
66 lines (55 loc) · 1.86 KB
/
Test-O365Token.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
<#
<-Test-O365Tokens->
.SYNOPSIS
Checks if access token is still valid and if not updates with the refresh token
.DESCRIPTION
Uses the Update-O365Tokens function to update tokens if required
.PARAMETER ClientSecret
API recource generally this is "https://graph.microsoft.com/" but this should be managed in the settings file
.PARAMETER AccessTokenName
The access token name that should be updated
.PARAMETER RefreshTokenName
The access token name that should be used and updated
.PARAMETER ClientId
The Client id which can be found in the registered application on the MSGraph page
.PARAMETER RedirectUri
The redirect URI that is registered in the created application
.PARAMETER TokenDirectory
Directory the tokens should be saved to
.NOTES
requires additional fuctions
Update-O365Tokens
#>
function Test-O365Tokens {
[CmdletBinding()]
Param(
[Parameter()][String]$ClientSecret = 'Your MS Graph Client Secret',
[Parameter()][String]$AccessTokenName = 'filen name of you access token',
[Parameter()][String]$RefreshTokenName = 'file name of you refresh token',
[Parameter()][String]$ClientId = 'Your MS Graph Client Id',
[Parameter()][String]$RedirectUri = 'your MS Graph redirect URI',
[Parameter()][String]$TokenDirectory = 'c:\tokens\'
)
try {
$AccessToken = get-content -Path(Join-Path -Path $TokenDirectory -ChildPath $AccessTokenName)
$Request = @{
Method = 'GET'
Headers = @{
Authorization = "Bearer $Accesstoken"
"Content-Type" = "application/json"
}
URI = 'https://graph.microsoft.com/v1.0/me'
}
Invoke-RestMethod @Request
return $false
}
catch {
try {
Update-O365Tokens
}
catch {
throw $_
}
return $true
}
}