Sign in to follow this  
Followers 0
Zinthose

_IsUserLoggedIn

4 posts in this topic

The following is a simple code snippet to determine if a user is currently logged into the system. This was needed for automating software deployments to systems on our network.

Example Scenario:

Using Altiris I schedule a software deployment to occur over the week end.

I use Wake-On-LAN to power on any offline systems.

If the system is at the log on screen AND no users are currently logged in, then the software will install without any custom prompts.

If the system is locked, or a user is actively logged in, then the software will display a custom prompt.

I know it's a limited and specialized use but I'm posting it here is hopes someone will find a use for it.

#Region : Example

    If _IsUserLoggedIn() Then
        Msgbox(0, "_IsUserLoggedIn()", "A user is currently logged into the system.")
    Else
        Msgbox(0, "_IsUserLoggedIn()", "No users are logged into the system.")
    EndIf

#EndRegion


Func _IsUserLoggedIn($HostName = Default)
    Local $WMIService; As Object
    Local $Computers; As Collection
    Local $Computer; As Object
    
    ;## Check for Defaults
        If $HostName = Default Then $HostName = "."
    
    $WMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $HostName & "\root\cimv2") 
    $Computers = $WMIService.ExecQuery("Select * from Win32_ComputerSystem")
    
    For $Computer in $Computers
        If $Computer.UserName = "" Then Return False
    Next
    
    Return True
EndFunc

--- TTFN

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

The following is a simple code snippet to determine if a user is currently logged into the system. This was needed for automating software deployments to systems on our network.

Example Scenario:

Using Altiris I schedule a software deployment to occur over the week end.

I use Wake-On-LAN to power on any offline systems.

If the system is at the log on screen AND no users are currently logged in, then the software will install without any custom prompts.

If the system is locked, or a user is actively logged in, then the software will display a custom prompt.

I know it's a limited and specialized use but I'm posting it here is hopes someone will find a use for it.

Found this VERY useful, thanks!!

Modified it to make the name of the user available :

Func _IsUserLoggedIn($HostName = Default)
    Local $WMIService; As Object
    Local $Computers; As Collection
    Local $Computer; As Object

;## Check for Defaults
    If $HostName = Default Then $HostName = "."

    $WMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $HostName & "\root\cimv2")
    $Computers = $WMIService.ExecQuery("Select * from Win32_ComputerSystem")

    For $Computer In $Computers
        If $Computer.UserName = "" Then Return False
    Next
    Global $loggedinuser = $Computer.UserName
    Return True
EndFunc  ;==>_IsUserLoggedIn
Edited by Graywalker

Share this post


Link to post
Share on other sites

this only seems to work if the account name you are using is logged into the remote system.. only then will it report that someone is logged in.. otherwise it will fail - that has been the result of my test anyway (checked like 10 pcs some where i am logged in some where i am not)

thanks

The following is a simple code snippet to determine if a user is currently logged into the system. This was needed for automating software deployments to systems on our network.

Example Scenario:

Using Altiris I schedule a software deployment to occur over the week end.

I use Wake-On-LAN to power on any offline systems.

If the system is at the log on screen AND no users are currently logged in, then the software will install without any custom prompts.

If the system is locked, or a user is actively logged in, then the software will display a custom prompt.

I know it's a limited and specialized use but I'm posting it here is hopes someone will find a use for it.

#Region : Example

    If _IsUserLoggedIn() Then
        Msgbox(0, "_IsUserLoggedIn()", "A user is currently logged into the system.")
    Else
        Msgbox(0, "_IsUserLoggedIn()", "No users are logged into the system.")
    EndIf

#EndRegion


Func _IsUserLoggedIn($HostName = Default)
    Local $WMIService; As Object
    Local $Computers; As Collection
    Local $Computer; As Object
    
    ;## Check for Defaults
        If $HostName = Default Then $HostName = "."
    
    $WMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $HostName & "\root\cimv2") 
    $Computers = $WMIService.ExecQuery("Select * from Win32_ComputerSystem")
    
    For $Computer in $Computers
        If $Computer.UserName = "" Then Return False
    Next
    
    Return True
EndFunc

Share this post


Link to post
Share on other sites

this only seems to work if the account name you are using is logged into the remote system.. only then will it report that someone is logged in.. otherwise it will fail - that has been the result of my test anyway (checked like 10 pcs some where i am logged in some where i am not)

thanks

I manage an Altiris Client Management server and sometimes when deploying a package I need to determine if the package is being executed as the current user or as the system account. This is the primary purpose of this script when I wrote it.


--- TTFN

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  
Followers 0