| 437-991-3573 | Data Engineering Services
Resources Tools
Experts in Microsoft SQL Server on Windows, Linux, Containers | Clusters, Always On, FCI | Migrations, Cloud, Performance

Powershell - Interactive Sessions, Legacy Remoting, Powershell Remoting(WSMan, WinRM Service)
by BF (Principal Consultant; Architecture; Engineering)

Interactive Sessions:

A method to execute remote commands is to initiate a Interactive Session with a remote server.

When a session starts the commands that are typed execute on the remote server as if one typed them
directly on the remote server.

To initiate an Interactive Session use the Enter-PSSession cmdlet.

Enter-PSSession Server1
- starts an interactive session with the Server1

The command prompt then changes to indicate you are connected to Server1:


At this point you can type commands on the Server1.

To close the Interactive Session use the Exit-PSSession cmdlet.

NOTE: You can connect to only one computer in each interactive session.

Legacy Remoting:

Typically, cmdlets that support remoting, without special configuration, have a -computerName parameter and do not have a
Session parameter.

To locate those cmdlets:

Get-Command | where { $_.Parameters.Keys -contains "ComputerName" -and $_.Parameters.Keys -NotContains "Session"}

For example, the following command gets the services on Server1:

Ex. Get-Service -ComputerName Server1

Powershell Remoting:

Use the Invoke-Command cmdlet. Use the -computername parameter of Invoke-Command to specify the remote Servers. Use the ScriptBlock
parameter to specify the command.

For example, the following command runs a Get-Service command on the Server1 Server:

invoke-command -ScriptBlock {get-service MSSQLSERVER} -computername $Computers

When you use the -computerName parameter of the Invoke-Command cmdlet, Windows PowerShell establishes a connection just for the
command. Then, it closes the connection when the command is complete. Any variables or functions that are defined in the command
are lost.

To establish a persistent connection to a remote computer use the Session parameter. To create a persistent connection to a Remote
Server, use the New-PSSession cmdlet. For example, the following command creates PSSessions on the Server1 and Server2 Server and
then saves the PSSessions in the $s variable.

$s = New-PSSession -computername Server1, Server2

With a PSSession, you can run a series of remote commands that share data, like functions, aliases, and the values of variables.
To run commands in a PSSession, use the Session parameter of the Invoke-Command cmdlet.

For example, the following command uses the Invoke-Command cmdlet to run a Get-Process command in the PSSessions on the Server1
and Server2 computers. The command saves the processes in a $p variable in each PSSession.

Invoke-Command -Session $s -ScriptBlock {$p = Get-Process}

Because the PSSession uses a persistent connection, you can run another command in the same PSSession that uses the $p variable.
The following command counts the number of processes saved in $p.

Invoke-Command -Session $s -ScriptBlock {$p.count}


From Powershell:

help about_remote -ShowWindow


Just a couple points on Legacy Remoting vs Powershell Remoting.

Legacy Remoting is typically processing done locally.
Powershell Remoting is typically processing done remotely.

Legacy Remoting uses cmdlets with -computername parameter and no -session parameter.
Powershell Remoting uses cmdlets with -session parameter.

Legacy Remoting would typically execute commands on multiple servers in sequential processing.
Powershell Remoting would run commands on multiple server in parallel. This is a major advantage because overall processing time
will be shorter.

Legacy Remoting uses legacy protocols like DCOM or RPC.
Powershell Remoting uses modern protocols like WSMAn. WSMan is an Industry Standard Networking protocol that is managed
by the WinRM Service(introduced in Windows Management Framework 3.0). It connects remotely via Port: 5985. It used Kerberos
Authentication and it's used Encrypted Communication. It can be support SSL over Port: 5986.

*Powershell Remoting:
Powershell Remoting is enabled, by default, in Windows Server 2012 and later.
To enable: Use Enable-PSRemoting cmdlet - you need to be on that Server in order to execute this command.
If you have many Servers that you need to enable Powershell Remoting, the best method is to use Group Policy & a StartUp Script.
To Test: Use Test-WSMan - to verify connectivity via the WSMan protocol.
To Disable: Use Disable-PSRemoting.