Quser.Crescendo.psm1

# Module created by Microsoft.PowerShell.Crescendo
# Version: 1.1.0
# Schema: https://aka.ms/PowerShell/Crescendo/Schemas/2021-11
# Generated at: 03/18/2023 22:50:09
class PowerShellCustomFunctionAttribute : System.Attribute { 
    [bool]$RequiresElevation
    [string]$Source
    PowerShellCustomFunctionAttribute() { $this.RequiresElevation = $false; $this.Source = "Microsoft.PowerShell.Crescendo" }
    PowerShellCustomFunctionAttribute([bool]$rElevation) {
        $this.RequiresElevation = $rElevation
        $this.Source = "Microsoft.PowerShell.Crescendo"
    }
}

# Queue for holding errors
$__CrescendoNativeErrorQueue = [System.Collections.Queue]::new()
# Returns available errors
# Assumes that we are being called from within a script cmdlet when EmitAsError is used.
function Pop-CrescendoNativeError {
    param ([switch]$EmitAsError)
        while ($__CrescendoNativeErrorQueue.Count -gt 0) {
            if ($EmitAsError) {
                $msg    = $__CrescendoNativeErrorQueue.Dequeue()
                $errmsg = $Errmsg + [Environment]::NewLine + "$msg,"
                if($__CrescendoNativeErrorQueue.Count -eq 0)
                {
                    $er = [System.Management.Automation.ErrorRecord]::new([system.invalidoperationexception]::new($errmsg), "$PSCmdlet.Name", "InvalidOperation", $errmsg)
                    $PSCmdlet.WriteError($er)
                }
            }
            else {
                $__CrescendoNativeErrorQueue.Dequeue()
            }
        }
    }
# this is purposefully a filter rather than a function for streaming errors
filter Push-CrescendoNativeError {
    if ($_ -is [System.Management.Automation.ErrorRecord]) {
        $__CrescendoNativeErrorQueue.Enqueue($_)
    }
    else {
        $_
    }
}

function Get-LoggedOnuser
{
[PowerShellCustomFunctionAttribute(RequiresElevation=$False)]
[CmdletBinding(DefaultParameterSetName='Default')]

param(
[Parameter(Position=1,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,ParameterSetName='Default')]
[string]$ComputerName,
[Parameter(Position=0,ValueFromPipelineByPropertyName=$true,ParameterSetName='Default')]
[string]$Identity
    )

BEGIN {
    $__PARAMETERMAP = @{
         ComputerName = @{
               OriginalName = '/SERVER:'
               OriginalPosition = '0'
               Position = '1'
               ParameterType = 'string'
               ApplyToExecutable = $False
               NoGap = $True
               ArgumentTransform = '$args'
               ArgumentTransformType = 'inline'
               }
         Identity = @{
               OriginalName = ''
               OriginalPosition = '0'
               Position = '0'
               ParameterType = 'string'
               ApplyToExecutable = $False
               NoGap = $False
               ArgumentTransform = '$args'
               ArgumentTransformType = 'inline'
               }
    }

    $__outputHandlers = @{
        Default = @{ StreamOutput = $False; Handler = { [CmdletBinding()]
                    param([object[]]$Lines,$Skip = 1)
                    process 
                    {
                        $Lines | Select-Object -Skip $skip | ForEach-Object {
                            $columns = $_ -split '\s{2,}' | Where-Object {$_ }
                            if($columns.count -eq 5)
                            {
                                [pscustomobject]@{
                                    UserName    = [String]($columns[0] -replace '>').trim()
                                    SessionName = $null
                                    ID          = [String]$columns[1]
                                    State       = [String]$columns[2]
                                    IdleTime    = [String]$columns[3]
                                    LogonTime   = Get-Date $columns[4]
                                }
                            }else{
                                [pscustomobject]@{
                                    UserName    = [String]($columns[0] -replace '>').Trim()
                                    SessionName = [String]$columns[1]
                                    ID          = [String]$columns[2]
                                    State       = [String]$columns[3]
                                    IdleTime    = [String]$columns[4]
                                    LogonTime   = Get-Date $columns[5]
                                }
                            }
                        }
                    }
                    end
                    {
                        Pop-CrescendoNativeError -EmitAsError
                    } } }
    }
}

PROCESS {
    $__boundParameters = $PSBoundParameters
    $__defaultValueParameters = $PSCmdlet.MyInvocation.MyCommand.Parameters.Values.Where({$_.Attributes.Where({$_.TypeId.Name -eq "PSDefaultValueAttribute"})}).Name
    $__defaultValueParameters.Where({ !$__boundParameters["$_"] }).ForEach({$__boundParameters["$_"] = get-variable -value $_})
    $__commandArgs = @()
    $MyInvocation.MyCommand.Parameters.Values.Where({$_.SwitchParameter -and $_.Name -notmatch "Debug|Whatif|Confirm|Verbose" -and ! $__boundParameters[$_.Name]}).ForEach({$__boundParameters[$_.Name] = [switch]::new($false)})
    if ($__boundParameters["Debug"]){wait-debugger}
    foreach ($paramName in $__boundParameters.Keys|
            Where-Object {!$__PARAMETERMAP[$_].ApplyToExecutable}|
            Sort-Object {$__PARAMETERMAP[$_].OriginalPosition}) {
        $value = $__boundParameters[$paramName]
        $param = $__PARAMETERMAP[$paramName]
        if ($param) {
            if ($value -is [switch]) {
                 if ($value.IsPresent) {
                     if ($param.OriginalName) { $__commandArgs += $param.OriginalName }
                 }
                 elseif ($param.DefaultMissingValue) { $__commandArgs += $param.DefaultMissingValue }
            }
            elseif ( $param.NoGap ) {
                $pFmt = "{0}{1}"
                if($value -match "\s") { $pFmt = "{0}""{1}""" }
                $__commandArgs += $pFmt -f $param.OriginalName, $value
            }
            else {
                if($param.OriginalName) { $__commandArgs += $param.OriginalName }
                if($param.ArgumentTransformType -eq 'inline') {
                   $transform = [scriptblock]::Create($param.ArgumentTransform)
                }
                else {
                   $transform = $param.ArgumentTransform
                }
                $__commandArgs += & $transform $value
            }
        }
    }
    $__commandArgs = $__commandArgs | Where-Object {$_ -ne $null}
    if ($__boundParameters["Debug"]){wait-debugger}
    if ( $__boundParameters["Verbose"]) {
         Write-Verbose -Verbose -Message "$env:windir/System32/quser.exe"
         $__commandArgs | Write-Verbose -Verbose
    }
    $__handlerInfo = $__outputHandlers[$PSCmdlet.ParameterSetName]
    if (! $__handlerInfo ) {
        $__handlerInfo = $__outputHandlers["Default"] # Guaranteed to be present
    }
    $__handler = $__handlerInfo.Handler
    if ( $PSCmdlet.ShouldProcess("$env:windir/System32/quser.exe $__commandArgs")) {
    # check for the application and throw if it cannot be found
        if ( -not (Get-Command -ErrorAction Ignore "$env:windir/System32/quser.exe")) {
          throw "Cannot find executable '$env:windir/System32/quser.exe'"
        }
        if ( $__handlerInfo.StreamOutput ) {
            if ( $null -eq $__handler ) {
                & "$env:windir/System32/quser.exe" $__commandArgs
            }
            else {
                & "$env:windir/System32/quser.exe" $__commandArgs 2>&1| Push-CrescendoNativeError | & $__handler
            }
        }
        else {
            $result = & "$env:windir/System32/quser.exe" $__commandArgs 2>&1| Push-CrescendoNativeError
            & $__handler $result
        }
    }
  } # end PROCESS

<#
.SYNOPSIS
Could not find $env:windir/System32/quser.exe to generate help.
 
.DESCRIPTION See help for $env:windir/System32/quser.exe
 
.PARAMETER ComputerName
Name of local or remote computer
 
 
.PARAMETER Identity
This value can be username, sessionname or sessionID.
 
 
 
#>

}