Remote connect to a Windows via PowerShell in Linux

10,219

Look to PoSH over SSH instead.

See the step thru here:

PowerShell Remoting Over SSH

Overview

PowerShell remoting normally uses WinRM for connection negotiation and data transport. SSH was chosen for this remoting implementation since it is now available for both Linux and Windows platforms and allows true multiplatform PowerShell remoting. However, WinRM also provides a robust hosting model for PowerShell remote sessions which this implementation does not yet do. And this means that PowerShell remote endpoint configuration and JEA (Just Enough Administration) is not yet supported in this implementation.

PowerShell SSH remoting lets you do basic PowerShell session remoting between Windows and Linux machines. This is done by creating a PowerShell hosting process on the target machine as an SSH subsystem. Eventually this will be changed to a more general hosting model similar to how WinRM works in order to support endpoint configuration and JEA.

The New-PSSession, Enter-PSSession and Invoke-Command cmdlets now have a new parameter set to facilitate this new remoting connection

https://github.com/PowerShell/PowerShell/tree/master/demos/SSHRemoting

Updated with regards to Anthony Geoghegan comment

As for ---

'That looks promising but the link to the GitHub repository no longer works.'

----The link is directly to the MS PS repo, so, Yeppers it's odd that a 404 is happening. Either way, you can get this directly on your system by using PowerShell.

Anyway …

Find-Module -Name '*ssh*' | Format-table -AutoSize

# Results


Version Name                         Repository Description                                                                                                
------- ----                         ---------- -----------                                                                                                
2.1     Posh-SSH                     PSGallery  Provide SSH and SCP functionality for executing commands against remote hosts.                             
0.0.2.0 OpenSSHUtils                 PSGallery  Utilities and functions for configuring OpenSSH on Windows.                                                
2.1.3   SSHSessions                  PSGallery  Svendsen Tech's SSH-Sessions module provides SSH session creation, management and interaction from Power...
1.0.0   SSH                          PSGallery  Provides a PowerShell-based SSH client based on SSH.net  http://sshnet.codeplex.com/                       
0.0.75  PSSharedGoods                PSGallery  Module covering functions that are shared within multiple projects                                         
1.1.3   PowerSSH                     PSGallery  This module detects the first use of an SSH command, automatically runs the SSH agent, keeps the SSH aut...
0.9.5   WinSSH                       PSGallery  Install OpenSSH-Win64, optionally install ssh-agent and sshd Services. Also includes functions to help c...
1.0.1   ssh-wrapper                  PSGallery  Exposes ssh from WSL by wrapping: bash -c "ssh $args". Requires Windows Subsystem for Linux on Windows 10. 
0.3.1   posh-sshell                  PSGallery  Provides integration with ssh-agent and pageant from within Powershell                                     
2.0.1.8 SkypeForBusinessHybridHealth PSGallery  Uses on-premises modules such as Skype For Business and SkypeOnlineConnector to validate basic requireme...
1.0.4   PSShortcut                   PSGallery  This module eases working with Windows shortcuts (LNK and URL) files.                                      
1.1.4   PowerSSH-Legacy              PSGallery  This module detects the first use of an SSH command, automatically runs the SSH agent, keeps the SSH aut...
1.0     cEPRSSharepoint              PSGallery  DSCModule helps in installing & configuring the sharepoint site, Farm etc.,                                
0.0.3   dockeraccesshelper           PSGallery  Allow a user account to access the docker engine without elevated access rights                            
0.3     PSShareFile                  PSGallery  A PowerShell module to manipulate various objects in Citrix's ShareFile service                            
0.0.1   PSSherpaDesk                 PSGallery  PowerShell module for interacting with the SherpaDesk API  


Find-Module -Name '*Posh-SSH' | 
Save-Module -Path "$env:USERPROFILE\Documents\WindowsPowerShell\Modules" 
Install-Module -Name 'Posh-SSH'

Use this link for demo of SSHRemoting

I just hit it, so I know it works.

See these YouTube videos PowerShell SSH Remoting in action.

Share:
10,219
James
Author by

James

Updated on September 18, 2022

Comments

  • James
    James over 1 year

    I'm trying to connect to a Windows Machine via PowerShell in a Linux Machine to get some system's info, but I need to achieve it without use the WinRm.

    Firstly, I installed the PowerShell following the instructions here. Starting the PowerShell with pwsh worked fine.

    Then I tried to get some info through WMI using the following command:

    Get-WmiObject -Class Win32_Process -Impersonation 3 -ComputerName IP_ADDRESS
    

    The return is Get-WmiObject : The term 'Get-WmiObject' is not recognized as the name of a cmdlet, function, script file, or operable program..

    Following the changelog for the version 6.0 of PowerShell's core, I found that the Get-Wmi* functions should be replaced for Get-Cmi* equivalents. Let's try:

    Get-CimInstance -Class Win32_Process -Impersonation 3 -ComputerName IP_ADDRESS
    

    The return is: Get-CimInstance : The term 'Get-CimInstance' is not recognized as the name of a cmdlet, function, script file, or operable program.

    Looking for the list of commands, typing Get- and then Tab, the return indeed does not show any Wmi or Cim function.

    I'm able to connect to the same computer using the cmdlet Invoke-Command, but as I said, I need to achieve it without the use of WinRm, and, apparently, this is not the case of Invoke-Comand

    After a lot of Google search, I found only one similar question here in SO, but it user the cmdlet Enter-PSSession, that's also use WinRM if I understood correctly.

    Finally, I found this blog post with fits like a glove with my needs. However, It suggest the use of the cmdlets Invoke-WmiMethod and Invoke-CimMethod that, for surprise of nobody, aren't recognized commands.

    My question is: Are there anyway to run a WMI query to get some Windows info through PowerShell in a linux machine without the use of WinRm?

    Note1: I'm able to run the Get-Wmi* and Get-Cim* cmdlets in Windows machine PowerShell (connected via RPC, for example);

    Note2: I know a workaround to deal with it in linux without PowerShell and it should work for someone with similar problem, but it didn't work for me due to an unsolvable (for now, at least) encoding problem;

    INFO

    • OS: Debian 8.10

    • $PSVersionTable.PSVersion: 6.0.1

    • Remote Windows: W10 Pro

  • James
    James about 6 years
    Thanks for your help. It really worked. Just a side note, this SSH approach will work only with recent versions of Windows.
  • James
    James about 6 years
    I marked this answer as correct since it is the only one, but it would be good to know some alternative that works with old versions of Windows, it means, a solution to connect to any windows' version through the PowerShell in linux.
  • postanote
    postanote about 6 years
    No worries. But understand that this will not happen anytime soon. The statement 'any version of Windows' is far to broad. There is little reason to target legacy unsupported Windows. If you had said, all supported business windows version, I'd be inclined to agree. Yet, know, the even today, not all of PoSH tools are available on all versions of Windows. v5 on Win7 is not the same a v5 on Win10 for example and v6 is a far cry from v5x on Windows. So, as an OSS offering now, it's really up to the community.
  • James
    James about 6 years
    You are right. All versions of windows is something too broad, but a solution that works in some of the old versions would be good.
  • postanote
    postanote about 5 years
    Anthony Geoghegan -- see the update I just did for your query.