• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

VB scripting question regarding variables

I have a vb script to extract the info from the event log and write it to a txt file on a network share. The problem that I have is that I want to be able to run this on all of my users machines and have it write to a txt file that includes the username or hostname variable so i can have a seperate file from each machine. (hostname.txt otherhostname.txt...). I am a scripting newbie, so any help would be great.

thanks
strComputer = "."
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 
Set colEvents = objWMIService.ExecQuery _
    ("Select * from Win32_NTLogEvent Where LogFile='Application'")
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("\\mar\EVT_LOGS\%USERNAME%")
 
For Each objEvent in colEvents
    strTimeWritten = objEvent.TimeWritten
 
    dtmTimeWritten = CDate(Mid(strTimeWritten, 5, 2) & "/" & _
        Mid(strTimeWritten, 7, 2) & "/" & Left(strTimeWritten, 4) _
            & " " & Mid (strTimeWritten, 9, 2) & ":" & _
                Mid(strTimeWritten, 11, 2) & ":" & Mid(strTimeWritten, 13, 2))
 
    dtmDate = FormatDateTime(dtmTimeWritten, vbShortDate)
    dtmTime = FormatDateTime(dtmTimeWritten, vbLongTime)
 
    strEvent = dtmDate & vbTab
    strEvent = strEvent & dtmTime & vbTab
    strEvent = strEvent & objEvent.SourceName & vbTab
    strEvent = strEvent & objEvent.Type & vbTab
    strEvent = strEvent & objEvent.Category & vbTab
    strEvent = strEvent & objEvent.EventCode & vbTab
    strEvent = strEvent & objEvent.User & vbTab
    strEvent = strEvent & objEvent.ComputerName & vbTab
 
    strDescription = objEvent.Message
    If IsNull(strDescription) Then
        strDescription = "The event description cannot be found."
    End If
    strDescription = Replace(strDescription, vbCrLf, " ")
    strEvent = strEvent & strDescription
 
    objFile.WriteLine strEvent
Next
 
objFile.Close

Open in new window

0
jsparnova
Asked:
jsparnova
1 Solution
 
TakedaTCommented:
This should work for you.  I had the script pull in both variables so you can change it to whichever one you would like to use.

strCompName = objENV("computername")
strUserName = objENV("username")
strComputer = "."
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 
Set colEvents = objWMIService.ExecQuery _
    ("Select * from Win32_NTLogEvent Where LogFile='Application'")
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objENV = objShell.Environment("Process")
strCompName = objENV("computername")
strUserName = objENV("username")
 
Set objFile = objFSO.CreateTextFile("\\mar\EVT_LOGS\"&strUserName&".log")
 
For Each objEvent in colEvents
    strTimeWritten = objEvent.TimeWritten
 
    dtmTimeWritten = CDate(Mid(strTimeWritten, 5, 2) & "/" & _
        Mid(strTimeWritten, 7, 2) & "/" & Left(strTimeWritten, 4) _
            & " " & Mid (strTimeWritten, 9, 2) & ":" & _
                Mid(strTimeWritten, 11, 2) & ":" & Mid(strTimeWritten, 13, 2))
 
    dtmDate = FormatDateTime(dtmTimeWritten, vbShortDate)
    dtmTime = FormatDateTime(dtmTimeWritten, vbLongTime)
 
    strEvent = dtmDate & vbTab
    strEvent = strEvent & dtmTime & vbTab
    strEvent = strEvent & objEvent.SourceName & vbTab
    strEvent = strEvent & objEvent.Type & vbTab
    strEvent = strEvent & objEvent.Category & vbTab
    strEvent = strEvent & objEvent.EventCode & vbTab
    strEvent = strEvent & objEvent.User & vbTab
    strEvent = strEvent & objEvent.ComputerName & vbTab
 
    strDescription = objEvent.Message
    If IsNull(strDescription) Then
        strDescription = "The event description cannot be found."
    End If
    strDescription = Replace(strDescription, vbCrLf, " ")
    strEvent = strEvent & strDescription
 
    objFile.WriteLine strEvent
Next
 
objFile.Close

Open in new window

0
 
jsparnovaAuthor Commented:
Thank you so much. this worked perfectly.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now