Simpel

SecondsAfterLogin

2 posts in this topic

Hi,

after I recognized that some of my scripts fail if they are at windows startup I researched for a solution. Problem could be fixed if I wait about 120 seconds after login. So how to measure this?

#include-once
#include <AD.au3>
#include <Date.au3>

; #FUNCTION# ====================================================================================================================
; Name ..........: _SecondsAfterLogin
; Description ...: Returns how many seconds are gone since last login
; Syntax ........: _SecondsAfterLogin()
; Parameters ....: None
; Return values .: Success - Integer of seconds
;                  Failure - 0, sets @error to 1
; Author ........: Conrad Zelck
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _SecondsAfterLogin()
    Local $sDate
    Local $iSec
    $sDate = __GetLogonTime() ; UTC
    If @error Then
        $sDate = __GetLogonTime_AD() ; local time
        If @error Then Return SetError(1, 0, 0)
    Else
        $sDate = __LocalTime($sDate) ; UTC to local time
    EndIf
    $iSec = __TimeDifference($sDate)
    Return $iSec
EndFunc

#region - INTERNAL_USE_ONLY
Func __GetLogonTime($sUserName = @UserName, $sComputerName = @ComputerName) ; Idea by trancexx: http://www.autoitscript.com/forum/topic/113611-if-isadmin-not-detected-as-admin/
    Local $aRet = DllCall("netapi32.dll", "long", "NetUserGetInfo", "wstr", $sComputerName, "wstr", $sUserName, "dword", 11, "ptr*", 0)
    If @error Or $aRet[0] Then Return SetError(1, 0, 0)
    Local $sSeconds = DllStructGetData(DllStructCreate("ptr;ptr;ptr;ptr;dword;dword;dword;ptr;ptr;dword;dword;dword;dword;ptr;dword;ptr;dword;dword;byte;dword", $aRet[4]), 10)
    DllCall("netapi32.dll", "long", "NetApiBufferFree", "ptr", $aRet[4])
    Local $sLastLogon = _DateAdd('s', Number($sSeconds), "1970/01/01 00:00:00")
    Return $sLastLogon
EndFunc

Func __GetLogonTime_AD()
    Local $iSuccess = _AD_Open()
    If $iSuccess = 1 Then
        Local $sDate = _AD_GetLastLoginDate()
        $sDate = __NumberDate_StringDate($sDate)
        _AD_Close()
        Return $sDate
    Else
        Return SetError(1, 0, 0)
    EndIf
EndFunc

Func __NumberDate_StringDate($sDate)
    Local $y, $m, $d, $h, $min, $s
    $y = StringMid($sDate,1, 4)
    $m = StringMid($sDate,5, 2)
    $d = StringMid($sDate,7, 2)
    $h = StringMid($sDate,9, 2)
    $min = StringMid($sDate,11, 2)
    $s = StringMid($sDate,13, 2)
    $sDate = $y & "/" & $m & "/" & $d & " " & $h & ":" & $min & ":" & $s
    Return $sDate
EndFunc

Func __LocalTime($sDate)
    Local $aTimeZone = _Date_Time_GetTimeZoneInformation()
    If $aTimeZone[0] <> 2 Then
        $iTimeZoneOffset = ($aTimeZone[1]) * -1
    Else
        $iTimeZoneOffset = ($aTimeZone[1] + $aTimeZone[7]) * -1
    EndIf
    $iTimeZoneOffset /= 60 ; in hours
    Local $sHour = StringMid($sDate, 12, 2)
    $sHour += $iTimeZoneOffset
    $sDate = StringLeft($sDate, 11) & $sHour & StringRight($sDate, 6)
    Return $sDate
EndFunc

Func __TimeDifference($sDate)
    Local $sNow = _NowCalc()
    Local $sDiff = _DateDiff("s", $sDate, $sNow)
    Local $iDiff = Number($sDiff)
    Return $iDiff
EndFunc
#endregion - INTERNAL_USE_ONLY

I had to look what kind of login it is. Some of my computers are logging in with active directory and other not.

Here is an example:

#include <SecondsAfterLogin.au3>

Local $iSec = _SecondsAfterLogin()
MsgBox(0, '', "Time since last login: " & _HHMMSS($iSec, "s"))
Exit

Func _HHMMSS($iTime, $sType = "ms")
    If $sType = "s" Then
        Local $sHHMMSS = StringFormat("%.2d:%.2d:%.2d", (Floor($iTime / 3600)), (Floor(Mod($iTime,3600) / 60)), (Mod(Mod($iTime,3600),60)))
    Else
        Local $sHHMMSS = StringFormat("%.2d:%.2d:%.2d", (Floor($iTime / 3600000)), (Floor(Mod($iTime,3600000) / 60000)), (Mod(Mod($iTime,3600000),60000) / 1000))
    EndIf
    Return $sHHMMSS
EndFunc

I hope someone find it useful.

Regards, Conrad

P.S. Review is welcome.


SciTE = 3.6.2.0/full   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win7Pro SP1   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE   H:\...\AutoIt3   H:\...\AutoIt3\Include   H: = Network Drive

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Share this post


Link to post
Share on other sites



#2 ·  Posted

Hi.

I added a small function _WaitSecAfterLogin():

#include-once
#include <AD.au3>
#include <Date.au3>

; #FUNCTION# ====================================================================================================================
; Name ..........: _SecondsAfterLogin
; Description ...: Returns how many seconds are gone since last login
; Syntax ........: _SecondsAfterLogin()
; Parameters ....: None
; Return values .: Success - Integer of seconds
;                  Failure - 0, sets @error to 1
; Author ........: Conrad Zelck
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _SecondsAfterLogin()
    Local $sDate
    Local $iSec
    $sDate = __GetLogonTime() ; UTC
    If @error Then
        $sDate = __GetLogonTime_AD() ; local time
        If @error Then Return SetError(1, 0, 0)
    Else
        $sDate = __LocalTime($sDate) ; UTC to local time
    EndIf
    $iSec = __TimeDifference($sDate)
    Return $iSec
EndFunc

; #FUNCTION# ====================================================================================================================
; Name ..........: _WaitSecAfterLogin
; Description ...: Sleeps the programm if $iWaitSecAfterLogin is greater then seconds after last login
; Syntax ........: _WaitSecAfterLogin([$iWaitSecAfterLogin = 180])
; Parameters ....: $iWaitSecAfterLogin  - [optional] an integer value. Default is 180.
; Return values .: None
; Author ........: Conrad Zelck
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _WaitSecAfterLogin($iWaitSecAfterLogin = 180)
    Local $iSec = _SecondsAfterLogin()
    If Not @error Then
        While $iSec < $iWaitSecAfterLogin
            TraySetToolTip("Wait " & $iWaitSecAfterLogin - $iSec & " seconds.")
            Sleep(1000)
            $iSec += 1
        WEnd
        TraySetToolTip()
    EndIf
EndFunc


#region - INTERNAL_USE_ONLY
Func __GetLogonTime($sUserName = @UserName, $sComputerName = @ComputerName) ; Idea by trancexx: http://www.autoitscript.com/forum/topic/113611-if-isadmin-not-detected-as-admin/
    Local $aRet = DllCall("netapi32.dll", "long", "NetUserGetInfo", "wstr", $sComputerName, "wstr", $sUserName, "dword", 11, "ptr*", 0)
    If @error Or $aRet[0] Then Return SetError(1, 0, 0)
    Local $sSeconds = DllStructGetData(DllStructCreate("ptr;ptr;ptr;ptr;dword;dword;dword;ptr;ptr;dword;dword;dword;dword;ptr;dword;ptr;dword;dword;byte;dword", $aRet[4]), 10)
    DllCall("netapi32.dll", "long", "NetApiBufferFree", "ptr", $aRet[4])
    Local $sLastLogon = _DateAdd('s', Number($sSeconds), "1970/01/01 00:00:00")
    Return $sLastLogon
EndFunc

Func __GetLogonTime_AD()
    Local $iSuccess = _AD_Open()
    If $iSuccess = 1 Then
        Local $sDate = _AD_GetLastLoginDate()
        $sDate = __NumberDate_StringDate($sDate)
        _AD_Close()
        Return $sDate
    Else
        Return SetError(1, 0, 0)
    EndIf
EndFunc

Func __NumberDate_StringDate($sDate)
    Local $y, $m, $d, $h, $min, $s
    $y = StringMid($sDate,1, 4)
    $m = StringMid($sDate,5, 2)
    $d = StringMid($sDate,7, 2)
    $h = StringMid($sDate,9, 2)
    $min = StringMid($sDate,11, 2)
    $s = StringMid($sDate,13, 2)
    $sDate = $y & "/" & $m & "/" & $d & " " & $h & ":" & $min & ":" & $s
    Return $sDate
EndFunc

Func __LocalTime($sDate)
    Local $aTimeZone = _Date_Time_GetTimeZoneInformation()
    If $aTimeZone[0] <> 2 Then
        $iTimeZoneOffset = ($aTimeZone[1]) * -1
    Else
        $iTimeZoneOffset = ($aTimeZone[1] + $aTimeZone[7]) * -1
    EndIf
    $iTimeZoneOffset /= 60 ; in hours
    Local $sHour = StringMid($sDate, 12, 2)
    $sHour += $iTimeZoneOffset
    $sDate = StringLeft($sDate, 11) & $sHour & StringRight($sDate, 6)
    Return $sDate
EndFunc

Func __TimeDifference($sDate)
    Local $sNow = _NowCalc()
    Local $sDiff = _DateDiff("s", $sDate, $sNow)
    Local $iDiff = Number($sDiff)
    Return $iDiff
EndFunc
#endregion - INTERNAL_USE_ONLY

Default is 180 seconds. So the programm will sleep until 180 seconds after last login are gone.

This I use very often when the programm is needing mounting network drives after startup etc.

Regards, Conrad


SciTE = 3.6.2.0/full   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win7Pro SP1   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE   H:\...\AutoIt3   H:\...\AutoIt3\Include   H: = Network Drive

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

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

  • Similar Content

    • spuuunit
      By spuuunit
      I have a script that autostart with Windows, and sometimes some of my code is skipped. My script takes long pauses, so it's not that the script "goes too fast". This is my script for now:
      TraySetIcon("C:\Portables\AutoIt\_\icon.ico") AutoItSetOption("WinTitleMatchMode", 2) Run("C:\Portables\Key Manager\keymanager.exe") WinActivate("ATNSOFT Key Manager v") WinWaitActive("ATNSOFT Key Manager v") WinMove("ATNSOFT Key Manager v", "", 650, 200, 600, 800) Sleep(500) $pos = MouseGetPos() MouseClick("left", 1220, 210, 1, 100) MouseMove($pos[0], $pos[1], 100) Run("C:\Portables\foobar2000\foobar2000.exe") $version = 298 For $i = 0 To 99 Step 1 $path = "C:\Users\spunit\AppData\Local\Discord\app-0.0." & $version + $i & "\Discord.exe" If FileExists($path) Then Run($path) ExitLoop EndIf Next Run("C:\Portables\Firefox\FirefoxPortable.exe") Sleep(10000) WinMove("- Mozilla Firefox", "", 205, 0, 1214, 1047) Sleep(100) MouseClick("left", 1300, 15, 1, 10) Sleep(100) MouseClick("left", 1130, 275, 1, 10) Sleep(100) MouseClick("left", 930, 105, 1, 10) Sleep(100) Send("#{m}") Sometimes it skips to click and move mouse right after Key Manager starts. Almost always it skips everything after the last run.

      This type of script were never an issue with Windows 7... Any thoughts?
    • kawliga751
      By kawliga751
      I am trying to script the opening of multiple tabs in IE with each tab having a separate login/password. I have been able to make a successful script that will open 3 separate IE sessions with the correct webpage and login or a script that will open 3 tabs in one session (which I want) but will not login. I have tried _IECreate, IEAttach etc. This is the script I am currently using . 
      #include <IE.au3> Const $navOpenInNewTab = 0x0800 Dim $oIE = _IECreate('http://asag.xxxxxxx.com/AAAA/index.htm') ;$o_IE.Navigate2('http://asag.xxxxxxx.com/apps/yyyyyyy/LogInSAG/login.asp', $navOpenInNewTab) ;$o_IE.Navigate2('http://asag.xxxxxxx.com/apps/yyyyyyy/LogInSAG/login.asp', $navOpenInNewTab) Call ("YYYYYYYSignIn") Func YYYYYYYSignIn () Local $username = _IEGetObjByName ($oIE,"uname") Local $password = _IEGetObjByName ($oIE,"pword") $oIE.Navigate2('http://asag.xxxxxxx.com/apps/xxxxxxx/LogInSAG/login.asp',2048) _IEFormElementSetValue ($username, "xxxxxxx") _IEFormElementSetValue ($password, "xxxxxxx") Send('{Enter}') EndFunc Call ("xxxxxxxSignIn") Func xxxxxxxSignIn () __IENavigate($oIE, "https://soa1gui.xxxxxxx.biz/gateway/NFServlet;jsessionid=1E3B8F6520DE0A1EC914A8ABB7E86341?NFH_MessageId=&NFH_Page=%2fpages%2fsearch%2fsearch.jsp", 1, 0x800) Local $domain = _IEGetObjByName ($oIE,"NF_CustomerID") Local $User_Name = _IEGetObjByName ($oIE,"NF_UserName") Local $Password = _IEGetObjByName ($oIE,"NF_Password") _IEFormElementSetValue ($domain, xxxxxxx") _IEFormElementSetValue ($User_Name, "xxxxxxx") _IEFormElementSetValue ($Password, "xxxxxxx") ;Send('{Enter}') EndFunc  
    • Fhelipe
      By Fhelipe
      Nothing More
    • Simpel
      By Simpel
      Hi,
      I wanted to use _GetLogonTime() by guinness (https://www.autoitscript.com/forum/topic/19370-autoit-wrappers/?do=findComment&comment=942069).
      #include <Date.au3> #include <Array.au3> ConsoleWrite(_GetLogonTime() & @LF) Func _GetLogonTime($sUserName = @UserName, $sComputerName = @ComputerName) ; Idea by trancexx: http://www.autoitscript.com/forum/topic/113611-if-isadmin-not-detected-as-admin/ Local $aRet = DllCall("netapi32.dll", "long", "NetUserGetInfo", "wstr", $sComputerName, "wstr", $sUserName, "dword", 11, "ptr*", 0) _ArrayDisplay($aRet) If @error Or $aRet[0] Then Return SetError(1, 0, False) Local $sHours = DllStructGetData(DllStructCreate("ptr;ptr;ptr;ptr;dword;dword;dword;ptr;ptr;dword;dword;dword;dword;ptr;dword;ptr;dword;dword;byte;dword", $aRet[4]), 18) DllCall("netapi32.dll", "long", "NetApiBufferFree", "ptr", $aRet[4]) Return _DateAdd("h", "-" & $sHours, _NowCalc()) EndFunc ;==>_GetLogonTime It always returns false.
      I displayed $aRet:
      [0]|2221
      [1]|mycomputer
      [2]|myuser
      [3]|11
      [4]|0x0000000000000000
      $aRet[4] shouldn't be 0x0000000000000000 right? Any help?
      Regards, Conrad
       
    • computergroove
      By computergroove
      I am trying to start my script on Windows startup and it doesn't run. I have disabled defender and smartscreen with no luck. There is no antivirus on this machine. I have added the script to shell:startup, shell:common startup and HKLM\Software\Microsoft\Windows\Current Version\Run all with no luck. Im at a loss. Please help.