-
Notifications
You must be signed in to change notification settings - Fork 1
/
get-remotesession.ps1
74 lines (58 loc) · 2.26 KB
/
get-remotesession.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#requires -version 5.1
#requires -module CimCmdlets
Function Get-PSRemoteSession {
[cmdletbinding()]
[alias("gpsr")]
Param(
[Parameter(
Position = 0,
ValueFromPipeline,
ValueFromPipelineByPropertyName)]
[ValidateNotNullorEmpty()]
[Alias("CN", "Name", "PSComputername")]
[string[]]$Computername = $env:COMPUTERNAME
)
Begin {
Write-Verbose "Starting $($MyInvocation.MyCommand)"
} #begin
Process {
foreach ($computer in $computername) {
Write-Verbose "Querying $computer"
Try {
#use CIM to remotely query the computer
#create a hashtable of parameter values to splat to Get-CimInstance
$paramHash = @{
classname = 'win32_process'
filter = "name='wsmprovhost.exe'"
computername = $computer
ErrorAction = 'Stop'
}
$data = Get-CimInstance @paramHash
} #try
Catch {
Write-Warning "Could not query $computer. $($_.Exception.Message)"
} #catch
if ($data) {
Write-Verbose "Found $(($data | Measure-Object).count) remoting sessions on $computer"
foreach ($item in $data) {
#get process owner
$owner = $item | Invoke-CimMethod -MethodName GetOwner
[pscustomobject]@{
PSTypeName = "PSRemoteConnection"
Computername = $item.PSComputername.toUpper()
ProcessID = $item.ProcessID
Created = $item.CreationDate
Runtime = (Get-Date) - $item.creationdate
Username = "$($owner.domain)\$($owner.user)"
}
} #foreach
#reset the variable for the next computer
Remove-Variable -Name data
} #if $data
} #foreach computer
} #process
End {
Write-verbose "Ending $($MyInvocation.MyCommand)"
} #end
} #end function
Update-FormatData -AppendPath $PSScriptRoot\psremoteconnection.format.ps1xml