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

        Invoke `quser.exe` on the localhost or the target system.

    .Parameter Server

        A server to target. No validation or reachability testing is done.

    .Parameter UserOrSession

        Optional username, sessionname, or sessionid to pass on to `quser.exe`.
#>

function Invoke-Quser {
    [CmdletBinding()]
    [OutputType([string])]
    Param(
        [Parameter(ValueFromPipeline)]
        [string]
        $Server,

        [string]
        $UserOrSession = ''
    )

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

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

        if ($Server -eq 'localhost') {
            $cmd = '{0}{1}'
        } else {
            $cmd = '{0} {1} /SERVER:{2}'
        }

        try {
            $quserPath = (Get-Command 'quser.exe' -ErrorAction 'Stop').Path
        } catch {
            $quserPath = (Get-Command "$env:SystemRoot\SysNative\quser.exe" -ErrorAction 'Stop').Path
        }

        $quser = $cmd -f $quserPath, $UserOrSession, $Server
        Write-Debug "[QuserObject Invoke-Quser] QUSER Command: ${quser}"

        try {
            $result = (Invoke-Expression $quser) 2>&1
        } catch {
            $result = $Error[0].Exception.Message
        }
        Write-Verbose "[QuserObject Invoke-Quser] QUSER Result (ExitCode: ${LASTEXITCODE}):`n$($result | Out-String)"

        if ($LASTEXITCODE -eq 0) {
            Write-Output @{
                Server = $Server
                Result = $result
            }
        } else {
            $message = if ($result.Exception) { $result.Exception.Message } else { $result }
            Write-Warning " ${Server}: $($message -join ', ')"
        }
    }
}