Using Get-Storedcredential Function in vs Code

I use a Get-StoredCredential function in my Powershell ISE profile to authenticate the various modules I use, which avoids the need for authentication prompts when loading PS modules

I would now like to start using VS Code with the PS extension as my primary script editor, instead of PS ISE.

When I try and load my profile script into VS Code however, the process errors when trying to execute the portion of the script that authenticates the modules.

The Get-StoredCredential function runs successfully, however it would appear that the modules in the script do not like being passed the .cred files

For example:

when the following portion of the script is run, an error is displayed:

example

I guess what I could do to know is:

  • is the use of stored credentials via .cred files supported in VS Code?
  • if so is there a workaround to be able to authenticate against the various MS online modules?
  • Is the issue with the modules, VS Code, the PS extension or all 3?

I'm very new to VS Code so apologies in advance for any noobness

Thanks

ran the functions below in isolation - the Get-StoredCredential -List successfully lists the stored creds once executed.

$keypath = 'C:\Users\user1\OneDrive\Keys'

Function New-StoredCredential {
    
    if (!(Test-Path Variable:\KeyPath)) {
        Write-Warning "The `$KeyPath variable has not been set. Consider adding `$KeyPath to your PowerShell profile to avoid this prompt."
        $path = Read-Host -Prompt "Enter a path for stored credentials"
        Set-Variable -Name KeyPath -Scope Global -Value $path

        if (!(Test-Path $KeyPath)) {
        
            try {
                New-Item -ItemType Directory -Path $KeyPath -ErrorAction STOP | Out-Null
            }
            catch {
                throw $_.Exception.Message
            }           
        }
    }

    $Credential = Get-Credential -Message "Enter a user name and password"

    $Credential.Password | ConvertFrom-SecureString | Out-File "$($KeyPath)\$($Credential.Username).cred" -Force

}


Function Get-StoredCredential {
 

    param(
        [Parameter(Mandatory=$false, ParameterSetName="Get")]
        [string]$UserName,
        [Parameter(Mandatory=$false, ParameterSetName="List")]
        [switch]$List
        )

    if (!(Test-Path Variable:\KeyPath)) {
        Write-Warning "The `$KeyPath variable has not been set. Consider adding `$KeyPath to your PowerShell profile to avoid this prompt."
        $path = Read-Host -Prompt "Enter a path for stored credentials"
        Set-Variable -Name KeyPath -Scope Global -Value $path
    }


    if ($List) {

        try {
        $CredentialList = @(Get-ChildItem -Path $keypath -Filter *.cred -ErrorAction STOP)

        foreach ($Cred in $CredentialList) {
            Write-Host "Username: $($Cred.BaseName)"
            }
        }
        catch {
            Write-Warning $_.Exception.Message
        }

    }

    if ($UserName) {
        if (Test-Path "$($KeyPath)\$($Username).cred") {
        
            $PwdSecureString = Get-Content "$($KeyPath)\$($Username).cred" | ConvertTo-SecureString
            
            $Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $PwdSecureString
        }
        else {
            throw "Unable to locate a credential for $($Username)"
        }

        return $Credential
    }
}
3
Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article