NIS_Longfellow
asked on
Run Netstat Remotely using WMI
I need to run the netstat command on a remote box and output the info to a text file. I currently have everything except how to run the command remotely. See below.
' Variable Declaration
Const ForAppending = 8 ' file mode for appending
' Error Checking
On Error Resume Next
' Variable Init
strUser = "iusr_metro" ' administrator id
strPassword = "MIOrchid#1" ' administrator password
' Injector Machine List
strMachines = "OH309APL2008.cdc.ent.nwie .net"
' Create a Text File
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
Set objTextFile = objFSO.OpenTextFile _
("c:\temp\comprept.txt", ForAppending, True)
aMachines = split(strMachines, ";")
For Each machine in aMachines
' Remote Login
Set objLocator = CreateObject( "WbemScripting.SWbemLocato r" )
Set objWMIService = objLocator.ConnectServer (machine, "root/cimv2", strUser, strPassword)
objWMIService.Security_.im personatio nlevel = 3
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=imper sonate}!\\ " & strComputer & "\root\cimv2")
objTextFile.WriteLine("### Injector Report: " & machine & " ###")
'remote netstat code here... I hope
' Writes 2 Blank Lines after each injector report.
objTextFile.WriteBlankLine s(2)
Next
' Text Log File Close
objTextFile.Close
' Variable Declaration
Const ForAppending = 8 ' file mode for appending
' Error Checking
On Error Resume Next
' Variable Init
strUser = "iusr_metro" ' administrator id
strPassword = "MIOrchid#1" ' administrator password
' Injector Machine List
strMachines = "OH309APL2008.cdc.ent.nwie
' Create a Text File
Set objFSO = CreateObject("Scripting.Fi
Set objTextFile = objFSO.OpenTextFile _
("c:\temp\comprept.txt", ForAppending, True)
aMachines = split(strMachines, ";")
For Each machine in aMachines
' Remote Login
Set objLocator = CreateObject( "WbemScripting.SWbemLocato
Set objWMIService = objLocator.ConnectServer (machine, "root/cimv2", strUser, strPassword)
objWMIService.Security_.im
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=imper
objTextFile.WriteLine("###
'remote netstat code here... I hope
' Writes 2 Blank Lines after each injector report.
objTextFile.WriteBlankLine
Next
' Text Log File Close
objTextFile.Close
ASKER
When I attempt to run the following two lines give me problems.
I get a path not found error. The temp folder does exist on the remote machine. So I'm not sure what it issue is.
Set objOutputFile = objFSO.CreateTextFile("\\" & machine & "\c$\Temp\nbtstat_batch.ba t", True)
objFSO.DeleteFile "\\" & machine & "\c$\Temp\nbtstat_batch.ba t", True
I get a path not found error. The temp folder does exist on the remote machine. So I'm not sure what it issue is.
Set objOutputFile = objFSO.CreateTextFile("\\"
objFSO.DeleteFile "\\" & machine & "\c$\Temp\nbtstat_batch.ba
ASKER
MY FULL CODE
'=============
' Variable Declaration
' Error Checking
'On Error Resume Next
' Variable Init
strUser = "iusr_metro" ' administrator id
strPassword = "MIOrchid#1" ' administrator password
' Injector Machine List
strMachines = "oh309apl2008"
' Create a Text File
Set objShell = CreateObject("WScript.Shel l")
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
strFileName = "\\localpc\c$\temp\z_compr ept.txt"
strPSExecPath = "\\localpc\c$\Documents and Settings\longfer\My Documents\LONGFER\SysInter nals\psexe c.exe"
If objFSO.FileExists(strFileN ame) = False Then
Set objOutputFile = objFSO.CreateTextFile(strF ileName, True)
objOutputFile.Close
Set objOutputFile = Nothing
End If
strFileName = objFSO.GetFile(strFileName ).ShortPat h
strPSExecPath = objFSO.GetFile(strPSExecPa th).ShortP ath
strRemoteCommands = "cmd /c netstat -a >> " & strFileName
strRemoteCommands = strRemoteCommands & VbCrLf & "echo "" >> " & strFileName
strRemoteCommands = strRemoteCommands & VbCrLf & "echo "" >> " & strFileName
strRemoteCommands = strRemoteCommands & VbCrLf & "echo "" >> " & strFileName
aMachines = split(strMachines, ";")
For Each machine in aMachines
' Remote Login
Set objOutputFile = objFSO.CreateTextFile("\\" & machine & "\c$\Temp\nbtstat_batch.ba t", True)
objOutputFile.Write strRemoteCommands
objOutputFile.Close
Set objOutputFile = Nothing
strCommand = "cmd /c " & strPSExecPath & " -accepteula -d -i -u " & strUser & " -p " & strPassword & " \\" & machine & " C:\Temp\nbtstat_batch.bat"
objShell.Run strCommand, 0, True
objFSO.DeleteFile "\\" & machine & "\c$\Temp\nbtstat_batch.ba t", True
Next
MsgBox "Done"
'=============
' Variable Declaration
' Error Checking
'On Error Resume Next
' Variable Init
strUser = "iusr_metro" ' administrator id
strPassword = "MIOrchid#1" ' administrator password
' Injector Machine List
strMachines = "oh309apl2008"
' Create a Text File
Set objShell = CreateObject("WScript.Shel
Set objFSO = CreateObject("Scripting.Fi
strFileName = "\\localpc\c$\temp\z_compr
strPSExecPath = "\\localpc\c$\Documents and Settings\longfer\My Documents\LONGFER\SysInter
If objFSO.FileExists(strFileN
Set objOutputFile = objFSO.CreateTextFile(strF
objOutputFile.Close
Set objOutputFile = Nothing
End If
strFileName = objFSO.GetFile(strFileName
strPSExecPath = objFSO.GetFile(strPSExecPa
strRemoteCommands = "cmd /c netstat -a >> " & strFileName
strRemoteCommands = strRemoteCommands & VbCrLf & "echo "" >> " & strFileName
strRemoteCommands = strRemoteCommands & VbCrLf & "echo "" >> " & strFileName
strRemoteCommands = strRemoteCommands & VbCrLf & "echo "" >> " & strFileName
aMachines = split(strMachines, ";")
For Each machine in aMachines
' Remote Login
Set objOutputFile = objFSO.CreateTextFile("\\"
objOutputFile.Write strRemoteCommands
objOutputFile.Close
Set objOutputFile = Nothing
strCommand = "cmd /c " & strPSExecPath & " -accepteula -d -i -u " & strUser & " -p " & strPassword & " \\" & machine & " C:\Temp\nbtstat_batch.bat"
objShell.Run strCommand, 0, True
objFSO.DeleteFile "\\" & machine & "\c$\Temp\nbtstat_batch.ba
Next
MsgBox "Done"
ASKER
1st Error - Line 16 "Path Not found" -
Set objOutputFile = objFSO.CreateTextFile(strF ileName, True)
psexec.exe is located on my local machine
Set objOutputFile = objFSO.CreateTextFile(strF
psexec.exe is located on my local machine
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Forced accept.
Computer101
EE Admin
Computer101
EE Admin
I have tried to get the SWBemLocator to do the job, but could not.
The only way I could get it to work was to use PSExec. Try the following script, replacing the relevant paths in it with paths of your own...
'=============
' Variable Declaration
' Error Checking
'On Error Resume Next
' Variable Init
strUser = "domain/username" ' administrator id
strPassword = "password" ' administrator password
' Injector Machine List
strMachines = "machine1;machine2"
' Create a Text File
Set objShell = CreateObject("WScript.Shel
Set objFSO = CreateObject("Scripting.Fi
strFileName = "\\localpc\c$\temp\z_compr
strPSExecPath = "\\server\share\psexec.exe
If objFSO.FileExists(strFileN
Set objOutputFile = objFSO.CreateTextFile(strF
objOutputFile.Close
Set objOutputFile = Nothing
End If
strFileName = objFSO.GetFile(strFileName
strPSExecPath = objFSO.GetFile(strPSExecPa
strRemoteCommands = "cmd /c netstat -a >> " & strFileName
strRemoteCommands = strRemoteCommands & VbCrLf & "echo "" >> " & strFileName
strRemoteCommands = strRemoteCommands & VbCrLf & "echo "" >> " & strFileName
strRemoteCommands = strRemoteCommands & VbCrLf & "echo "" >> " & strFileName
aMachines = split(strMachines, ";")
For Each machine in aMachines
' Remote Login
Set objOutputFile = objFSO.CreateTextFile("\\"
objOutputFile.Write strRemoteCommands
objOutputFile.Close
Set objOutputFile = Nothing
strCommand = "cmd /c " & strPSExecPath & " -accepteula -d -i -u " & strUser & " -p " & strPassword & " \\" & machine & " C:\Temp\nbtstat_batch.bat"
objShell.Run strCommand, 0, True
objFSO.DeleteFile "\\" & machine & "\c$\Temp\nbtstat_batch.ba
Next
MsgBox "Done"
'=============
Regards,
Rob.