I needed to run some simple tasks from a PowerShell session on a remote Windows computer. I thought this would be no problem – just fire up the PowerShell container that Microsoft provides and start a remote session but of course it is never that easy.
I will be using the mcr.microsoft.com/powershell:preview
container. To start it:
docker run \ --rm \ -it mcr.microsoft.com/powershell:preview
No WSMan
The container appears to be missing WSMan. You will probably get an error like this if you simply fire it up and try and Enter-PSSession:
PS /> $creds = Get-Credential PowerShell credential request Enter your credentials. User: DOMAIN\myuser Password for user DOMAIN\myuser: ********* PS /> Enter-PSSession -ComputerName my-computer -Credential $creds Enter-PSSession: This parameter set requires WSMan, and no supported WSMan client library was found. WSMan is either not installed or unavailable for this system. PS />
To fix this, the PSWSMan
module needs to be installed:
Install-Module -Name PSWSMan -Force Install-WSMan
After installing WSMan I got a message like this but I just ignored it:
WARNING: WSMan libs have been installed, please restart your PowerShell session to enable it in PowerShell
Should you need to start the session again you can just run pwsh
.
Success!
After installing the requirements finally it seems to work fine:
PS /> Enter-PSSession -ComputerName my-computer -Credential $creds [my-computer]: PS C:\WINDOWS\system32>
Note: MI_RESULT_ACCESS_DENIED
At first I was trying to use a different PowerShell container: mcr.microsoft.com/azure-powershell
When using the Enter-PSSession
cmdlet I was getting this:
PS /> Enter-PSSession -ComputerName my-computer -Credential $cred Enter-PSSession: MI_RESULT_ACCESS_DENIED PS /> Enter-PSSession -ComputerName my-computer -Credential $cred -Authentication Negotiate Enter-PSSession: Connecting to remote server my-computer failed with the following error message : MI_RESULT_ACCESS_DENIED For more information, see the about_Remote_Troubleshooting Help topic.
This was fixed by switching container image to mcr.microsoft.com/powershell:preview
.