Get-TFSStoredCredential.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
# Author: Miodrag Milic <miodrag.milic@gmail.com>
# Last Change: 18-May-2016.

<#
.SYNOPSIS
    Get saved TFS credential from the Windows Credential Manager. If none is available, create and store one.
#>

function Get-TFSStoredCredential {
    [CmdletBinding()]
    param()

    if ($global:tfs.root_url -eq $null) { throw 'You must set $global:tfs.root_url in order to get stored credentials' }
    if (gmo -ListAvailable CredentialManager -ea 0)  {
        $cm = $true
        try {
            Write-Verbose "Trying to get storred credentials for '$($global:tfs.root_url)'"
            $cred = Get-StoredCredential -Target $global:tfs.root_url
        } catch {
            if ($_.Exception.Message -ne 'CredRead failed with the error code 1168.') { throw $_ }
        }
    }

    if ($cred -eq $null) { $cred = New-TFSCredential } else { Write-Verbose 'Stored credentials retrieved' }

    $cred
}