How to use Get-Credential with Windows containers
Published Jan 23 2023 09:29 AM 2,085 Views

The idea on this blog post came from an issue opened by an user on the Windows Containers GitHub Repo. I thought the problem faced by the user should be common enough that others might be interested in a solution.

 

Get-Credential cmdlet pop-up

If you use PowerShell, you most likely came across the Get-Credential cmdlet at some point. It’s extremely useful for situations on which you want to set a username and password to be used in a script, variable, etc.. However, the way Get-Credential works is by providing a pop-up window for you to enter the credentials:

Get-Credential01.png

On a traditional Windows environment, this is totally fine as the pop-up window shows up, you enter the username and password, and save the information. However, on a Windows container there’s no place to display the pop-up window:

Get-Credential02.png

As you can see on the image above, the command hangs waiting for the confirmation, but nothing happens as the pop-up is not being displayed. Even typing CRTL+C doesn’t work. In my case, I had to close the PowerShell window, which left the container in an exited state.

 

Changing the Get-Credential behavior

To work around this issue, you can change the PowerShell policy to accept credential input from the console session. Here’s the script for that workaround:

$key = "HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds"
Set-ItemProperty -Path $key -Name ConsolePrompting -Value $true

The next time you use the Get-Credential cmdlet, it will ask for the username and password on the console session:

Get-Credential03.png

On the example above, I simply entered the username and password for the Get-Credential cmdlet. You could, obviously, save that on a variable for later use.

 

While this workaround solves the problem of not being able to use the Get-Credential cmdlet on Windows containers, it’s obviously not ideal. The information from the product team is that they are looking into making this the default option for Windows containers in the future – although, no timelines are available at this moment.

 

I hope this is useful to you! Let us know in the comments!

Version history
Last update:
‎Jan 23 2023 09:29 AM
Updated by: