PowerShell Remoting



Connecting to a Remote Server via PowerShell

Using credentials from your local computer:

Enter-PSSession 192.168.1.1

Prompting for credentials on the remote computer

Enter-PSSession 192.168.1.1 -Credential $(Get-Credential)

Run commands on a Remote Computer

Once Powershell remoting is enabled (Enable-PSRemoting) You can run commands on the remote computer like this:

Invoke-Command -ComputerName "RemoteComputerName" -ScriptBlock { Write-Host "Remote Computer Name: $ENV:ComputerName" }

The above method creates a temporary session and closes it right after the command or scriptblock ends.

To leave the session open and run other command in it later, you need to create a remote session first:

$Session = New-PSSession -ComputerName "RemoteComputerName"

Then you can use this session each time you invoke commands on the remote computer:

Invoke-Command -Session $Session -ScriptBlock { Write-Host "Remote Computer Name: $ENV:ComputerName" } Invoke-Command -Session $Session -ScriptBlock { Get-Date }

If you need to use different Credentials, you can add them with the -Credential Parameter:

$Cred = Get-Credential Invoke-Command -Session $Session -Credential $Cred -ScriptBlock {...}

Enable/Disable-PSRemoting

The Enable-PSRemoting cmdlet configures the computer to receive Windows PowerShell remote commands that are sent by using the WS-Management technology. The Disable-PSRemoting cmdlet prevents users on other computers from running commands on the local computer.

Enable-PSRemoting -Force Disable-PSRemoting -Force

This command does the following:

Enter-PSSession

The Enter-PSSession cmdlet starts an interactive PowerShell session with a single remote computer. During the session, the commands that you type run on the remote computer, just as though you were typing directly on the remote computer. You can have only one interactive session at a time.

Enter-PSSession -Computer RemoteHost

Create a session on the local computer

This command creates a new PSSession on the local computer and saves the PSSession in the $s variable.

$s = New-PSSession

Create a session on a remote computer

This command creates a new PSSession on the Server01 computer and saves it in the $Server01 variable.

$Server01 = New-PSSession -ComputerName Server01 $s1, $s2, $s3 = New-PSSession -ComputerName Server01,Server02,Server03

Best practise for automatically cleaning-up PSSessions

When a remote session is created via the New-PSsession cmdlet, the PSSession persists until the current PowerShell session ends. Meaning that, by default, the PSSession and all associated resources will continue to be used until the current PowerShell session ends.

Multiple active PSSessions can become a strain on resources, particularly for long running or interlinked scripts that create hundreds of PSSessions in a single PowerShell session.

It is best practise to explicitly remove each PSSession after it is finished being used.

The following code template utilises try-catch-finally in order to achieve the above, combining error handling with a secure way to ensure all created PSSessions are removed when they are finished being used:

try { $session = New-PSsession -Computername "RemoteMachineName" Invoke-Command -Session $session -ScriptBlock {write-host "This is running on $ENV:ComputerName"} } catch { Write-Output "ERROR: $_" } finally { if ($session) { Remove-PSSession $session } }