Cross-Platform Windows PowerShell Management: Accessing Windows Server 2012 from Linux via SSH-like CLI


1 views

html

As a Linux administrator working with Windows Server 2012 systems, I needed a solution to execute PowerShell commands remotely from my Ubuntu workstation. While SSH is the standard for Linux server management, Windows environments traditionally required different approaches.

Windows PowerShell includes powerful remoting capabilities through WS-Management (WinRM). To enable this on your Windows Server 2012:

# On Windows Server 2012:
Enable-PSRemoting -Force
Set-Item WSMan:\localhost\Client\TrustedHosts * -Force
Restart-Service WinRM

On your Ubuntu workstation, install the necessary tools:

sudo apt-get install -y openssh-client
sudo apt-get install -y powershell # For PowerShell Core

Using PowerShell Core on Linux, you can now connect:

pwsh
$cred = Get-Credential
Enter-PSSession -ComputerName windows-server-2012 -Credential $cred

Newer Windows versions (and Server 2019+) support native SSH for PowerShell:

# On Windows (if supported):
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Start-Service sshd

Execute common administration tasks remotely:

# Single command execution
Invoke-Command -ComputerName win-server -ScriptBlock { Get-Service } -Credential $cred

# Script file execution
Invoke-Command -ComputerName win-server -FilePath ./admin_script.ps1

Common issues and solutions:

  • Ensure WinRM listener is configured: winrm enumerate winrm/config/listener
  • Check firewall rules allow WinRM traffic (port 5985/5986)
  • Verify PowerShell execution policy: Set-ExecutionPolicy RemoteSigned

As a sysadmin working with mixed environments, I frequently need to manage Windows Server 2012 systems from my Ubuntu workstation. While SSH is my go-to for Linux servers, Windows traditionally required different approaches. Here's how I've solved this workflow challenge.

Windows PowerShell includes a remoting capability similar to SSH called WS-Management (WinRM). To enable this on your Windows Server 2012:

# On Windows Server 2012:
Enable-PSRemoting -Force
Set-Item WSMan:\localhost\Client\TrustedHosts * -Force
Restart-Service WinRM

On your Ubuntu workstation, you'll need these packages:

sudo apt-get install -y openssh-client python3-pip
pip3 install pywinrm

Using the pywinrm Python library, you can execute PowerShell commands remotely:

import winrm

session = winrm.Session('windows-server-ip', 
                       auth=('username', 'password'),
                       transport='ntlm')

result = session.run_ps('Get-Service | Where Status -eq "Running"')
print(result.std_out)

Another approach is to install PowerShell Core on your Linux machine and connect directly:

# Install PowerShell Core on Ubuntu
sudo apt-get install -y wget
wget -q https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
sudo apt-get update
sudo apt-get install -y powershell

# Connect to Windows Server
pwsh
Enter-PSSession -ComputerName windows-server-ip -Credential (Get-Credential)

For production environments, consider these security enhancements:

  • Use SSL/TLS for WinRM connections
  • Restrict TrustedHosts to specific IPs
  • Implement certificate-based authentication
  • Configure firewalls carefully (TCP 5985/5986)

Here's how to check IIS status from your Linux terminal:

import winrm

s = winrm.Session('windows-server-ip', auth=('admin', 'P@ssw0rd'))
r = s.run_ps('Get-WindowsFeature Web-Server | Select Installed')
print("IIS installed status:", r.std_out.strip())

Common issues and solutions:

  • Double-check WinRM service is running on Windows
  • Verify network connectivity and firewall rules
  • Test with basic commands before complex scripts
  • Check event logs on Windows for authentication errors