Jump to content

Detect if user logged on


Recommended Posts

Is there a simple method to detect whether a user is logged into a computer? My script is running as the SYSTEM account in non-interactive mode and I want to detect if a user is logged on to the console of WinXP workstations. I did find the Win32_ComputerSystem class in WMI which has a UserName value which appears to show the console user. I see some posts out there saying that the UserName field isn't consistent. Anyone know of other ways?

Link to comment
Share on other sites

http://www.codeproject.com/KB/system/logonsessions.aspx

This article might help.

Thanks, that helps explain the different ways to find the information. I was hoping there was already a UDF that had this nailed down already. Time to stop being lazy... :mellow:

For now I'm using this utnil I can find something better.

Func IsLoggedOn($strComputer = ".")
    Dim $objWMIService, $objItem, $colUsers
    $wbemFlagReturnImmediately = 0x10
    $wbemFlagForwardOnly = 0x20
    $objWMIService = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
    $colUsers = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem WHERE UserName <> NULL", "WQL", _
                                              $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    For $objItem in $colUsers
        Return 1
    Next
EndFunc
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...