Skip to content

Instantly share code, notes, and snippets.

@adriaanthomas
Last active May 19, 2017 14:02
Show Gist options
  • Save adriaanthomas/8d4f7ed9c69814e71bd7acc2e2d2f049 to your computer and use it in GitHub Desktop.
Save adriaanthomas/8d4f7ed9c69814e71bd7acc2e2d2f049 to your computer and use it in GitHub Desktop.
Accessing Windows PowerShell with AzureRM from a Linux client

Accessing Windows PowerShell AzureRM from a Linux/MacOS client

Following this description we attempted to create a Windows Docker container that will allow remote (Linux) clients to log in and run Windows PowerShell commands, in our case particularly so that they can use the full set of AzureRM PowerShell CmdLets.

Server

On a Windows Server 2016, build and run the container using the Dockerfile from this gist. Note that the sshd_config has an added subsystem for powershell.

docker build --tag sshserver .
docker run -ti -p 2222:22 sshserver

(you may have to open port 2222 on the host's firewall)

Client

Direct SSH

On any other machine, the following now works but is ugly (and certain key-combinations like Ctrl+C don't work):

ssh -p 2222 user@<IP_address_of_win2016_host>

We will end up in a cmd session, but after entering powershell we can run our commands. As mentioned though, this is not ideal.

Remote PowerShell over SSH

On most OSX clients running PowerShell Core 6.0.0-beta.1 the following works (log in using normal ssh first to ensure ~/.ssh/known_hosts has been updated properly; there appears to be a bug in the PowerShell SSH client):

powershell
Enter-PSSession -HostName <IP_address_of_win2016_host> -UserName user -SSHTransport -Verbose -Port 2222

This will give a useable system.

Strangely enough, the same never appears to work from a Linux PowerShell Docker container (at the time of this writing, the PowerShell version is identical):

docker run -ti --rm microsoft/powershell
Enter-PSSession -HostName <IP_address_of_win2016_host> -UserName user -SSHTransport -Verbose -Port 2222

Results in:

Enter-PSSession : An error has occurred which Windows PowerShell cannot handle. A remote session might have ended.
At line:1 char:1
+ Enter-PSSession -HostName <IP_address_of_win2016_host> -UserName user -SSHTran ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (:) [Enter-PSSession], PSRemotingDataStructureException
    + FullyQualifiedErrorId : System.Management.Automation.Remoting.PSRemotingDataStructureException,Microsoft.PowerShell.Commands.EnterPSSessionCommand
FROM microsoft/windowsservercore
SHELL ["powershell.exe", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
RUN iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
RUN choco install -y openssh -params '"/SSHServerFeature"'
RUN Invoke-WebRequest 'https://github.com/PowerShell/PowerShell/releases/download/v6.0.0-beta.1/PowerShell-6.0.0-beta.1-win10-win2016-x64.msi' -Outfile powershell.msi; \
Start-Process powershell.msi /qn -Wait
ADD ["sshd_config", "C:/Program Files/OpenSSH-Win64/sshd_config"]
RUN New-LocalUser -Name 'user' -Password (ConvertTo-SecureString -AsPlainText -Force 'passw0rd') -PasswordNeverExpires -AccountNeverExpires -UserMayNotChangePassword
EXPOSE 22
# SSH server is now set up
# Now install AzureRM modules as this is what we really wanted. Note that we have to install them in PS6 which is what
# we use for remote SSH connections.
SHELL ["C:/Program Files/PowerShell/6.0.0-beta.1/powershell.exe", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
RUN Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
RUN Install-Module AzureRM -Force
# for debugging run PowerShell
CMD ["powershell.exe"]
# $OpenBSD: sshd_config,v 1.84 2011/05/23 03:30:07 djm Exp $
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/bin:/bin:/usr/sbin:/sbin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
# The default requires explicit activation of protocol 1
#Protocol 2
# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 1h
#ServerKeyBits 1024
# Logging
# obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
LogLevel DEBUG3
# Authentication:
#LoginGraceTime 2m
#PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
#RSAAuthentication yes
#PubkeyAuthentication yes
# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile .ssh/authorized_keys
# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#RhostsRSAAuthentication no
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes
# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication yes
#PermitEmptyPasswords no
# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
#UsePAM no
#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
#X11Forwarding no
#X11DisplayOffset 10
#X11UseLocalhost yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS yes
#PidFile /var/run/sshd.pid
#MaxStartups 10
#PermitTunnel no
#ChrootDirectory none
# no default banner path
#Banner none
# override default of no subsystems
Subsystem sftp sftp-server.exe
Subsystem powershell C:/Program Files/PowerShell/6.0.0-beta.1/powershell.exe -sshs -nologo -noprofile
# Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# ForceCommand cvs server
# PubkeyAcceptedKeyTypes ssh-ed25519*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment