Announcement

Collapse
No announcement yet.

Keep a log of the users logon (was: who, wher, when......)

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Keep a log of the users logon (was: who, wher, when......)

    Who Where When……..:
    I want to keep a log of the users logon. This includes login date, login time, workstation name, IP address, username and logout time.
    Here is the way that I proceeded:
    I created a hidden share on the server’s data drive (log$) and gave every users read-write access. (They cannot do any arm as they are not able to see it because of the “$” at the end of the sharing name.)
    Within this folder I have 2 other folders:
    -Cleanup (contains mergefiles.vbs)
    -Compiled (contains one folder for each month of the year)
    How does it works:
    Everytime somebody logs in the network, the login script who-when-where.vbs (part of the GPO) will create a small text file in the log folder. The file is named after the workstation name.
    When the user logs out, logout time will be appended to the file.
    At the end of the day (9:00 pm), I start a schedule process running the file mergefiles.vbs. I reads all the text files created during the day and compiled them into a csv(excel). This file is stored under the compiled directory. All the text files created during the day are then deleted.
    The file mergefiles.vbs will also delete any csv older than 2 months.
    With over 700 login a day, the size of the daily log is no more than 50Kb

    ************************************************** *********************************************
    ---------------------------------------
    - logon script (who-where-when.vbs ): -
    ---------------------------------------
    Const ForWriting = 2
    Const ForAppending = 8
    dim info

    arAddresses = GetIPAddresses()
    info = ""
    for each ip in arAddresses
    info = info & ip' & vbTab & GetFQDN(ip) & vbCR
    next
    'WScript.echo info

    Function GetIPAddresses()
    set sh = createobject("wscript.shell")
    set fso = createobject("scripting.filesystemobject")
    Set Env = sh.Environment("PROCESS")
    if Env("OS") = "Windows_NT" then
    ' workfile = fso.gettempname
    workfile = "h:\tmpip.tmp"
    sh.run "%comspec% /c ipconfig > " & workfile,0,true
    else
    'winipcfg in batch mode sends output to
    'filename winipcfg.out
    workfile = "winipcfg.out"
    sh.run "winipcfg /batch" ,0,true
    end if
    set sh = nothing
    set ts = fso.opentextfile(workfile)
    data = split(ts.readall,vbcr)
    ts.close
    set ts = nothing
    fso.deletefile workfile
    set fso = nothing
    arIPAddress = array()
    index = -1
    for n = 0 to ubound(data)
    if instr(data(n),"IP Address") then
    parts = split(data(n),":")
    if trim(parts(1)) <> "0.0.0.0" then
    index = index + 1
    ReDim Preserve arIPAddress(index)
    arIPAddress(index)= trim(cstr(parts(1)))
    end if
    end if
    next
    GetIPAddresses = arIPAddress
    End Function

    Set objShell = CreateObject("WScript.Shell")

    strComputer = objShell.ExpandEnvironmentStrings("%computername%" )
    strUser = objShell.ExpandEnvironmentStrings("%username%")
    '************************************************* ******************
    '* setup the path of the hidden share folder to store the log file *
    '************************************************* ******************
    STRLOG = "\\adhs01\log$\" & STRCOMPUTER & ".txt"


    Set objFSO = CreateObject("Scripting.FileSystemObject")

    If Not objFSO.FileExists(strLog) Then
    Set objFile = objFSO.CreateTextFile(strLog, ForWriting)
    ' objFile.WriteLine "Workstation Name Time Logged In"
    Else
    Set objFile = objFSO.OpenTextFile(strLog, ForAppending)
    End If

    objFile.Write strComputer & "," &info & "," &struser & "," &date & "," &time & ","
    objFile.Close

    ************************************************** ****************************************
    ---------------------
    - logout.vbs script -
    ---------------------
    Const ForWriting = 2
    Const ForAppending = 8

    Set objShell = CreateObject("WScript.Shell")

    strComputer = objShell.ExpandEnvironmentStrings("%computername%" )
    strUser = objShell.ExpandEnvironmentStrings("%username%")
    '***********************************
    '* setup the path for the log file *
    '***********************************
    STRLOG= "\\adhs01\log$\" & STRCOMPUTER & ".txt"
    Set WshShell = WScript.CreateObject("WScript.Shell")
    WshShell.Run "%windir%\system32\cmd.exe /c ",0,true

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    If Not objFSO.FileExists(strLog) Then
    Set objFile = objFSO.CreateTextFile(strLog, ForWriting)
    Else
    Set objFile = objFSO.OpenTextFile(strLog, ForAppending)
    End If

    objFile.Write time
    objFile.WriteLine " "
    objFile.Close

    ************************************************** *************************************
    --------------------------
    - mergefiles.vbs script: -
    --------------------------

    '************************************************* ********
    '* This section is where you set your personal variables *
    '************************************************* ********
    resultpath= "d:\log\compiled\" 'path where the compiled report is saved
    sourcepath= "d:\log\" 'source of individual logs

    '************************************************* ********
    select case month(date)
    case 1 compiledpath= resultpath & "January\"
    case 2 compiledpath= resultpath & "February\"
    case 3 compiledpath= resultpath & "March\"
    case 4 compiledpath= resultpath & "April\"
    case 5 compiledpath= resultpath & "May\"
    case 6 compiledpath= resultpath & "June\"
    case 7 compiledpath= resultpath & "July\"
    case 8 compiledpath= resultpath & "August\"
    case 9 compiledpath= resultpath & "September\"
    case 10 compiledpath= resultpath & "October\"
    case 11 compiledpath= resultpath & "November\"
    case 12 compiledpath= resultpath & "December\"
    end select
    opName = compiledpath & weekdayname(Weekday(Date))& "-" &(Day(Date)) & ".csv"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set outFile = objFSO.OpenTextFile(opName,8, True)
    'create blank file to avoid error if directory is empty
    ' Set fso = CreateObject("Scripting.FileSystemObject")
    ' sFolder = compiledpath
    ' sfile= compiledpath & weekdayname(Weekday(Date)) & ".csv"
    ' Set NewFile = fso.CreateTextFile(sfile, ForAppending)
    'newfile.close
    'Loop through input files
    Set srcDir = objFSO.GetFolder("d:\log")
    Set files = srcDir.Files
    For Each file in files
    If objFSO.getExtensionName(file) <> "vbs" AND objFSO.getFileName(file) <> opName AND objFSO.getFileName(file) <>"" Then
    On Error Resume Next
    Set fileN = objFSO.OpenTextFile(file, 1)

    content = fileN.readAll
    outFile.Write content
    If Err.Number <> 0 Then
    Wscript.Echo Err.Description & ": " & fileN
    End If
    End If
    Next

    outFile.Close
    fileN.close
    Set fso=CreateObject("Scripting.FileSystemObject")
    CleanPath=sourcepath
    For Each file In fso.GetFolder(CleanPath).Files
    file.delete
    Next
    '************************************
    '* Deleting logs older than 2 month *
    '************************************
    select case month(date)
    case 1 compiledpath= resultpath & "November\"
    case 2 compiledpath= resultpath & "December\"
    case 3 compiledpath= resultpath & "January\"
    case 4 compiledpath= resultpath & "February\"
    case 5 compiledpath= resultpath & "March\"
    case 6 compiledpath= resultpath & "April\"
    case 7 compiledpath= resultpath & "May\"
    case 8 compiledpath= resultpath & "June\"
    case 9 compiledpath= resultpath & "July\"
    case 10 compiledpath= resultpath & "August\"
    case 11 compiledpath= resultpath & "September\"
    case 12 compiledpath= resultpath & "October\"
    end select
    Set fso=CreateObject("Scripting.FileSystemObject")
    CleanPath=compiledpath
    'if (Day(Date))= 31 then
    For Each file In fso.GetFolder(CleanPath).Files
    file.delete
    Next
    Last edited by bertv; 8th September 2008, 22:51.

  • #2
    Re: Keep a log of the users logon (was: who, wher, when......)

    Bertrand I changed the title of the thread so it become usable for others when searching the scripting forum.

    If you have posted the samples to ask a question, then ask that question. If you posted the scripts to share, then say so (and it would be nice to explain the code a litte bit).

    And please do wrap all code in your posts between [code] tags, it'll make your scripts better readable and better to copy.

    b.t.w. Your logonscript will only work for the English OSs!
    What was your motivation to use vbs and not a faster batch for the type of logging the script does (using vbs you could write directly to a protected MS database if you want)?


    \Rems
    Last edited by Rems; 9th September 2008, 08:34.

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment

    Working...
    X