Public/Set-PDKGithubCredential.ps1

Function Set-PDKGithubCredential {
    <#
    .SYNOPSIS
        .
    .PARAMETER Credential
        .
    .PARAMETER Token
        .
    .EXAMPLE
        PS C:\> Set-PDKGithubCredential
    .EXAMPLE
        PS C:\> Set-PDKGithubCredential -Credential (get-credential)
    .EXAMPLE
        PS C:\> Set-PDKGithubCredential -Token "MyMagicGithubToken"
    .NOTES
        File Name : Set-PDKGithubCredential.ps1
        Author : Thomas ILLIET (contact@thomas-illiet.fr)
    #>

    [CmdletBinding(DefaultParameterSetName='Basic')]
    [OutputType([Void])]
    Param(
        [Parameter(ParameterSetName='Basic', Mandatory=$True, Position=0)]
        [System.Management.Automation.PSCredential]$Credential,
        [Parameter(ParameterSetName='Token', Mandatory=$True, Position=0)]
        [String]$Token,
        [parameter(Mandatory = $False)]
        [Switch]$PassThru
    )

    switch ($PSCmdlet.ParameterSetName) {

        # Basic Authentication
        Basic {
            $AuthString = "{0}:{1}" -f $Credential.Username,(Get-PDKPlainPassword -SecureString $Credential.Password)
            $AuthBytes  = [System.Text.Encoding]::Ascii.GetBytes($AuthString)
            $AuthBase64 = [Convert]::ToBase64String($AuthBytes)
            $Script:GithubAuthorization = "Basic $AuthBase64"
        }

        # Token Authentication
        Token {
            $Script:GithubAuthorization = "Token $Token"
        }
    }

    # Return GithubAuthorization if PassThru is defined
    if ($PassThru) { return $Script:GithubAuthorization }
}