PowerShell Function to Determine PSSessions to your Servers

This past week, I needed to determine if anyone had a PSSession connected to any of the servers that I support. This is fairly easy to accomplish with a PowerShell one-liner, but I can never remember the syntax so I decided to create a reusable function to accomplish the task.

 1#Requires -Version 3.0
 2function Get-MrRemotePSSession {
 3
 4<#
 5.SYNOPSIS
 6    Retrieves a list of the Windows PowerShell sessions that are connected to the specified remote computer(s).
 7
 8.DESCRIPTION
 9    The Get-MrRemotePSSession function gets the user-managed Windows PowerShell sessions (PSSessions) on remote
10    computers even if they were not created in the current session.
11
12.PARAMETER ComputerName
13    Specifies an array of names of computers. Gets the sessions that connect to the specified computers.
14    Wildcard characters are not permitted. The default value is the local computer.
15
16.PARAMETER Credential
17    Specifies a user credential. This function runs the command with the permissions of the specified user.
18    Specify a user account that has permission to connect to the remote computer. The default is the current
19    user. Type a user name, such as `User01`, `Domain01\User01`, or `User@Domain.com`, or enter a PSCredential
20    object, such as one returned by the Get-Credential cmdlet. When you type a user name, this cmdlet prompts
21    you for a password.
22
23.EXAMPLE
24     Get-MrRemotePSSession -ComputerName Server01, Server02 -Credential (Get-Credential)
25
26.EXAMPLE
27     'Server01', 'Server02' | Get-MrRemotePSSession -Credential (Get-Credential)
28
29.INPUTS
30    String
31
32.OUTPUTS
33    PSCustomObject
34
35.NOTES
36    Author:  Mike F Robbins
37    Website: http://mikefrobbins.com
38    Twitter: @mikefrobbins
39#>
40
41    [CmdletBinding()]
42    param (
43        [Parameter(ValueFromPipeline)]
44        [string[]]$ComputerName = $env:COMPUTERNAME ,
45
46        [System.Management.Automation.Credential()]$Credential = [System.Management.Automation.PSCredential]::Empty
47    )
48
49    BEGIN {
50        $Params = @{
51            ResourceURI = 'shell'
52            Enumerate = $true
53        }
54
55        if ($PSBoundParameters.Credential) {
56            $Params.Credential = $Credential
57        }
58    }
59
60    PROCESS {
61        foreach ($Computer in $ComputerName) {
62            $Params.ConnectionURI = "http://$($Computer):5985/wsman"
63
64            Get-WSManInstance @Params |
65            Select-Object -Property @{label='PSComputerName';expression={$Computer}}, Name, Owner, ClientIP, State
66        }
67    }
68
69}

The function accepts multiple computer names via parameter input:

1Get-MrRemotePSSession -ComputerName dc01, sql02 | Format-Table

remote-pssession1a.png

It also accepts the computer names via pipeline input and it has a Credential parameter so that alternate credentials can be specified:

1'dc01', 'sql02' | Get-MrRemotePSSession -Credential (Get-Credential) | Format-Table

remote-pssession2a.png

The Get-MrRemotePSSession function shown in this blog article can be downloaded from my PowerShell repository on GitHub.

µ