-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogMeOff.ps1
82 lines (69 loc) · 1.88 KB
/
LogMeOff.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
75
76
77
78
79
80
81
82
Function Get-WMIComputerSessions {
<#
.SYNOPSIS
Retrieves tall user sessions from local or remote server/s
.DESCRIPTION
Retrieves tall user sessions from local or remote server/s
.PARAMETER computer
Name of computer/s to run session query against.
.NOTES
Name: Get-WmiComputerSessions
Author: Boe Prox
DateCreated: 01Nov2010
.LINK
https://boeprox.wordpress.org
.EXAMPLE
Get-WmiComputerSessions -computer "server1"
Description
-----------
This command will query all current user sessions on 'server1'.
#>
[cmdletbinding(
DefaultParameterSetName = 'session',
ConfirmImpact = 'low'
)]
Param(
[Parameter(
Mandatory = $True,
Position = 0,
ValueFromPipeline = $True)]
[string[]]$computer
)
Begin {
#Create empty report
$report = @()
}
Process {
#Iterate through collection of computers
ForEach ($c in $computer) {
#Get explorer.exe processes
$proc = gwmi win32_process -computer $c -Filter "Name = 'explorer.exe'"
#Go through collection of processes
ForEach ($p in $proc) {
$temp = "" | Select Computer, Domain, User
$temp.computer = $c
$temp.user = ($p.GetOwner()).User
$temp.domain = ($p.GetOwner()).Domain
$report += $temp
}
}
}
End {
$report
}
}
$output = Get-WMIComputerSessions localhost
$output
foreach ($user in $output){
#Write-host "The user is: "$user
if ($user -like '*mitchell.grimes*') { Write-Host $user;$logoff = 'true' }else { $logoff = 'false' }
}
$server = 'localhost'
$username = 'mitchell.grimes'
if ($logoff -eq 'true'){
$session = ((quser /server:$server | ? { $_ -match $username }) -split ' +')[2]
logoff $session /server:$server
}
Write-Host "After Logoff command. Remaining sessions."
$output = Get-WMIComputerSessions localhost
$output