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
<#
    .Synopsis

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

    .Parameter ServerName

        The server to be queried. Default is current.

    .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 ServerName.
        
        Default is "Name", but you might want to change it to something like "DNSHostName".

    .Example

        Get-QuserObject

    .Example

        Get-QuserObject -ServerName 'ThisServer'

    .Example

        Get-QuserObject -ServerName 'ThisServer', 'ThatServer'

    .Example

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

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

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

        [Parameter(ParameterSetName = 'ADComputer')]
        [ValidateNotNullOrEmpty()]
        [PSObject]
        $Property = 'Name'
    )

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

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

        if ($ServerName) {
            $ServerName | Invoke-Quser | ConvertTo-QuserObject
        } elseif ($ADComputer) {
            $ADComputer.$Property | Invoke-Quser | ConvertTo-QuserObject
        } else {
            Invoke-Quser | ConvertTo-QuserObject
        }
    }
}