Quser data to Custom PSObject? RRS feed

  • Question

  • I am attempting to write a script that import a list of servers and for each server it will query user (I have it set up to one user). We seem to have a problem with our users not logging out properly. This is what i have so far.. I do know that I will also need to write some kind of throw and catch statement (Maybe) that basically states - if the user doesn't have an IDLE session, then skip / don't add to list.. not sure how I would do that one either. Here is the code so far.. any advice?

    #Creation of a new powershell object 
    $Quser = @()
    # Variables 
    $username = read-host "Enter Username that you will query against servers:  "
    $serverlist = get-content "C:\temp\sessiontest.txt"
    $query = query user $username /server:$server
    foreach ($object in $serverlist) {
    # Calls the "Quser Command / Variable #
    $Qobject = New-Object PSObject
                $Qobject|Add-Member -MemberType NoteProperty -Name "Username" -Value $username 
                $Qobject|Add-Member -MemberType NoteProperty -Name "State" -Value $query.state
                $Qobject|Add-Member -MemberType NoteProperty -Name "Server" -Value $serverlist
                $Qobject|Add-Member -MemberType NoteProperty -Name "IDLE_TIME" -Value $query.'IDLE TIME'
                $Qobject|Add-Member -MemberType NoteProperty -Name "LOGON_TIME" -Value $query.'LOGON TIME'
                $Quser = $Qobject | Format-Table -AutoSize 

    The results look like this so far:


    Thursday, October 12, 2017 4:25 PM


  • $query is an array of strings not an object with properties that you are seeing, so you'll need to extract the information from the string.

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    • Marked as answer by Dremenyi Thursday, October 12, 2017 7:23 PM
    Thursday, October 12, 2017 4:48 PM