Public/Remove-JiraSession.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
function Remove-JiraSession {
    <#
    .Synopsis
       Removes a persistent JIRA authenticated session
    .DESCRIPTION
       This function removes a persistent JIRA authenticated session and closes the session for JIRA.
       This can be used to "log out" of JIRA once work is complete.
 
       If called with the Session parameter, this function will attempt to close the provided
       JiraPS.Session object.
 
       If called with no parameters, this function will close the saved JIRA session in the module's
       PrivateData.
    .EXAMPLE
       New-JiraSession -Credential (Get-Credential jiraUsername)
       Get-JiraIssue TEST-01
       Remove-JiraSession
       This example creates a JIRA session for jiraUsername, runs Get-JiraIssue, and closes the JIRA session.
    .EXAMPLE
       $s = New-JiraSession -Credential (Get-Credential jiraUsername)
       Remove-JiraSession $s
       This example creates a JIRA session and saves it to a variable, then uses the variable reference to
       close the session.
    .INPUTS
       [JiraPS.Session] A Session object to close.
    .OUTPUTS
       [JiraPS.Session] An object representing the Jira session
    #>

    [CmdletBinding(SupportsShouldProcess = $false)]
    [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseShouldProcessForStateChangingFunctions', '')]
    param(
        # A Jira session to be closed. If not specified, this function will use a saved session.
        [Parameter(
            Position = 0,
            Mandatory = $false,
            ValueFromPipeline = $true
        )]
        [Object] $Session
    )

    begin {
        try {
            Write-Debug "[Remove-JiraSession] Reading Jira server from config file"
            $server = Get-JiraConfigServer -ConfigFile $ConfigFile -ErrorAction Stop
        }
        catch {
            $err = $_
            Write-Debug "[Remove-JiraSession] Encountered an error reading configuration data."
            throw $err
        }

        $uri = "$server/rest/auth/1/session"

        $headers = @{
            'Content-Type' = 'application/json';
        }
    }

    process {
        if ($Session) {
            Write-Debug "[Remove-JiraSession] Validating Session parameter"
            if ((Get-Member -InputObject $Session).TypeName -eq 'JiraPS.Session') {
                Write-Debug "[Remove-JiraSession] Successfully parsed Session parameter as a JiraPS.Session object"
            }
            else {
                Write-Debug "[Remove-JiraSession] Session parameter is not a JiraPS.Session object. Throwing exception"
                throw "Unable to parse parameter [$Session] as a JiraPS.Session object"
            }
        }
        else {
            Write-Debug "[Remove-JiraSession] Session parameter was not supplied. Checking for saved session in module PrivateData"
            $Session = Get-JiraSession
        }

        if ($Session) {
            Write-Debug "[Remove-JiraSession] Preparing for blastoff!"

            try {
                $webResponse = Invoke-WebRequest -Uri $uri -Headers $headers -Method Delete -WebSession $Session.WebSession

                Write-Debug "[Remove-JiraSession] Removing session from module's PrivateData"
                if ($MyInvocation.MyCommand.Module.PrivateData) {
                    Write-Debug "[Remove-JiraSession] Removing session from existing module PrivateData"
                    $MyInvocation.MyCommand.Module.PrivateData.Session = $null;
                }
                else {
                    Write-Debug "[Remove-JiraSession] Creating module PrivateData"
                    $MyInvocation.MyCommand.Module.PrivateData = @{
                        'Session' = $null;
                    }
                }
            }
            catch {
                $err = $_
                $webResponse = $err.Exception.Response
                Write-Debug "[Remove-JiraSession] Encountered an exception from the Jira server: $err"

                Write-Warning "JIRA returned HTTP error $($webResponse.StatusCode.value__) - $($webResponse.StatusCode)"

                # Retrieve body of HTTP response - this contains more useful information about exactly why the error
                # occurred
                $readStream = New-Object -TypeName System.IO.StreamReader -ArgumentList ($webResponse.GetResponseStream())
                $body = $readStream.ReadToEnd()
                $readStream.Close()
                Write-Debug "Retrieved body of HTTP response for more information about the error (`$body)"
                ConvertFrom-Json2 -InputObject $body
            }
        }
        else {
            Write-Verbose "No Jira session is saved."
        }
    }
}