DevOpsHandling/Get-DevopsToken.ps1

<#
.SYNOPSIS
Gets a PAT for DevOps
.DESCRIPTION
Gets a PAT for DevOps. If it was not stored yet, it will ask user for PAT
.PARAMETER sourcePath
Source path of the project settings file. If left blank, it will retrieve it from the module store
.EXAMPLE
$token = Get-DevOpsToken -SourcePath C:\temp\
#>

function Get-DevOpsToken {
    param(
        [Parameter(Mandatory = $false)]
        [string]$sourcePath = "",
        [Parameter(Mandatory = $false)]
        [string]$devOpsOrganization = "",
        [Parameter(Mandatory = $false)]
        [string]$devOpsToken = ""
    )

    if ($devOpsToken -ne "" -and $null -ne $devOpsToken -and $devOpsOrganization -ne "") {
        $tempToken = $devOpsToken
        $sourcePath = ""
        $devOpsToken = ""
    }

    if ($devOpsToken -eq "") {
        if ($sourcePath -ne "") {
            $devOpsToken = Get-EnvironmentKeyValue -SourcePath $sourcePath -KeyName "token"
        }
        if ($null -eq $devOpsToken -or $devOpsToken -eq "") {
            $vaultName = "NAV-X"
            if ($devOpsOrganization -eq "" -and $sourcePath -ne "") {
                $devOpsOrganization = Get-EnvironmentKeyValue -SourcePath $sourcePath -KeyName "organization"
            }
            switch ($devOpsOrganization) {
                "cfbs-us" {
                    $vaultName = "CfbsDev"
                    break
                }
            }
            $devOpsToken = Get-Secret -vaultName $vaultName -secretName "personalDevOpsToken"
        }
    }

    if ($devOpsToken -eq "" -and $null -ne $tempToken) {
        $devOpsToken = $tempToken
    }

    $devOpsToken
}