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

Related questions 2 Powershell 'Get-StoredCredential' 194 Executing a command stored in a variable from PowerShell 1085 How do you comment out code in PowerShell? Related questions 2 Powershell 'Get-StoredCredential' 194 Executing a command stored in a variable from PowerShell 1085 How do you comment out code in PowerShell? 28 With PowerShell, how can I get Write-Debug output to appear in the console? 0 Powershell script to remove old cached Credentials (Error1168) 0 How to call powershell commands in a row from python 2 read a remote registry key with alternate credentials in powershell 0 script to map drive in powershell Load 5 more related questions Show fewer related questions

Reset to default

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like