Jump to content

LastLogon GUI, check my code.


Recommended Posts

Basically what I created was a GUI for the 'whereis.bat' part of the lastlogon logon script as see here: http://www.jsifaq.com/SF/Tips/Tip.aspx?id=3437.

What it does is display a list of users, which are users who have logged into the domain, and when you display one, it will display the contents of the file which look like this:

Last logon was 03/02/2001 08:30 from computer name JSI001.

Take a look at the code and let me know what you think.

#include <GUIConstants.au3>
#include <array.au3>
#include <file.au3>

$a1 = IniReadSection(@ScriptDir & "\lastlogon.ini",  "ServerPath")
If @error Then
    MsgBox(4096, "", "Error occurred, probably no INI file." & @CRLF & "Next dialog will create one.")
    $uncpath = InputBox("LastLogon Path", "Please enter your server and path to the lastlogon share in UNC format." & @CRLF & @CRLF & "Example: \\Server\Lastlogon$", "", " M")
    IniWrite(@ScriptDir & "\lastlogon.ini", "ServerPath", "Path", $uncpath)
Else
    For $i = 1 to $a1[0][0]
        ConsoleWrite("Key: " & $a1[$i][0] & @CRLF & "Value: " & $a1[$i][1])
        $sServerPath = $a1[$i][1]
    Next
EndIf

$Form1 = GUICreate("Last Login", 438, 238, 260, 234)
$Group1 = GUICtrlCreateGroup("Search", 8, 24, 409, 57)
$Combo1 = GUICtrlCreateCombo("", 40, 48, 185, 25)
    $lastlogon = DriveMapAdd('*', $sServerPath)
    $aFileArray = _FileListToArrayEx($lastlogon, '*.lli')
    _ArraySort($aFileArray)
    _PopulateCombo($Combo1, $aFileArray)
    DriveMapDel($lastlogon)
$Button1 = GUICtrlCreateButton("Display", 240, 48, 75, 25, 0)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("Info", 8, 96, 409, 105)
$Label1 = GUICtrlCreateLabel("", 24, 120, 385, 73)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            Local $fArray
            $cValue = GUICtrlRead($Combo1)
            $lastlogon = DriveMapAdd('*', $sServerPath)
            If Not _FileReadToArray($lastlogon & "\" & $cValue, $fArray) Then
                MsgBox(4096, "Error", "Error reading lastlogon file." & @CRLF & "Program will now exit.")
                Exit
            EndIf
            For $n = 1 to $fArray[0]
            GUICtrlSetData($Label1, $fArray[$n])
            DriveMapDel($lastlogon)
            ExitLoop
            Next
    EndSwitch
WEnd

Func _FileListToArrayEx($sPath, $sFilter = '*.*', $iFlag = 0, $sExclude = '')
    If Not FileExists($sPath) Then Return SetError(1, 1, '')
    If $sFilter = -1 Or $sFilter = Default Then $sFilter = '*.*'
    If $iFlag = -1 Or $iFlag = Default Then $iFlag = 0
    If $sExclude = -1 Or $sExclude = Default Then $sExclude = ''
    Local $aBadChar[6] = ['', '/', ':', '>', '<', '|']
    For $iCC = 0 To 5
        If StringInStr($sFilter, $aBadChar[$iCC]) Or _
            StringInStr($sExclude, $aBadChar[$iCC]) Then Return SetError(2, 2, '')
    Next
    If StringStripWS($sFilter, 8) = '' Then Return SetError(2, 2, '')
    If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3, 3, '')
    If Not StringInStr($sFilter, ';') Then $sFilter &= ';'
    Local $aSplit = StringSplit(StringStripWS($sFilter, 8), ';'), $sRead
    For $iCC = 1 To $aSplit[0]
        If StringStripWS($aSplit[$iCC], 8) = '' Then ContinueLoop
        If StringLeft($aSplit[$iCC], 1) = '.' And _
            UBound(StringSplit($aSplit[$iCC], '.')) - 2 = 1 Then $aSplit[$iCC] = '*' & $aSplit[$iCC]
        Local $iPid = Run(@ComSpec & ' /c ' & 'dir "' & $sPath & '' & $aSplit[$iCC]  _
                & '" /b /o-e /od', '', @SW_HIDE, 6)
        While 1
            $sRead &= StdoutRead($iPid)
            If @error Then ExitLoop
        WEnd
    Next
    If StringStripWS($sRead, 8) = '' Then SetError(4, 4, '')
    Local $aFSplit = StringSplit(StringTrimRight(StringStripCR($sRead), 1), @LF)
    Local $sHold
    For $iCC = 1 To $aFSplit[0]
        If $sExclude And StringLeft($aFSplit[$iCC], _
            StringLen(StringReplace($sExclude, '*', ''))) = StringReplace($sExclude, '*', '') Then ContinueLoop
        Switch $iFlag
            Case 0
                $sHold &= $aFSplit[$iCC] & Chr(1)
            Case 1
                If StringInStr(FileGetAttrib($sPath & '' & $aFSplit[$iCC]), 'd') Then ContinueLoop
                $sHold &= $aFSplit[$iCC] & Chr(1)
            Case 2
                If Not StringInStr(FileGetAttrib($sPath & '' & $aFSplit[$iCC]), 'd') Then ContinueLoop
                $sHold &= $aFSplit[$iCC] & Chr(1)
        EndSwitch
    Next
    If StringTrimRight($sHold, 1) Then Return StringSplit(StringTrimRight($sHold, 1), Chr(1))
    Return SetError(4, 4, '')
EndFunc

Func _PopulateCombo($hwndCTRLID, $vInfo)
    Local $sStoreForCombo = ''
    For $iCount = 1 To UBound($vInfo) - 1
        If $vInfo[$iCount] <> '' Then $sStoreForCombo &= $vInfo[$iCount] & '|'
    Next
    GUICtrlSetData($hwndCTRLID, $sStoreForCombo)
EndFunc
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...