Jump to content
Sign in to follow this  
Cyri

Detect if user logged on

Recommended Posts

Cyri

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?

Share this post


Link to post
Share on other sites
Richard Robertson

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

This article might help.

Share this post


Link to post
Share on other sites
Cyri

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

Share this post


Link to post
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
Sign in to follow this  

×