PowerShell PSSessions

In this tutorial we will see about Windows PowerShell Sessions – PSSessions. We will see what are those PSSession, and how and when we need to use them. It is important to know why you need to use them. After you go through this tutorial it would be good to work with them, in order to get more familiar. After you practise you will see that it can help you a lot while working on remote computers / servers.

What is a PSSession

A session is the environment that Windows PowerShell runs. Every time that we run Windows PowerShell, a session is created. In this session we are able to run commands and functions. Also we are able to add modules, snap-ins, aliases, variables and custom items. This session that is created for us, we are not able to managed it. The items that will be created in a session a exist only in this session and they are deleted when we exit the session and the session ends.

We are able to create our own sessions, which are user-managed. These sessions are called Windows PowerShell Sessions or PSSessions. We are able to create these sessions either on local or remote computer. In a PSSession, we are able to run commands and create items, as in the default session. But unlikely the default sessions, we are able to manage the session. We are allowed to get the active PSSessions, configure them according to our needs, remove them, disconnect and reconnect to them. When we create a PSSession, it always remain available until we delete it, or it will time out. PSSessions are mostly used to run multiple commands on a remote computer. The connection that is established, when we create a PSSession with a remote computer, is persistent.

There are also temporary sessions. Those sessions we are not able to control them. temporary sessions are created to run only a single command and the sessions is closed after the command is completed. When we use Enter-PSSession and Invoke-Command along with the ComputerName parameter, Windows PowerShell will create a temporary session on the remote computer, which might be interactive as well. The session will close as soon as the command that we run is completed interactive session finishes. Any items created within the temporary session, will be lost as soon as the sessions ends.

The current session, in Windows PowerShell, is always the session that we are working on it. This might be the default session, a PSSession or a temporary session.

[adinserter name=”In Article”]

PSSessions Requirements

In order to be able to create a session to a remote computer, you must configure the remote computer for remoting in Windows PowerShell. On the remote computer, you must be a member of Administrators group or be able to provide the credentials of an administrator.

Create PSSessions

As we mentioned above, PSSessions are used when we need to run multiple commands. In order to create a PSSession, we need to use New-PSSession cmdlet. The format of the cmdlet is below:

Code:

New-PSSession -ComputerName RemoteComputer1

The above command will create a persistent session with “RemoteComputer1” in order to run multiple commands on that computer. As soon as the PSSession is created, in the output we will get the PSSession object that has been created.

We are able to save this object in a variable in order to use it at a later stage to reconnect on “RemoteComputer1”. In order to save the PSSession object in a variable we need to use the below:

Code:

$mysession = New-PSSession -ComputerName RemoteComputer1

We are also able to create sessions on multiple remote computers at once using the same command. In ComputerName parameter, we need to provide multiple computer names separated by comma ( , ).

Code:

New-PSSession -ComputerName RemoteComputer1,RemoteComputer2,RemoteServer1,RemoteServer2

[adinserter name=”In Article”]

Get PSSessions

In order to see the PSSessions that we have created in our sessions, we need to use Get-PSSession cmdlet. By running this cmdlet alone, it provide us with the same object that we have received when we created the PSSessions.

If you like to refer to a specific session, you can refer to them by using:

PSSession ID

PSSession Name

Instance ID (a GUID)

You are also able to save the sessions, that are retrieve by Get-PSSession , in variables. See the screenshot below:

The above methods can provide you with the sessions that you have created in the current session. As from Windows PowerShell 3.0, PSSessions are maintained on the remote computer. They are completely independent of the session that they were created from. By using ComputerName parameter, in order to get PSSessions on a remote computer, it will provide you also the PSSessions from the local computer or any other computer, to the remote computer even if they were created from another session. If you are using Windows PowerShell 2.0, you will get only PSSessions that were created from your local computer in the current session.

As you can see above, when I used Get-PSSession without ComputerName parameter, I got two PSSessions, for the two remote servers that I have created one, in my current session. Note here that I had already another Windows PowerShell running on another window and created a session to the first server. When I run Get-PSSession command including ComputerName parameter to get only the sessions related to the first server only, I got back that there are two sessions for the particular server. It provided my also the session to that server that I had created using another session.

[adinserter name=”In Article”]

Delete PSSessions

As PSSessions create persistent connections, in order to release the resources, after the you finish you need remote the session. PSSession resources are released when the PSSession is removed or timed out. You are able to use use Remove-PSSession to delete PSSessions. Remove-PSSession allows you to specify which of the sessions to close. Similar to Get-PSSession you are able to specify the sessions by using the below methods:

PSSession ID

PSSession Name

Instance ID (a GUID)

By using the Session Parameter you are also to specify the PSSession object that you want to remove. Check the below screenshot to see the different ways.

You are also able to set a timeout limit, in order for your PSSession will be removed when it will time out. In order to specify the time out time along with other advance options for you PSSession you need to use New-PSSessionOption cmdlet. New-PSSessionOption creates an object with different options that you can specify when you are creating you session to a remote computer. You need to specify the option when you will use New-PSSession , Enter-PSSession and Invoke-Command . The below example will force our session to time out after 5 minutes of idle time.

Code:

$MySessionOption = New-PSSessionOption -IdleTimeout 300000 $MySessionOption New-PSSession -ComputerName RemoteServer1 -SessionOption $MySessionOption

As you can see above, I set the Idle time out limit to 5 minutes (300 000 miliseconds) and then used this option when I created my PSSession with the remote server. This session will close after the idle time out limit has been reached.

[adinserter name=”In Article”]

PSSession related cmdlets

Below is the list of PSSession related cmdlets:

Connect-PSSession – Connects a PSSession to the current session

– Connects a PSSession to the current session Disconnect-PSSession – Disconnects a PSSession from the current session

– Disconnects a PSSession from the current session Enter-PSSession – Starts an interactive session

– Starts an interactive session Exit-PSSession – Ends an interactive session

– Ends an interactive session Get-PSSession – Gets the PSSessions in the current session

– Gets the PSSessions in the current session New-PSSession – Creates a new PSSession on a local or remote computer

– Creates a new PSSession on a local or remote computer Receive-PSSession – Gets the results of commands that ran in a disconnected session

– Gets the results of commands that ran in a disconnected session Remove-PSSession – Deletes the PSSessions in the current session

You are able to get the list of related commands by using the below 2 methods:

Code:

Get-Help *-PSSession Get-Command -Noun PSSession

[adinserter name=”In Article”]

Running Commands in a PSSession

In order to run commands in a sessions you need to use Invoke-Command cmdlet and specify the session and the commands to run.

Code:

$MySession = New-PSSession -ComputerName bsm-rap-ad Invoke-Command -Session $MySession -ScriptBlock { Get-ChildItem -Path "C:\TestFolder1\" Write-Host "========================================" Rename-Item -Path C:\TestFolder1\test2.txt -NewName FileRenamed.txt Get-ChildItem -Path "C:\TestFolder1\" Write-Host "========================================" }

or

$MySession = New-PSSession -ComputerName bsm-rap-ad $MyCommands = { Get-ChildItem "C:\TestFolder1\" Write-Host "========================================" Rename-Item -Path C:\TestFolder1\test2.txt -NewName FileRenamed.txt Get-ChildItem "C:\TestFolder1\" Write-Host "========================================" } Invoke-Command -Session $MySession -ScriptBlock $MyCommands

Output:

Note the difference in ScriptBlock parameter. When I am saving my commands in a variable, I am not using curly brackets.

I hope the tutorial about Windows PowerShell Sessions – PSSessions is helpful.

Please let me know your comments and thoughts.

You feedback is appreciated.

[adinserter name=”In Article”]

Related Links:

[adinserter name=”Matched-Content”]