Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connect to remote servers

Status
Not open for further replies.

snufse2

Programmer
Nov 8, 2010
2
US
I currently have a vb script that is using MMC and retrieves events (Application, security etc) from my local desktop. The script is working fine.

Now, I need to connect to multiple servers and run the same script on each one (there will be a list if servers in either a table or text file). The script itself will reside in a central location only.

Is this possible and if so does someone have some links or code snippet that will help me get started? Thank you.
 
I currently have a vb script
Which code ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
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

 
[Q.0] Can you clarify whether vgiwpw03-sql3 is the remote m/c? And the path to eventvwr.msc on the remote m/c you may need? And the path to MMC EVent Viewer Remote Server.vbs? etc... The path is fairly important as you don't have current directory set and the search may go to windows\system32 where the cmd to run the bat file is located.

[Q.1] In the meantime, what is the errReturn being returned?

[Q.2] Also why does it need to make a pass through .bat? You may well directly execute the cscript?

[Q.3] And then, what is the "central" location? which executable is hosted there? Is it different from the local m/c where the (wmi) script is executed?

[Q.4] The user running the wmi script and got impersonated, does he belong to the local admin group of the remote?...

The picture is not very clear.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top