Connect to remote servers.

I am using the code below to get the security log date from the local server. Can someone tell me how to adjust the code so that I can get data from a remote server.

Dim logType As String = "Security"
       
        Dim ev As New EventLog(logType, System.Environment.MachineName)

        Dim LastLogToShow As Integer = ev.Entries.Count
        If LastLogToShow <= 0 Then
            Console.WriteLine("No Event Logs in the Log :" & logType)
            Exit Sub
        End If

        For Each entry In ev.Entries
            sep = "---------------------------------------------------------------------------"
            'WriteToFile("Event ID : " & entry.EventID & vbCrLf & "Entry Type : " & entry.EntryType.ToString() & vbCrLf & "Message :  " & entry.Message & vbCrLf & sep & vbCrLf)
            'If entry.EventID = "5061" Then
            WriteToFile("Event ID : " & entry.EventID & vbCrLf & "Entry Type : " & entry.EntryType.ToString() & vbCrLf & "Message :  " & entry.Message & vbCrLf & sep & vbCrLf)
            'End If
        Next
        ev.Close()
dthillsrAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CoralonCommented:
The easiest thing to do (probably) would be to use powershell, but from a vb/vbscript perspective..
The "base" sample code is:
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NTLogEvent",,48)
For Each objItem in colItems
    Wscript.Echo "Category: " & objItem.Category
    Wscript.Echo "CategoryString: " & objItem.CategoryString
    Wscript.Echo "ComputerName: " & objItem.ComputerName
    Wscript.Echo "Data: " & objItem.Data
    Wscript.Echo "EventCode: " & objItem.EventCode
    Wscript.Echo "EventIdentifier: " & objItem.EventIdentifier
    Wscript.Echo "InsertionStrings: " & objItem.InsertionStrings
    Wscript.Echo "Logfile: " & objItem.Logfile
    Wscript.Echo "Message: " & objItem.Message
    Wscript.Echo "RecordNumber: " & objItem.RecordNumber
    Wscript.Echo "SourceName: " & objItem.SourceName
    Wscript.Echo "TimeGenerated: " & objItem.TimeGenerated
    Wscript.Echo "TimeWritten: " & objItem.TimeWritten
    Wscript.Echo "Type: " & objItem.Type
    Wscript.Echo "User: " & objItem.User
Next

Open in new window

So, with a quick where clause.. and a minor modification, you could output it to a file.. something like this (might need to be tweaked)
On Error Resume Next
set fso = CreateObject("scripting.filesystemobject")
oFile = fso.CreateTextFile("c:\temp\EventLogEntries.txt")

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NTLogEvent WHERE (EventIdentifier = '5061') AND (LogFile = 'Security')",,48)
For Each objItem in colItems
	sOutString = "Event ID : " & objItem.EventIdentifier & vbCrLf   
	sOutString = sOutString & "Entry Type : " & objItem.Type & vbCrLf
	sOutString = sOutString & "Message :  " & objItem.Message & vbCrLf
	sOutString = sOutString & "------------------------------------" & vbCrLf
    fso.WriteLine(sOutString)
Next

oFile.Close()
set oFile = nothing
set fso = nothing 

Open in new window


Coralon

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.