Menu

[ea00bd]: / util-winAdmin / Get-QUserInfo.ps1  Maximize  Restore  History

Download this file

67 lines (61 with data), 2.3 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
#
# Get-QUserInfo - Get Idle and Logon time of users on list of computers
#
# reddit user: Lee Dailey @ https://www.reddit.com/user/Lee_Dailey/
#
function Get-QUserInfo
{
[CmdletBinding()]
Param (
[Parameter ()]
[string[]]
$ComputerName = $env:COMPUTERNAME
)
begin {
$Header = 'UserName','SessionName','ID','State','IdleTime','LogonTime'
$No_Connection = '-- No Connection --'
}
process {
foreach ($CN_Item in $ComputerName) {
if (Test-Connection -ComputerName $CN_Item -Count 1 -Quiet) {
quser /server:$CN_Item |
Select-Object -Skip 1 |
ForEach-Object {($_ -replace '\s{2,}', ',').Trim()} |
ConvertFrom-Csv -Header $Header |
ForEach-Object {
if ($_.IdleTime -eq 'none') {
$IdleTime = $Null
} else {
$IdleTime = [timespan]$_.IdleTime
}
[PSCustomObject]@{
ComputerName = $CN_Item
UserName = $_.UserName
SessionName = $_.SessionName
ID = $_.ID
State = $_.State
IdleTime = $IdleTime
LogonTime = [datetime]$_.LogonTime
}
# Write-Output "object: " $_ | Get-Member | Format-Table
}
} else {
[PSCustomObject]@{
ComputerName = $CN_Item
UserName = $No_Connection
SessionName = $No_Connection
ID = $No_Connection
State = $No_Connection
IdleTime = $No_Connection
LogonTime = $No_Connection
}
}
} # end >> foreach ($CN_Item in $ComputerName)
} # end >> process {}
end {}
} # end >> function Get-QUserInfo
#
# example usage:
#
Get-QUserInfo -ComputerName localhost
Get-QUserInfo -ComputerName notARealHost, localHost
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.