Jump to content

run a script on administrator account to check current user


Luigi
 Share

Go to solution Solved by jguinch,

Recommended Posts

Hi forum,

I need run a service it must be check if there are user logged and get your user name.

I found this script in VB and adjust to AutoIt.

The code work fine, it show what I need.

The question is:

1) there are another ways?

2) I think about writing a AdLibRegister ("execute_function", $iTime) stop checking the status of the user ... But this needs to WMI, and will stay exectuando function every $iTime time interval. I know that Windows / AutoIt has lots of functions that fire a trigger when a situation happens, it is possible to implement this function?

3) if such a script, to be executed every 500ms on an old machine (for example: Intel Celeron D326 2.53GH with 2Gb Ram, with Win XP), could present some certain slowness??

 

Thanks, Detefon

Local $strComputer = "." ; " use "." for local computer

Local $objWMI = ObjGet("winmgmts:" _
         & "{impersonationLevel=impersonate}!\\" _
         & $strComputer & "\root\cimv2")

Local $colSessions = $objWMI.ExecQuery _
        ("Select * from Win32_LogonSession Where LogonType = 2 OR LogonType = 10")

If $colSessions.Count = 0 Then
    ConsoleWrite("No interactive users found" & @LF)
Else
    For $objSession In $colSessions
        If $objSession.LogonType = 2 Then
            ConsoleWrite("Logon type: Console" & @LF)
        Else
            ConsoleWrite("Logon type: RDP/Terminal Server" & @LF)
        EndIf
        Local $colList = $objWMI.ExecQuery("Associators of " _
                 & "{Win32_LogonSession.LogonId=" & $objSession.LogonId & "} " _
                 & "Where AssocClass=Win32_LoggedOnUser Role=Dependent")

        For $objItem In $colList
            ConsoleWrite("User: " & $objItem.Name & @LF)
            ConsoleWrite("FullName: " & $objItem.FullName & @LF)
            ConsoleWrite("Domain: " & $objItem.Domain & @LF)
        Next
        ConsoleWrite("Session start time: " & $objSession.StartTime & @LF)
    Next
EndIf
Edited by Detefon

Visit my repository

Link to comment
Share on other sites

I found an alternative, it's work on DOS, but not work when run in Run()

query session /server:computer_name

Have some resctriction to execute "query session"?

The script

;~ #include <Array.au3>
#include <File.au3>

Local $cmd = "query session /server:" & @ComputerName ; don't work
Local $cmd = "dir" ; work

Local $execute = _CMDreturn($cmd)

ConsoleWrite($execute & @LF)

Func _CMDreturn($sCommand) ; This function returns the output of a DOS command as a string
    $cmdreturn = ""
    $stream = Run(@ComSpec & " /c " & $sCommand, @SystemDir, @SW_HIDE, $STDERR_MERGED + $STDIN_CHILD)
    While 1 ; loop through the return from the command until there is no more
        $line = StdoutRead($stream)
        If @error Then ExitLoop
        $cmdreturn &= $line
    WEnd
;~  $cmdreturn = _OEM2ANSI($cmdreturn)
    Return $cmdreturn
EndFunc   ;==>_CMDreturn

Func _OEM2ANSI($Entrada)
    Local $Retorno = DllCall("user32.dll", "Int", "OemToChar", "str", $Entrada, "str", "")
    Return $Retorno[2]
EndFunc   ;==>_OEM2ANSI

Visit my repository

Link to comment
Share on other sites

The query command is available only in the native system32 folder (c:windowssystem32).

If you are using a x64 Windows and a x86 script, then try to set the full path to the command, using the sysnative folder.

Local $cmd = "c:\windows\sysnative\query session"
Link to comment
Share on other sites

  • Solution

Si WMI seems to be good..

A way could be to use WinAPI functions LsaEnumerateLogonSessions + LsaGetLogonSessionData... but it seems to be hard (for me).

Another way could be to use an external tool like SysInternals LogonSessions.

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...