Public/Export-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
130
131
132
133
134
<#
    .NOTES
    ===========================================================================
     Created with:     SAPIEN Technologies, Inc., PowerShell Studio 2017 v5.4.135
     Created on:     2/9/2017 5:25 AM
     Edited on: 2/16/2017
     Created by:     Mark Kraus
     Organization:     Mitel
     Filename:     Export-GraphOauthAccessToken.ps1
    ===========================================================================
    .DESCRIPTION
        Export-GraphOauthAccessTokenFunction
#>


<#
    .SYNOPSIS
        Exports a Graph OAuth Access Token object to a file.

    .DESCRIPTION
        Used to Export a Graph OAuth Access Token object to a file so it can later be imported.

    .PARAMETER Path
        Specifies the path to the file where the XML representation of the Graph AccessToken object will be stored

    .PARAMETER LiterlPath
        Specifies the path to the file where the XML representation of the Graph AccessToken object will be stored. 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.

    .PARAMETER Encoding
        Specifies the type of encoding for the target file. The acceptable values for this parameter are:

        -- ASCII
        -- UTF8
        -- UTF7
        -- UTF32
        -- Unicode
        -- BigEndianUnicode
        -- Default
        -- OEM

        The default value is Unicode.

    .PARAMETER AccessToken
        Graph OAuth Acess Token Object to be exported.

    .EXAMPLE
        PS C:\> $GraphAccessToken | Export-GraphOAuthAccessToken -Path 'c:\GraphAccessToken.xml'

    .OUTPUTS
        System.IO.FileInfo, System.IO.FileInfo

    .NOTES
        This is an Export-Clixml wrapper.
        See Import-GraphOauthAccessToken for importing exported Graph AccessToken Objects
        See Get-GraphOauthAccessToken for obtaining a Graph AccessToken Objects

    .LINK
        http://psmsgraph.readthedocs.io/en/latest/functions/Export-GraphOauthAccessToken
    .LINK
        http://psmsgraph.readthedocs.io/en/latest/functions/Import-GraphOauthAccessToken
    .LINK
        http://psmsgraph.readthedocs.io/en/latest/functions/Get-GraphOauthAccessToken
#>

function Export-GraphOauthAccessToken {
    [CmdletBinding(DefaultParameterSetName = 'Path',
                   HelpUri = 'http://psmsgraph.readthedocs.io/en/latest/functions/Export-GraphOAuthAccessToken',
                   ConfirmImpact = 'Low',
                   SupportsShouldProcess = $true)]
    [OutputType([System.IO.FileInfo])]
    param
    (
        [Parameter(ParameterSetName = 'Path',
                   Mandatory = $true,
                   ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$Path,

        [Parameter(ParameterSetName = 'LiteralPath',
                   Mandatory = $true,
                   ValueFromRemainingArguments = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$LiterlPath,

        [Parameter(ParameterSetName = 'LiteralPath',
                   Mandatory = $false,
                   ValueFromPipelineByPropertyName = $true)]
        [Parameter(ParameterSetName = 'Path',
                   Mandatory = $false,
                   ValueFromPipelineByPropertyName = $true)]
        [ValidateSet('ASCII', 'UTF8', 'UTF7', 'UTF32', 'Unicode', 'BigEndianUnicode', 'Default', 'OEM')]
        [string]$Encoding = 'Unicode',

        [Parameter(ParameterSetName = 'LiteralPath',
                   Mandatory = $true,
                   ValueFromPipeline = $true,
                   ValueFromPipelineByPropertyName = $true)]
        [Parameter(ParameterSetName = 'Path',
                   Mandatory = $true,
                   ValueFromPipeline = $true,
                   ValueFromPipelineByPropertyName = $true)]
        [pstypename('MSGraphAPI.OAuth.AccessToken')]
        [Alias('Token')]
        [System.Management.Automation.PSObject]$AccessToken
    )

    Process {
        # Strip the ScriptProperty properties to prevent cleartext secrets in the export
        # These will be reconstituted when imported
        $ExportProperties = $AccessToken.psobject.Properties.where({ $_.MemberType -ne 'ScriptProperty' }).Name
        Write-Verbose "Propertes: $($ExportProperties -join ' ')"
        $ExportToken = $AccessToken | Select-Object -Property $ExportProperties
        switch ($PsCmdlet.ParameterSetName) {
            'Path' {
                $Params = @{
                    Encoding = $Encoding
                    Path = $Path
                    InputObject = $ExportToken
                }
                $Target = $Path
            }
            'LiteralPath' {
                $Params = @{
                    Encoding = $Encoding
                    LiteralPath = $LiterlPath
                    InputObject = $ExportToken
                }
                $Target = $LiteralPath
            }
        }
        if ($pscmdlet.ShouldProcess($Target)) {
            Export-Clixml @Params
        }
    }
}