Remoting and Automation

Managing 200 Servers from My Desk

Five years ago, applying a configuration change meant: RDP to each server, open PowerShell, run commands, document results, repeat 200 times. A full day's work for a simple change.

Today, with PowerShell remoting:

Invoke-Command -ComputerName (Get-Content servers.txt) -ScriptBlock {
    Set-Service -Name "wuauserv" -StartupType Automatic
    Start-Service -Name "wuauserv"
} | Select-Object PSComputerName, Status

Five minutes. Same change. 200 servers. That's the power of remoting.

PowerShell Remoting Basics

PowerShell remoting uses WinRM (Windows Remote Management) or SSH to execute commands on remote computers.

Enabling WinRM

On Windows Server, WinRM is usually enabled. On client:

# Run as Administrator
Enable-PSRemoting -Force

# Check WinRM service
Get-Service WinRM

# Test remoting (to itself)
Test-WSMan localhost

One-to-One Remoting: Enter-PSSession

Interactive session with remote computer:

One-to-Many Remoting: Invoke-Command

Execute commands on multiple computers:

Using Credentials

PowerShell Sessions

Persistent connections for multiple commands.

Creating Sessions

Multiple Sessions

Session Benefits

  • Faster: Reuse connection

  • State preservation: Variables persist between commands

  • Efficiency: One connection, multiple commands

SSH Remoting (PowerShell 7+)

Cross-platform remoting using SSH.

Setup SSH

Passing Arguments

Using -ArgumentList

Multiple Arguments

Using $using:

Background Jobs

Run commands asynchronously.

Starting Jobs

Remote Jobs

Real-World Examples

Health Check Across Servers

Parallel Deployment

Log Collection

Security Considerations

Using CredSSP (Carefully)

Just Enough Administration (JEA)

Restrict remote users to specific commands.

Common Pitfalls

1. Firewall Blocking WinRM

Problem: Can't connect remotely Solution: Ensure WinRM ports (5985, 5986) are open

2. The Double-Hop Problem

Problem: Can't access resources from remote session Solution: Use CredSSP or pass credentials explicitly

3. Forgetting to Remove Sessions/Jobs

Problem: Resource leaks Solution: Always cleanup sessions and jobs

Key Takeaways

  • Enter-PSSession: Interactive remote shell

  • Invoke-Command: Execute on multiple computers

  • New-PSSession: Persistent connections

  • Jobs: Asynchronous execution

  • SSH: Cross-platform remoting

  • Always cleanup: Remove sessions and jobs

What You've Learned

✅ PowerShell remoting with WinRM and SSH ✅ Interactive and batch remote execution ✅ Using sessions for efficiency ✅ Background jobs for async operations ✅ Real-world remote management patterns ✅ Security and troubleshooting

Next Steps

Now you can manage systems remotely. Let's integrate with external services. In Working with APIs and Web Data, you'll learn:

  • REST API calls with Invoke-RestMethod

  • Authentication and headers

  • JSON and XML processing

  • Real-world API integrations

Connect PowerShell to the wider world.


Ready to work with APIs? Continue to Working with APIs and Web Data

Last updated