Public/Get-Quser.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
83
84
85
86
87
88
89
90
91
92
93
94
<#
    .Synopsis

        Invoke the `quser.exe` and return a PSObject.

    .Parameter Server

        The server to be queried. Default is current.

    .Parameter IdleStartTime

        By default, IdleTime is returned as a [timespan] making it consistent with the way quser.exe give the idle time.

        Setting this switch will return IdleTime as [datetime] of when idleness started.

    .Parameter AdComputer

        The AD computer object of the server to be queried.

    .Parameter Property

        When working with an AD computer object, you can specify which AD property to target as the Server.

        Default is "Name", but you might want to change it to something like "DNSHostName".

    .Example

        Get-Quser

    .Example

        Get-Quser -Server 'ThisServer'

    .Example

        Get-Quser -Server 'ThisServer', 'ThatServer'

    .Example

        Get-ADComputer 'ThisServer' | Get-Quser
#>

function Get-Quser {
    [CmdletBinding(DefaultParameterSetName = 'Server')]
    [OutputType([PSObject])]
    [Alias('Get-LoggedOnUsers')]
    Param(
        [Parameter(
            ParameterSetName = 'Server',
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true
        )]
        [ValidateNotNullOrEmpty()]
        [Alias('__ServerName', 'ServerName', 'Computer', 'Name')]
        [string[]]
        $Server = 'localhost',

        [Parameter(ParameterSetName = 'Server')]
        [Parameter(ParameterSetName = 'AdComputer')]
        [switch]
        $IdleStartTime,

        [Parameter(
            ParameterSetName = 'AdComputer',
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true
        )]
        [ValidateNotNullOrEmpty()]
        [PSObject]
        $AdComputer,

        [Parameter(ParameterSetName = 'AdComputer')]
        [ValidateNotNullOrEmpty()]
        [string]
        $Property = 'Name'
    )

    begin {
        Write-Debug "[QuserObject Get-Quser] Begin Bound Parameters: $($MyInvocation.BoundParameters | ConvertTo-Json)"
        Write-Debug "[QuserObject Get-Quser] Begin Unbound Parameters: $($MyInvocation.UnboundParameters | ConvertTo-Json)"

        [boolean] $script:IdleStartTime = $IdleStartTime.IsPresent
    }

    process {
        Write-Debug "[QuserObject Get-Quser] Process Bound Parameters: $($MyInvocation.BoundParameters | ConvertTo-Json)"
        Write-Debug "[QuserObject Get-Quser] Process Unbound Parameters: $($MyInvocation.UnboundParameters | ConvertTo-Json)"

        if ($AdComputer) {
            Write-Output ($AdComputer.$Property | Invoke-Quser | ConvertTo-QuserObject)
        } else {
            Write-Output ($Server | Invoke-Quser | ConvertTo-QuserObject)
        }
    }
}