Public/Import-GraphOauthAccessToken.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
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
<#
    .NOTES
    ===========================================================================
     Created with: SAPIEN Technologies, Inc., PowerShell Studio 2017 v5.4.135
     Created on: 2/9/2017 6:55 AM
     Edited on: 3/30/2017
     Created by: Mark Kraus
     Organization: Mitel
     Filename: Import-GraphOauthAccessToken.ps1
    ===========================================================================
    .DESCRIPTION
        Import-GraphOauthAccessToken function
#>


<#
    .SYNOPSIS
        Imports an exported Graph OAuth Access Token Object
    
    .DESCRIPTION
        Imports an exported Graph OAuth Access Token Object and retruns a Graph OAuth Access Token Object.
    
    .PARAMETER Path
        Specifies the XML files where the Graph Application Object was exported.
    
    .PARAMETER LiteralPath
        Specifies the XML files where the Graph Application Object was exported. Unlike Path, the value of the LiteralPath parameter is used exactly as it is typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell Windows PowerShell not to interpret any characters as escape sequences.
    
    .EXAMPLE
        PS C:\> $GraphAccessToken = Import-GraphOAuthAccessToken -Path 'c:\GraphAccessToken.xml'
    
    .NOTES
        See Export-GraphOauthAccessToken for exporting Graph AcessToken Objects
        See Get-GraphOauthAccessToken for obtaining a Graph Access Token from the API
    
    .LINK
        http://psmsgraph.readthedocs.io/en/latest/functions/Import-GraphOauthAccessToken
    .LINK
        http://psmsgraph.readthedocs.io/en/latest/functions/Export-GraphOauthAccessToken
    .LINK
        http://psmsgraph.readthedocs.io/en/latest/functions/Get-GraphOauthAccessToken
    
    .OUTPUTS
        MSGraphAPI.Oauth.AccessToken
#>

function Import-GraphOauthAccessToken {
    [CmdletBinding(DefaultParameterSetName = 'Path',
                   ConfirmImpact = 'Low',
                   HelpUri = 'http://psmsgraph.readthedocs.io/en/latest/functions/Import-GraphOAuthAccessToken',
                   SupportsShouldProcess = $true)]
    [OutputType('MSGraphAPI.Oauth.AccessToken')]
    param
    (
        [Parameter(ParameterSetName = 'Path',
                   Mandatory = $true,
                   ValueFromPipeline = $true,
                   ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [string[]]$Path,
        
        [Parameter(ParameterSetName = 'LiteralPath',
                   Mandatory = $true,
                   ValueFromRemainingArguments = $true)]
        [ValidateNotNullOrEmpty()]
        [string[]]$LiteralPath
    )
    
    Process {
        Switch ($PsCmdlet.ParameterSetName) {
            'Path' {
                $ImportFiles = $Path
            }
            'LiteralPath' {
                $ImportFiles = $LiteralPath
            }
        }
        foreach ($ImportFile in $ImportFiles) {
            if ($pscmdlet.ShouldProcess($ImportFile)) {
                Write-Verbose "Processing $($ImportFile)."
                $Params = @{
                    "$($PsCmdlet.ParameterSetName)" = $ImportFile
                    ErrorAction = 'Stop'
                }
                try {
                    $InObject = Import-Clixml @Params
                }
                Catch {
                    $ErrorMessage = "Unable to import from '{0}': {1}" -f @(
                        $ImportFile
                        $_.Exception.Message
                    )
                    Write-Error $ErrorMessage
                }
                Write-Verbose "Reconstituting Application object"
                $Application = $InObject.Application | New-GraphApplication
                Write-Verbose 'Reconstituting Session object'
                $Session = [Microsoft.PowerShell.Commands.WebRequestSession]::new()
                $Session.Certificates = $InObject.Session.Certificates
                $Session.Credentials = $InObject.Session.Credentials
                try {
                    $InObject.Session.Headers.GetEnumerator() | ForEach-Object {
                        $Session.Headers[$_.key] = $_.value
                    }
                }
                catch {
                    Write-Warning "Session headers could not be imported."
                }
                $Session.MaximumRedirection = $InObject.Session.MaximumRedirection
                $Session.Proxy = $InObject.Session.Proxy
                $Session.UseDefaultCredentials = $InObject.Session.UseDefaultCredentials
                $Session.UserAgent = $Application.UserAgent
                
                Write-Verbose 'Create Graph Token Object'
                $Params = @{
                    Application = $Application
                    AccessTokenCredential = $InObject.AccessTokenCredential
                    RefreshTokenCredential = $InObject.RefreshTokenCredential
                    RequestedDate = $InObject.RequestedDate
                    Response = $InObject.Response
                    ResponseHeaders = $InObject.ResponseHeaders
                    LastRequestDate = $InObject.LastRequestDate
                    Session = $Session
                    GUID = $InObject.GUID
                }
                New-GraphOauthAccessToken @Params
            } #End Should Process
        } #End Foreach
    } #End Process
} #End Function