Summary: There are cases where it’s necessary to use Windows Remote Management (WinRM), also known as WS-Management (WS-Man) to automate Windows Servers (especially Windows Server that are behind a Windows hop server). This is handy when there is no direct network access to the Windows server that need to be reached (typically for security reasons).
In this example, the following command is executed on the ThirdServer (through the FirstServer and then the SecondServer) in order to update a firewall rule to allow the WinRM service to respond to any source computer request (rather than just the local subnet).
Set-NetFirewallRule -Name WINRM-HTTP-In-TCP-PUBLIC -Action "Allow" -Direction "Inbound" -RemoteAddress "Any"
The default configuration for the WinRM firewall rule in Windows Server 2012+ is to only allow WinRM requests that originate from the local subnet of that server. This command changes a firewall rule to open WinRM to respond to requests from any source IP address.
In addition, for environments that require multi-hop access over and to Windows Servers, RDP can be problematic if there are any network bandwidth or latency issues. For actions that don’t require access to the Windows desktop, WinRM is ideal since it is much more efficient and faster.
Note: The authentication token for the session on the ThirdServer may be reduced compared to the access available for the FirstServer. Specifically for access to external resources like network shares.
MultiHop-ConfigWinRm.ps1
# Version:: 0.1.0 (1/13/2016) # Script Description:: Expands WinRM scope. # # Author(s):: Otto Helweg # Write-Host "Configuring WinRM for remote access..." # Get the necessary credentials for WinRM (usually Administrator level creds) $creds = Get-Credential $serverName = "FirstServer" $secondServerName = "SecondServer" $thirdServerName = "ThirdServer" Write-Host "Running command from $serverName" Invoke-Command -ComputerName $serverName -Credential $creds -ScriptBlock { param($secondServerName,$thirdServerName,$creds) Write-Host "Running command from $secondServerName" Invoke-Command -ComputerName $secondServerName -Credential $creds -ScriptBlock { param($thirdServerName,$creds) Write-Host "Running command from $thirdServerName" Invoke-Command -ComputerName $thirdServerName -Credential $creds -ScriptBlock { Set-NetFirewallRule -Name WINRM-HTTP-In-TCP-PUBLIC -Action "Allow" -Direction "Inbound" -RemoteAddress "Any" } } -ArgumentList $thirdServerName,$creds } -ArgumentList $secondServerName,$thirdServerName,$creds
Note: The username for the credentials, needs to include the domain or server prefix. If this is a local account, use the ‘local\’ prefix. Therefore a local ‘Administrator’ account should be entered as ‘local\Administrator’.
Enjoy!