Private/Get-CrumbHeader.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
function Get-CrumbHeader {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [string] $Username,
        [Parameter(Mandatory=$true)]
        [securestring] $Password
    )

    if (!$script:crumbsByUser) {
        $script:crumbsByUser = [hashtable]::new()
    }

    if ($crumbHeader = $crumbsByUser[$Username]) {
        return $crumbHeader
    } else {
        $uri = "$script:jenkinsUrl/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,`":`",//crumb)"
        $basicAuthCreds = ConvertTo-BasicAuth -Username $Username -Password $Password
        $requestHeaders = @{ Authorization = "Basic $basicAuthCreds" }
        $crumbHeader = Invoke-RestMethod -Uri $uri -Method "Get" -Headers $requestHeaders -UseBasicParsing
        $script:crumbsByUser[$Username] = $crumbHeader.Replace(":", "=")
        return $crumbsByUser[$Username]
    }
}