From my local desktop I need to connect to remote server and execute vb script that uses MMC and get events (application, security etc). For some reason it seems like the script on my local desktop does not execute the script on the remote server (which is a .BAT file calling a .VBS). If I call the .BAT file on the remote server directly it work fine.
............................................................
Script on local desktop:
dim strComputer, strCommand, objWMIService, objProcess, intProcessID
'strComputer = "."
strComputer = "vgiwpw03-sql3"
strCommand = "MMC Event Viewer Remote Server.bat"
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objProcess = objWMIService.Get("Win32_Process")
errReturn = objProcess.Create(strCommand, null, null, intProcessID)
If errReturn = 0 Then
Wscript.Echo "MMC Event Viewer on remote server was started with a process ID: " & intProcessID
Else
Wscript.Echo "MMC Event Viewer on remote server could not be started due to error: " & errReturn
End If
This code should call a .bat file on the remote server. When I run the script I get that a process ID was created but it does not seem that the .BAT is calling the .VBS on th remote server:
............................................................
.BAT file on remote server - MMC Event Viewer Remote Server.bat
@ECHO OFF
cscript "MMC EVent Viewer Remote Server.vbs"
............................................................
Script file on remote server -MMC EVent Viewer Remote Server.vbs
Option Explicit
Dim Locator
Dim Service
Dim ManagementAgent
Set Locator = CreateObject("WbemScripting.SWbemLocator")
Set Service = Locator.ConnectServer("vgiwpw03-sql3", "root\cimv2")
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
public objFso, objTextFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\EventLogFile.txt", ForAppending, True)
' Create the MMC Application object.
Dim objMMC
Set objMMC = Wscript.CreateObject("MMC20.Application")
' Load a console file.
' The console file in this case contains the Event Viewer snap-in.
' This console file ships with the operating system.
objMMC.Load("eventvwr.msc")
' Retrieve the Document object. The Document object provides access
' to the ScopeNamespace and ActiveView objects.
Dim objDoc
Set objDoc = objMMC.Document
' Retrieve the ScopeNamespace object. The ScopeNamespace object
' will be used when navigating the scope tree.
Dim objSN
Set objSN = objDoc.ScopeNamespace
' Get the console root node.
Dim objRoot
Set objRoot = objDoc.RootNode
' Using the console Root Node, get the Event Viewer node.
Dim objEvtVwrNode
Set objEvtVwrNode = objSN.GetChild(objRoot)
' Expand the Event Viewer Node.
objSN.Expand(objEvtVwrNode)
' Get the ActiveView, which is a View object.
' This object is used to access the list of nodes and column data.
Dim objView
Set objView = objDoc.ActiveView
' Get the first child node of the Event Viewer node.
On Error Resume Next
Dim objNode
Set objNode = Nothing
Set objNode = objSN.GetChild(objEvtVwrNode)
if (objNode Is Nothing) then
' Unexpected condition.
objTextFile.WriteLine("Unable to get Event Viewer child node.")
'Wscript.echo "Unable to get Event Viewer child node."
'Wscript.quit
end if
Dim objSib ' Used when moving to the next child (sibling) node.
' Loop through each Event Viewer's child nodes.
Do Until (objNode is Nothing)
' Expand the Event Viewer's child node.
objSN.Expand(objNode)
' Display text stating which node is being examined.
objTextFile.WriteLine("Error events in the " + objNode.Name + " log")
'Wscript.echo "Error events in the " + objNode.Name + " log"
objTextFile.WriteLine("=====================================")
Dim objList
Set objList = objView.ListItems
' Iterate through the list of nodes.
Dim objItem
For Each objItem In objList
Dim str
' Retrieve the data in the first column. In the Event Viewer
' snap-in, the first column is for the Event type.
str = objView.CellContents(objItem, 1)
' Determine if the event type is for an Error event.
If (str = "Error") Then
' The node is for an Error event.
' Output the details of this Error event, with
' a comma separating each data field.
dim myVariable
myVariable = objView.CellContents(objItem, 1) & "," & _
objView.CellContents(objItem, 2) & "," & _
objView.CellContents(objItem, 3) & "," & _
objView.CellContents(objItem, 4) & "," & _
objView.CellContents(objItem, 5) & "," & _
objView.CellContents(objItem, 6) & "," & _
objView.CellContents(objItem, 7) & "," & _
objView.CellContents(objItem, 8)
objTextFile.WriteLine(myVariable)
End If
Next
' Print a blank line before the next child node is processed.
'Wscript.echo ""
objTextFile.WriteLine("")
' Move to the next node under the Event Viewer node.
Set objSib = Nothing
Set objSib = objSN.GetNext(objNode)
Set objNode = objSib
Loop
objTextFile.Close