Conditional Table?

old dos command. i have tried try/catch, if/else but nothing is working. it works perfectly if quser returns something

trying to wrap a condition around this:

New-UDTable -Title "Logged In Users" -Headers @("User","Session","ID","State","IdleFor","LogonTime") -Endpoint {
quser /server:$server | ConvertFrom-String | select -Skip 1 | foreach {
[PSCustomObject]@{
User = $_.p2
Session = $_.p3
ID = $_.p4
State = $_.p5
IdleFor = $_.p6
LogonTime = ("$($_.p7) $($_.p8)").replace(" 00:00:00 "," ")
}                         
} | Out-UDTableData -Property @("User","Session","ID","State","IdleFor","LogonTime")
}

to avoid this:

Try this

you can read about redirect output by running get-help about_*redirect*

 New-UDTable -Title "Logged In Users" -Headers @("User", "Session", "ID", "State", "IdleFor", "LogonTime") -Endpoint {
                        $result = quser  /server:$server 2>&1 
                        if($result -is [System.Management.Automation.ErrorRecord] ){
                            # Do something like write to log ..
                        }else{
                            $result |  ConvertFrom-String | select -Skip 1 | foreach {
                                    [PSCustomObject]@{
                                        User      = $_.p2
                                        Session   = $_.p3
                                        ID        = $_.p4
                                        State     = $_.p5
                                        IdleFor   = $_.p6
                                        LogonTime = ("$($_.p7) $($_.p8)").replace(" 00:00:00 ", " ")
                                    }                         
                            } | Out-UDTableData -Property @("User", "Session", "ID", "State", "IdleFor", "LogonTime")
                        }
                }

the result if no users are found
24%20PM

that worked perfectly, thank you!

1 Like