-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathWhosLoggedOn.ps1
More file actions
79 lines (69 loc) · 2.6 KB
/
Copy pathWhosLoggedOn.ps1
File metadata and controls
79 lines (69 loc) · 2.6 KB
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
#*******************************************************************************
# Author: Mick Pletcher
# Date: 23 August 2013
#
# Program: Who's Logged On and Logged Off?
#*******************************************************************************
#Declare Global Variables
Set-Variable -Name Command -Scope Global -Force
Set-Variable -Name Computer -Scope Global -Force
Set-Variable -Name Computers -Scope Global -Force
Set-Variable -Name i -Scope Global -Value 1 -Force
Set-Variable -Name Output -Scope Global -Force
Set-Variable -Name RelativePath -Scope Global -Force
Set-Variable -Name Results -Scope Global -Force
Set-Variable -Name Username -Scope Global -Force
Function RenameWindow ($Title) {
#Declare Local Memory
Set-Variable -Name a -Scope Local -Force
$a = (Get-Host).UI.RawUI
$a.WindowTitle = $Title
#Cleanup Local Memory
Remove-Variable -Name a -Scope Local -Force
}
Function GetRelativePath{
$Global:RelativePath=(split-path $SCRIPT:MyInvocation.MyCommand.Path -parent)+"\"
}
cls
RenameWindow "Who's Logged On and Logged Off?"
GetRelativePath
$Results = @()
$Username = @()
$Computers = Get-Content -Path $Global:RelativePath"Computers.txt"
$Size = $Computers.Length
Foreach ($Computer in $Computers) {
Write-Host "Scanning System "$i" of "$Size
Write-Host
Write-Host "System:"$Computer
$Command = $Global:RelativePath+"PsLoggedon.exe -x -l \\$Computer"
$Output = Invoke-Expression $Command
If ($Output.SyncRoot[2] -eq "Users logged on locally:") {
$Username += $Output.SyncRoot[3].Trim()
} else {
$Username += "N/A"
}
$Output = $Output.SyncRoot[2]
$Output = $Output.Substring(0,$Output.Length-1)
$Results += $Output
$i = $i + 1
cls
}
cls
for ($i=0 ; $i -lt $Results.length ; $i++) {
If ($Results[$i] -eq "No one is logged on locally") {
Write-Host $Computers[$i]": "$Results[$i] -BackgroundColor Yellow -ForegroundColor Black
} elseIf ($Results[$i] -eq "Users logged on locally") {
Write-Host $Computers[$i]": "$Results[$i]" -- "$Username[$i]-ForegroundColor White
} else {
Write-Host $Computers[$i]": "$Results[$i]" -- "$Username[$i]-ForegroundColor Red
}
}
#Cleanup Global Variables
Remove-Variable -Name Command -Scope Global -Force
Remove-Variable -Name Computer -Scope Global -Force
Remove-Variable -Name Computers -Scope Global -Force
Remove-Variable -Name i -Scope Global -Force
Remove-Variable -Name Output -Scope Global -Force
Remove-Variable -Name RelativePath -Scope Global -Force
Remove-Variable -Name Results -Scope Global -Force
Remove-Variable -Name Username -Scope Global -Force