/
Close-ActiveSessions.ps1
69 lines (67 loc) · 2.42 KB
/
Close-ActiveSessions.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
<#
.SYNOPSIS
Closes the specified open sessions of a remote or local workstations
.DESCRIPTION
Close-ActiveSessions uses the command line tool rwinsta to close the specified open sessions on a computer whether they are RDP or logged on locally.
.PARAMETER $Name
The name of the computer that you would like to close the active sessions on.
.PARAMETER ID
The ID number of the session to be closed.
.EXAMPLE
Close-ActiveSessions -ComputerName PC1 -ID 1
Closes the session with ID of 1 on PC1
.EXAMPLE
Get-ActiveSessions DC01 | ?{$_.State -ne "Active"} | Close-ActiveSessions
Closes all sessions that are not active on DC01
.INPUTS
[string]$Name
[int]ID
.OUTPUTS
Progress of the rwinsta command.
.NOTES
Author: Anthony Howell
.LINK
rwinsta
http://stackoverflow.com/questions/22155943/qwinsta-error-5-access-is-denied
https://theposhwolf.com
#>
Function Close-ActiveSessions {
Param(
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
Position = 1
)]
[string]$ComputerName,
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
Position = 2
)]
[int]$ID
)
Begin {}
Process {
If (!(Test-Connection $ComputerName -Quiet -Count 1)) {
Write-Error -Message "Unable to contact $ComputerName. Please verify its network connectivity and try again." -Category ObjectNotFound -TargetObject $ComputerName
Return
}
If([bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544")){ #check if user is admin, otherwise no registry work can be done
#the following registry key is necessary to avoid the error 5 access is denied error
$LMtype = [Microsoft.Win32.RegistryHive]::LocalMachine
$LMkey = "SYSTEM\CurrentControlSet\Control\Terminal Server"
$LMRegKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($LMtype,$Name)
$regKey = $LMRegKey.OpenSubKey($LMkey,$true)
If($regKey.GetValue("AllowRemoteRPC") -ne 1){
$regKey.SetValue("AllowRemoteRPC",1)
Start-Sleep -Seconds 1
}
$regKey.Dispose()
$LMRegKey.Dispose()
}
rwinsta.exe /server:$ComputerName $ID /V
}
End {}
}