Access Linux via PowerShell

Follow the link below to install PowerShell in Linux Ubuntu

https://learn.microsoft.com/en-us/powershell/scripting/install/install-ubuntu?view=powershell-7.2

# Update the list of packages
sudo apt-get update
# Install pre-requisite packages.
sudo apt-get install -y wget apt-transport-https software-properties-common
# Download the Microsoft repository GPG keys
wget -q "https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb"
# Register the Microsoft repository GPG keys
sudo dpkg -i packages-microsoft-prod.deb
# Update the list of packages after we added packages.microsoft.com
sudo apt-get update
# Install PowerShell
sudo apt-get install -y powershell
# Start PowerShell
pwsh

Now, we will access Ubuntu via PowerShell on Windows Server 2016.

Install OpenSSH in Windows 2016.

Follow the link below to download OpenSSH-Win64-8.9.10.msi. Then double click to install OpenSSH.

https://github.com/PowerShell/Win32-OpenSSH/releases/tag/v8.9.1.0p1-Beta

An OpenSSH folder is created under C:\Program Files\OpenSSH directory.

We create a PS session from Windows Server 2016 to Linux Ubuntu (ubuntu-desktop.cisa.local) via OpenSSH.

$session = New-PSSession -HostName ubuntu-desktop.cisa.local -UserName tung -SSHTransport
The error message happens because the cmdlet doesn’t find the ssh.exe file.

To solve the issue:

  • Add the OpenSSH path into environment variable and restart Windows 2016 server
  • OR copy ssh.exe to C:\Windows and C:\Windows\System32 directory.

Establish a PS connection to Linux Ubuntu via OpenSSH.

$session = New-PSSession -HostName ubuntu-desktop.cisa.local -UserName tung -SSHTransport
Enter-PSSession $session
Invoke-Command -Session $session -ScriptBlock {Get-Process}
Invoke-Command -Session $session -ScriptBlock {ip a}
Invoke-Command -Session $session -ScriptBlock {netstat -antp}

It can be seen that an OpenSSH connection has been established between Windows server 2016 and Linux Ubuntu.