Private/Get-QuserIdleTime.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
<#
    .Synopsis

        This converts quser idletime to something useful.

    .Parameter QuserIdleTime

        The idle time, as returned by Quser.

    .Parameter AsDateTime

        Return as [datetime] instead of [timespan].
#>

function Get-QuserIdleTime {
    [CmdletBinding()]
    [OutputType([timespan])]
    [OutputType([datetime])]
    [OutputType([void])]
    Param(
        [Parameter(Mandatory = $true)]
        [string]
        $QuserIdleTime,

        [Parameter()]
        [switch]
        $AsDateTime
    )

    $QuserIdleTime = $QuserIdleTime.Replace('+', '.')

    if ($QuserIdleTime -as [int]) {
        $QuserIdleTime = "0:${QuserIdleTime}"
    }

    if ($QuserIdleTime -as [timespan]) {
        [timespan] $idleTime = $QuserIdleTime

        if ($AsDateTime.IsPresent) {
            $now = Get-Date
            return $now.Subtract($idleTime)
        } else {
            return $idleTime
        }
    } else {
        return $null
    }
}