I am writing a script to update WINS and DNS entries on all servers in a
file. I also want to output to an Excel file the success or failure. If a
server is not pingable i want to output that to the spreadsheet and move on
to the next server in the text file.
This script works when i don't test to see if the server is pingable first.
It will hang though if a server is not online. When i add the ping test it
fails with an "Object required: 'objShell' " error in this line "Set
objExecObject = objShell.Exec(strCommand)"
see script below. Would appreciate any help in fixing the error in this
script. Thank you.
'On Error Resume Next
Const ForReading = 1
Const serversFilename = "Servers.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(serversFilename, ForReading)
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.Workbooks.Add
Set objWb = objExcel.ActiveWorkbook
x = 2
objExcel.Cells(1, 1) = "Server Name"
objExcel.Cells(1, 2) = "DNS UPDATED"
Do Until objTextFile.AtEndOfStream
strComputer = objTextFile.ReadLine
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNetCards = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
If (strComputer <> "") Then
strCommand ="%comspec% /c ping -n 3 -w 1000 " & strComputer
Set objExecObject = objShell.Exec(strCommand)
strText = objExecObject.StdOut.ReadAll()
If Instr(strText, "Reply") > 0 Then
For Each objNetCard in colNetCards
strPrimaryServer = "192.168.2.100"
strSecondaryServer = "192.168.2.200"
objNetCard.SetWINSServer strPrimaryServer, strSecondaryServer
arrDNSServers = Array("192.168.1.100", "192.168.1.200")
objNetCard.SetDNSServerSearchOrder(arrDNSServers)
objExcel.Cells(x, 1) = strComputer
objExcel.Cells(x, 2) = "Complete"
x = x + 1
Next
Else
objExcel.Cells(x, 1) = strComputer
objExcel.Cells(x, 2) = "Server Not reachable"
x = x + 1
End if
End if
loop
objTextFile.Close
objWb.SaveAs "DNS.xls"