Using Quser when a user session is disconnected RRS feed

  • Question

  • There is this awesome dr scripto article "Automating Quser through PowerShell", that goes into how we can use quser to find logged in users on servers and then log them off if we so chose. It's great when they have an active login that has a named session. However, it doesn't work if instead the session is disconnected (Disc), because it doesn't have a sessionname. And when it doesn't have a session name, everything gets moved up. For example here is my code:

    $connectionName = "MyServerHere"
    $quserResult = quser /server:$connectionName 2>&1 
    $quserRegex = $quserResult | ForEach-Object -Process { $_ -replace '\s{2,}',',' }
    $quserObject = $quserRegex | ConvertFrom-Csv

    If the server has an active login, my results are like this:

    USERNAME    : myEmpId
    SESSIONNAME : coolsessionname
    ID          : 2
    STATE       : Disc
    IDLE TIME   : 54
    LOGON TIME  : 11/3/2020 9:39 PM

    But if instead that is disconnected, the results are like this:

    USERNAME    : myEmpId
    ID          : Disc
    STATE       : 54
    IDLE TIME   : 11/3/2020 9:39 PM
    LOGON TIME  : 

    This causes a problem when I try to do this:

    logoff $quserObject.ID /server:$connectionName

    I don't actually even need the sessionname, but I can't figure out a way to remove it from returning in the initial quser, which would be the easiest method. Anyone have any ideas?

    Wednesday, November 4, 2020 4:09 AM

All replies

  • Don't use the session ID use the sessionname and it will always wioork.

    Also this forum is not here to support scripts found on the Internet.  Post questions to the author of the script please.

    This forum will be shutting down permanently tomorrow.


    Wednesday, November 4, 2020 4:28 AM
  • Thanks,

    I will say, the script isn't a random one on the internet, it's a dr scripto. And fundamentally, my question is about the quser command and how to work with it if the sessionID is empty.

    My account just isn't confirmed so it wouldn't let me link it, maybe it will now. In that article it does point to this forum:


    Wednesday, November 4, 2020 4:47 AM
  • https://blog.topqore.com/find-disconnected-user-on-servers-using-powershell-script-scom-monitor/


    Wednesday, November 4, 2020 4:47 AM
  • As I noted - the sessiionname is what you need to use.


    Wednesday, November 4, 2020 5:01 AM