Zinthose Posted August 15, 2008 Share Posted August 15, 2008 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 Link to comment Share on other sites More sharing options...
Graywalker Posted March 26, 2009 Share Posted March 26, 2009 (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 March 26, 2009 by Graywalker Link to comment Share on other sites More sharing options...
gcue Posted April 20, 2009 Share Posted April 20, 2009 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 Link to comment Share on other sites More sharing options...
Zinthose Posted April 20, 2009 Author Share Posted April 20, 2009 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)thanksI 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 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now