Managing Windows Powershell from Linux terminal

Solution 1:

Some options:

  1. Install an SSH server on a Windows machine, SSH in, run Powershell on the Windows box
  2. Run an RDP client on Linux, RDP in, run Powershell on the Windows box
  3. Use Powershell Web Access, hit the POSH WA gateway server with a browser, run Powershell via the browser

http://technet.microsoft.com/en-us/library/hh831611.aspx

I'm not aware of any POSH executables for Linux, so you can't run POSH commands directly from a shell in Linux - you must first get into the Windows machine somehow, like RDP, remote console (DRAC, iLO), SSH, or POSH Web Access.

/Edit- looks like there are some options. http://sharpcodenotes.blogspot.com/2014/01/running-powershell-commands-from-linux.html

You could try and get OpenWSMan to do what you want, but i think you'd have to craft every command like a URL request rather than simply issue the correct Powershell command - as I said, there's no actual Powershell for Linux. You could install the WinRB ruby gem on every Windows machine and then run Ruby code on your Linux box to call Powershell commands.

In neither case does it seem that you will get the interactive Powershell experience, like tab-completion, help, etc. These seem to be for running a script or a single command, and you'll presumably have to capture any errors or other feedback on your own. You're not going to get a first-class solution on Linux.

/Edit again - yes, MSDN says that this will tend to be ugly/clunky. Even the simple/effective solution that he gives looks like no fun.

Although Powershell remoting is built on top of WS-Management, it’s really a protocol within a protocol and trying to interop with PSRP (Powershell Remoting Protocol) directly would essentially require replicating Powershell on the client.

Solution 2:

Microsoft is officially releasing Powershell on Linux and OSX. See the August 2016 announcement: PowerShell is open sourced and is available on Linux

It's currently in alpha. Ubuntu and Centos are officially supported, more on the way. If you're adventurous, installation instructions are available here: https://github.com/PowerShell/PowerShell/blob/master/docs/installation/linux.md


Solution 3:

Yes, you have 2 options:

  1. You can install SSHd server on windows and in configuration set up path to the PowerShell.

    For this you can use tutorial for installation of SSHd and configuration PowerShell path.

    In short:

    1. Install freeSSHd
    2. Add new user account and allow SSH access
    3. Enable SSH port in Windows Firewall
    4. Configure path to PowerShell.exe
  2. PowerShell Web Access (Only Win Server 2012 R2)

    There you have one tutorial on Technet -- blogs.technet.com/b/canitpro/archive/2013/11/14/step-by-step-deploy-powershell-web-access.aspx


Solution 4:

Tested in Ubuntu 14.04 LTS

Download and install winexe:

wget https://passing-the-hash.googlecode.com/files/winexePTH1.1.0-1.deb

dpkg -i winexePTH1.1.0-1.deb

Basic usage

winexe -U DOMAIN/USERNAME%PASSWORD //MACHINEIP "cmd.exe"

As an example:

echo -ne '\n' | winexe -U DOMAIN/USERNAME%PASSWORD //IP 'powershell.exe -command "Get-Process | Select-Object Name, ProcessName"' | sort -u

Solution 5:

You can also install win32-openSSH by hand from https://github.com/PowerShell/Win32-OpenSSH or through Chocolatey https://chocolatey.org or even faster through Boxstarter (which utilizes Chocolatey under the hood) https://boxstarter.org (fast install https://boxstarter.org/packages/nr/win32-openssh ).

It takes a little setup, but it is a native (yeah, for real!) win32 port maintained by the Microsoft guys doing PowerShell.

Otherwise as somebody mentioned you could install the WinRM gem on Linux and get a little bit of connectivity over the WinRM protocol, but that's not as nice as just starting a PSSession.

Additionally the folks behind the WinRM gem (WinRb) are also looking at implementing a more native PowerShell interaction that tunnels PowerShell over WinRM which will be more functional for uploads and faster for other operations as well. See this blog post: http://www.hurryupandwait.io/blog/a-look-under-the-hood-at-powershell-remoting-through-a-ruby-cross-plaform-lens