bsharath
asked on
Ping all machines and results in different excel sheets
Hi,
I want a way that when i run the script it pings each machine in the Domain and get the data ta a sheet in excel and rename the excel to the todats date.When i run the next day it has to crete the data in another new sheet.Is there a way to do this.
Regards
Sharath
I want a way that when i run the script it pings each machine in the Domain and get the data ta a sheet in excel and rename the excel to the todats date.When i run the next day it has to crete the data in another new sheet.Is there a way to do this.
Regards
Sharath
>You willl need to use VBA to do this.
I believe that Sharath was hoping you would provide the VBA.
I believe that Sharath was hoping you would provide the VBA.
you could use some code like:
Dim ws1 as worksheet
Sub NewSheet()
Sheets.Add
ActiveSheet.Copy After:=Sheets(ActiveWorkbo ok.Sheets. Count)
ActiveSheet.Name = Format(Now(), "dd-mmm-yyyy")
Set ws1 = activesheet
Call YOURPINGMACRO
End Sub
And use ws1 as sheet in your PingMacro
regards,
Jeroen
Dim ws1 as worksheet
Sub NewSheet()
Sheets.Add
ActiveSheet.Copy After:=Sheets(ActiveWorkbo
ActiveSheet.Name = Format(Now(), "dd-mmm-yyyy")
Set ws1 = activesheet
Call YOURPINGMACRO
End Sub
And use ws1 as sheet in your PingMacro
regards,
Jeroen
here is a simple one. OP please adapt/amend
Option Explicit
Dim objPing,objFile,objFSO,obj Excel,objS heet,PING
Dim myOutFile,query,Row,Col,Pi ngMachines (2),i,resp onse
Col = 1
Row = 1
PingMachines(0)="127.0.0.1 "
PingMachines(1)="134.0.0.1 "
Set objFSO=CreateObject("Scrip ting.FileS ystemObjec t")
Set objExcel = CreateObject("Excel.Applic ation")
objExcel.Workbooks.Add
Set objSheet = objExcel.ActiveWorkbook.Wo rksheets(1 )
objExcel.Columns(1).Column Width = 16
objExcel.Columns(2).Column Width = 20
objSheet.Cells(1,Col).Valu e = "Server" 'Contain IP address
objSheet.Cells(1,Col+1).Va lue = "Date" ''Date Time
objSheet.Cells(1,Col+2).Va lue = "Response" 'Response of the ping
Row=Row+1
For i=LBound(PingMachines) To UBound(PingMachines) -1
query="select * from Win32_PingStatus where address ='"& PingMachines(i) & "'"
Set objPing = GetObject("winmgmts:{imper sonationLe vel=impers onate}").E xecQuery(q uery)
For Each PING In objPing
Select Case PING.StatusCode
Case 0
response="Reply from " & PING.ProtocolAddress
Case 11002
response="Destination Net Unreachable"
Case 11003
response="Destination Net Unreachable"
Case 11010
response="Request Timed Out"
End Select
objSheet.Cells(Row,1).Valu e=PingMach ines(i)
objSheet.Cells(Row,2).Valu e=Now
objSheet.Cells(Row,3).Valu e=response
Row=Row+1
Next
Next
objExcel.ActiveWorkbook.Sa veAs("c:\t emp\test.x ls")
objExcel.ActiveWorkbook.Cl ose
objExcel.Application.Quit
Option Explicit
Dim objPing,objFile,objFSO,obj
Dim myOutFile,query,Row,Col,Pi
Col = 1
Row = 1
PingMachines(0)="127.0.0.1
PingMachines(1)="134.0.0.1
Set objFSO=CreateObject("Scrip
Set objExcel = CreateObject("Excel.Applic
objExcel.Workbooks.Add
Set objSheet = objExcel.ActiveWorkbook.Wo
objExcel.Columns(1).Column
objExcel.Columns(2).Column
objSheet.Cells(1,Col).Valu
objSheet.Cells(1,Col+1).Va
objSheet.Cells(1,Col+2).Va
Row=Row+1
For i=LBound(PingMachines) To UBound(PingMachines) -1
query="select * from Win32_PingStatus where address ='"& PingMachines(i) & "'"
Set objPing = GetObject("winmgmts:{imper
For Each PING In objPing
Select Case PING.StatusCode
Case 0
response="Reply from " & PING.ProtocolAddress
Case 11002
response="Destination Net Unreachable"
Case 11003
response="Destination Net Unreachable"
Case 11010
response="Request Timed Out"
End Select
objSheet.Cells(Row,1).Valu
objSheet.Cells(Row,2).Valu
objSheet.Cells(Row,3).Valu
Row=Row+1
Next
Next
objExcel.ActiveWorkbook.Sa
objExcel.ActiveWorkbook.Cl
objExcel.Application.Quit
ASKER
Roos01
Can i use ghostdog1 code in excel and use your code.
I am a bit confused on which code to use...
Can i use ghostdog1 code in excel and use your code.
I am a bit confused on which code to use...
ASKER
ghostdog74
Can the script take the ip's or machine names from a txt file and change the sheetname to today's date...
So when i repeat the ping it has to create a new sheet and change date to today's date...
Can the script take the ip's or machine names from a txt file and change the sheetname to today's date...
So when i repeat the ping it has to create a new sheet and change date to today's date...
Hello bsharath,
I asumed you already had the script for pinging machines on the domain.
The script I gave is creating a new worksheet every day.
You might try this one, where a sheet called: PC Names, contains all the PC names you want to track.
Dim ws1, ws2 As Worksheet
Dim i As Long
Sub NewSheet()
Set ws2 = Sheets("PC Names") 'this sheetc ontains all PC names
RowsA = ws2.Cells(Rows.Count, 1).End(xlUp).Row
Sheets.Add
ActiveSheet.Copy After:=Sheets(ActiveWorkbo ok.Sheets. Count)
ActiveSheet.Name = Format(Now(), "dd-mmm-yyyy")
Set ws1 = ActiveSheet
For i = 2 To RowsA
If Cells(i, 1) <> "" Then
Call MYPINGMACRO(Cells(i, 1))
End If
End Sub
Sub MYPINGMACRO(strComputer)
'http://msdn2.microsoft.com/en-us/library/aa394595.aspx
'strComputer = Cells(i, 2)
ws1.Cells(i, 1) = strComputer
'"ping -n 1 ComputerName >> C:\results.txt".
Set objShell = CreateObject("WScript.Shel l")
Set objScriptExec = objShell.Exec( _
"ping -n 2 -w 1000 " & strComputer)
strPingResults = LCase(objScriptExec.StdOut .ReadAll)
If InStr(strPingResults, "reply from") Then
If InStr(strPingResults, "destination net unreachable") Then
ws1.Cells(i, 2) = strComputer & "did not respond to ping."
Else
ws1.Cells(i, 2) = strComputer & " responded to ping."
End If
Else
ws1.Cells(i, 2) = strComputer & " did not respond to ping."
End If
End Sub
regards,
Jeroen
I asumed you already had the script for pinging machines on the domain.
The script I gave is creating a new worksheet every day.
You might try this one, where a sheet called: PC Names, contains all the PC names you want to track.
Dim ws1, ws2 As Worksheet
Dim i As Long
Sub NewSheet()
Set ws2 = Sheets("PC Names") 'this sheetc ontains all PC names
RowsA = ws2.Cells(Rows.Count, 1).End(xlUp).Row
Sheets.Add
ActiveSheet.Copy After:=Sheets(ActiveWorkbo
ActiveSheet.Name = Format(Now(), "dd-mmm-yyyy")
Set ws1 = ActiveSheet
For i = 2 To RowsA
If Cells(i, 1) <> "" Then
Call MYPINGMACRO(Cells(i, 1))
End If
End Sub
Sub MYPINGMACRO(strComputer)
'http://msdn2.microsoft.com/en-us/library/aa394595.aspx
'strComputer = Cells(i, 2)
ws1.Cells(i, 1) = strComputer
'"ping -n 1 ComputerName >> C:\results.txt".
Set objShell = CreateObject("WScript.Shel
Set objScriptExec = objShell.Exec( _
"ping -n 2 -w 1000 " & strComputer)
strPingResults = LCase(objScriptExec.StdOut
If InStr(strPingResults, "reply from") Then
If InStr(strPingResults, "destination net unreachable") Then
ws1.Cells(i, 2) = strComputer & "did not respond to ping."
Else
ws1.Cells(i, 2) = strComputer & " responded to ping."
End If
Else
ws1.Cells(i, 2) = strComputer & " did not respond to ping."
End If
End Sub
regards,
Jeroen
ASKER
Hi,
Is this a Macro or vbs script...
Is this a Macro or vbs script...
This is a macro which should be used in a new module in excel.
ASKER
I have put the whole code in a new Module and named the sheet (PC Names)
Where should i put the machine names?
Or should i create a file name (PC Names) and run the script...
Should the whole script be in a single Module?
Where should i put the machine names?
Or should i create a file name (PC Names) and run the script...
Should the whole script be in a single Module?
The machine names should be stored in column A of the sheet called PC Names
the script can be p[laced in one module within the VBA editor.
to run the script while reading a textfile need more coding and changes on errors.
If it is easy to add the machinenames in excel then I would prefer that approach.
Jeroen
the script can be p[laced in one module within the VBA editor.
to run the script while reading a textfile need more coding and changes on errors.
If it is easy to add the machinenames in excel then I would prefer that approach.
Jeroen
ASKER
I get a Compile error.
End Sub
To just reconfirm....Is all this 1 code .I need to put this in 1 module...
Start code......................
Dim ws1, ws2 As Worksheet
Dim i As Long
Sub NewSheet()
Set ws2 = Sheets("PC Names") 'this sheetc ontains all PC names
RowsA = ws2.Cells(Rows.Count, 1).End(xlUp).Row
Sheets.Add
ActiveSheet.Copy After:=Sheets(ActiveWorkbo ok.Sheets. Count)
ActiveSheet.Name = Format(Now(), "dd-mmm-yyyy")
Set ws1 = ActiveSheet
For i = 2 To RowsA
If Cells(i, 1) <> "" Then
Call MYPINGMACRO(Cells(i, 1))
End If
End Sub
Sub MYPINGMACRO(strComputer)
'http://msdn2.microsoft.com/en-us/library/aa394595.aspx
'strComputer = Cells(i, 2)
ws1.Cells(i, 1) = strComputer
'"ping -n 1 ComputerName >> C:\results.txt".
Set objShell = CreateObject("WScript.Shel l")
Set objScriptExec = objShell.Exec( _
"ping -n 2 -w 1000 " & strComputer)
strPingResults = LCase(objScriptExec.StdOut .ReadAll)
If InStr(strPingResults, "reply from") Then
If InStr(strPingResults, "destination net unreachable") Then
ws1.Cells(i, 2) = strComputer & "did not respond to ping."
Else
ws1.Cells(i, 2) = strComputer & " responded to ping."
End If
Else
ws1.Cells(i, 2) = strComputer & " did not respond to ping."
End If
End Sub
End Code......................
End Sub
To just reconfirm....Is all this 1 code .I need to put this in 1 module...
Start code......................
Dim ws1, ws2 As Worksheet
Dim i As Long
Sub NewSheet()
Set ws2 = Sheets("PC Names") 'this sheetc ontains all PC names
RowsA = ws2.Cells(Rows.Count, 1).End(xlUp).Row
Sheets.Add
ActiveSheet.Copy After:=Sheets(ActiveWorkbo
ActiveSheet.Name = Format(Now(), "dd-mmm-yyyy")
Set ws1 = ActiveSheet
For i = 2 To RowsA
If Cells(i, 1) <> "" Then
Call MYPINGMACRO(Cells(i, 1))
End If
End Sub
Sub MYPINGMACRO(strComputer)
'http://msdn2.microsoft.com/en-us/library/aa394595.aspx
'strComputer = Cells(i, 2)
ws1.Cells(i, 1) = strComputer
'"ping -n 1 ComputerName >> C:\results.txt".
Set objShell = CreateObject("WScript.Shel
Set objScriptExec = objShell.Exec( _
"ping -n 2 -w 1000 " & strComputer)
strPingResults = LCase(objScriptExec.StdOut
If InStr(strPingResults, "reply from") Then
If InStr(strPingResults, "destination net unreachable") Then
ws1.Cells(i, 2) = strComputer & "did not respond to ping."
Else
ws1.Cells(i, 2) = strComputer & " responded to ping."
End If
Else
ws1.Cells(i, 2) = strComputer & " did not respond to ping."
End If
End Sub
End Code......................
I noticed that I forgot the Next statement:
Dim ws1, ws2 As Worksheet
Dim i As Long
Sub NewSheet()
Set ws2 = Sheets("PC Names") 'this sheetc ontains all PC names
RowsA = ws2.Cells(Rows.Count, 1).End(xlUp).Row
Sheets.Add
ActiveSheet.Copy After:=Sheets(ActiveWorkbo ok.Sheets. Count)
ActiveSheet.Name = Format(Now(), "dd-mmm-yyyy")
Set ws1 = ActiveSheet
For i = 2 To RowsA
If Cells(i, 1) <> "" Then
Call MYPINGMACRO(Cells(i, 1))
End If
Next i 'Forgot about this statement
End Sub
Dim ws1, ws2 As Worksheet
Dim i As Long
Sub NewSheet()
Set ws2 = Sheets("PC Names") 'this sheetc ontains all PC names
RowsA = ws2.Cells(Rows.Count, 1).End(xlUp).Row
Sheets.Add
ActiveSheet.Copy After:=Sheets(ActiveWorkbo
ActiveSheet.Name = Format(Now(), "dd-mmm-yyyy")
Set ws1 = ActiveSheet
For i = 2 To RowsA
If Cells(i, 1) <> "" Then
Call MYPINGMACRO(Cells(i, 1))
End If
Next i 'Forgot about this statement
End Sub
ASKER
Thanks now it dont get any error.It creates the sheet with todays date but no results in there.
In the sheet Pcname i have some machine names.When i run the Macro it just creates the sheet but no data in it.
Can you even give the time.As if i run the macro mor than once it says duplicate sheet name available.
In the sheet Pcname i have some machine names.When i run the Macro it just creates the sheet but no data in it.
Can you even give the time.As if i run the macro mor than once it says duplicate sheet name available.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Stephen