Searching for an answer to this finds a lot about GitHub credentials but nothing on this specific issue. I am starting to work with the PowerShell (PoSH) Cmdlet 'Get-Credential' because my company uses Two-factor Authentication (2FA), so a username/password won't work to remote from my desktop (user account) to a server (Admin account). Additionally, I have just started using VS Code. The issue is that when running a simple PoSH snippet, in PoSH ISE using Get-Credential I get a popup that lets me select the certificate that I need and then enter a Pin. When I run this same snippet in VS Code, the popup never appears. Does anyone understand why this is, and can I resolve it?
Here is the snippet for reference.
$serverList = Get-Content "C:\temp\Servers.txt"
$cred = Get-Credential
ForEach ($Server in $serverList){
$OS = Invoke-Command -Credential $cred -ComputerName $Server -ScriptBlock {
(get-itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ProductName).ProductName
}
Write-Host $Server, $OS
}
Thanks all.
TL;DR: That behaviour is by design. VS Code is the result of Microsoft's cross-platform development experience efforts. The PowerShell version is the cross-platform PowerShell Core by default. It's focused on the terminal experience and scripts. So, you cannot trigger the Windows native credential prompt on VS Code.
When you checked the version with $PSVERSIONTABLE, you'll see something like this:
=====> PowerShell Integrated Console v2021.12.0 <=====
PS C:\Users\Foo> $PSVersionTable
Name Value
---- -----
PSVersion 7.2.1
PSEdition Core
GitCommitId 7.2.1
OS Microsoft Windows 10.0.18363
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion
By default, this is not powershell.exe
, meaning it is not Windows native and Powershell 5.x or lower that you used with ISE or console. It is VS Code host utilizing pwsh.exe
, the cross-platform version with version 6.0 and higher.
You can check the documentation: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.security/get-credential?view=powershell-7.2
You can see that with 6.x and above, it is always focused on scripts and terminals. The bad news is VS Code is also built around the cross-platform approach. So even if you change the profiles in the terminal to use Windows Powershell like the one below, you can use PowerShell 5.1 but cannot view that prompt:
"PowerShell Core": {
"path": "C:\\Program Files\\PowerShell\\7\\pwsh.exe"
},
"Windows PowerShell (x64)": {
"path": "${env:windir}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
}
In sum, you cannot get that prompt with VS Code. Except for typing powershell.exe
in the Integrated Terminal and pasting code there, basically not using VS Code. It won't help you. I'd stick to ISE for this requirement, beware that it's depreciated.
Edit 1: Added TL;DR.
Edit 2: I created a cmdlet that only pops up the default credential dialog and returns a PSCredential
object.
Edit 3: I published a package, called Get-WinCredential, that allows you to utilize the credential dialog, both legacy and modern one. It has been a good exercise for me.
Just like other comment owners, I utilized P/Invoke
.