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"

Re: DNS and WINS update by Pegasus

Pegasus
Tue Apr 22 16:28:55 CDT 2008


"JayJ" <JayJ@discussions.microsoft.com> wrote in message
news:F489A951-3741-48DC-BCD3-4142C329B1EC@microsoft.com...
>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)"

Before you can use objShell.Exec(strCommand), you must create it:

Set objShell = CreateObject("WScript.Shell")




Re: DNS and WINS update by JayJ

JayJ
Tue Apr 22 21:47:00 CDT 2008

Thank you. I thought I had already done that. Looked at it a long time and
missed the obvious.

It now will run again, but only if all servers in the list are valid and
pingable. It still isn't actually using the ping test, outputting that it
failed (for an offline server) and moving on to the next server. Can you help
with that problem? Again, I appreciate your help. Maybe this isn't as obvious
as my first question?

Thank you. Copying middle of script again.

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


"Pegasus (MVP)" wrote:

>
> "JayJ" <JayJ@discussions.microsoft.com> wrote in message
> news:F489A951-3741-48DC-BCD3-4142C329B1EC@microsoft.com...
> >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)"
>
> Before you can use objShell.Exec(strCommand), you must create it:
>
> Set objShell = CreateObject("WScript.Shell")
>
>
>
>

Re: DNS and WINS update by Pegasus

Pegasus
Wed Apr 23 03:49:27 CDT 2008

The line
Set objExecObject = objShell.Exec(strCommand)
makes no sense: objShell.exec generates a return code, not an
object. Try this:
intRC = objShell.Exec(strCommand)

"JayJ" <JayJ@discussions.microsoft.com> wrote in message
news:FFA94089-389C-4CE0-AF8A-B37B979656DB@microsoft.com...
> Thank you. I thought I had already done that. Looked at it a long time and
> missed the obvious.
>
> It now will run again, but only if all servers in the list are valid and
> pingable. It still isn't actually using the ping test, outputting that it
> failed (for an offline server) and moving on to the next server. Can you
> help
> with that problem? Again, I appreciate your help. Maybe this isn't as
> obvious
> as my first question?
>
> Thank you. Copying middle of script again.
>
> 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
>
>
> "Pegasus (MVP)" wrote:
>
>>
>> "JayJ" <JayJ@discussions.microsoft.com> wrote in message
>> news:F489A951-3741-48DC-BCD3-4142C329B1EC@microsoft.com...
>> >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)"
>>
>> Before you can use objShell.Exec(strCommand), you must create it:
>>
>> Set objShell = CreateObject("WScript.Shell")
>>
>>
>>
>>



Re: DNS and WINS update by ekkehard

ekkehard
Wed Apr 23 07:32:46 CDT 2008

Pegasus (MVP) schrieb:
> The line
> Set objExecObject = objShell.Exec(strCommand)
> makes no sense: objShell.exec generates a return code, not an
> object. Try this:
> intRC = objShell.Exec(strCommand)
[...]
From the Docs:

The Exec method returns a WshScriptExec object, which provides status
and error information about a script run with Exec along with access
to the StdIn, StdOut, and StdErr channels.

I think you thought of .Run.

Re: DNS and WINS update by Pegasus

Pegasus
Wed Apr 23 08:13:09 CDT 2008


"ekkehard.horner" <ekkehard.horner@arcor.de> wrote in message
news:480f2c6f$0$6517$9b4e6d93@newsspool4.arcor-online.net...
> Pegasus (MVP) schrieb:
>> The line
>> Set objExecObject = objShell.Exec(strCommand)
>> makes no sense: objShell.exec generates a return code, not an
>> object. Try this:
>> intRC = objShell.Exec(strCommand)
> [...]
> From the Docs:
>
> The Exec method returns a WshScriptExec object, which provides status
> and error information about a script run with Exec along with access
> to the StdIn, StdOut, and StdErr channels.
>
> I think you thought of .Run.

It's a little sad when other people need to tell me what they think
I was thinking, and even sadder when they are spot on . . . :-)

Back to the OP's code: This code fragment works very nicely on
my machine:

Set objShell = CreateObject("WScript.Shell")
strComputer = "TestPC"
If (strComputer <> "") Then
strCommand ="%comspec% /c ping -n 3 -w 1000 " & strComputer
Set objExecObject = objShell.Exec(strCommand)
strText = objExecObject.StdOut.ReadAll()
WScript.Echo strText
end if

If it does not work on your machine then you should insert
some strategic wscript.echo statements to see how far it gets.

If this was my code then I would make it more robust by coding
If InStr(uCase(strText), "REPLY") > 0 Then instead of
If InStr(strText, "Reply") > 0 Then



Re: DNS and WINS update by JayJ

JayJ
Sat Apr 26 09:47:00 CDT 2008



"Pegasus (MVP)" wrote:

>
> "ekkehard.horner" <ekkehard.horner@arcor.de> wrote in message
> news:480f2c6f$0$6517$9b4e6d93@newsspool4.arcor-online.net...
> > Pegasus (MVP) schrieb:
> >> The line
> >> Set objExecObject = objShell.Exec(strCommand)
> >> makes no sense: objShell.exec generates a return code, not an
> >> object. Try this:
> >> intRC = objShell.Exec(strCommand)
> > [...]
> > From the Docs:
> >
> > The Exec method returns a WshScriptExec object, which provides status
> > and error information about a script run with Exec along with access
> > to the StdIn, StdOut, and StdErr channels.
> >
> > I think you thought of .Run.
>
> It's a little sad when other people need to tell me what they think
> I was thinking, and even sadder when they are spot on . . . :-)
>
> Back to the OP's code: This code fragment works very nicely on
> my machine:
>
> Set objShell = CreateObject("WScript.Shell")
> strComputer = "TestPC"
> If (strComputer <> "") Then
> strCommand ="%comspec% /c ping -n 3 -w 1000 " & strComputer
> Set objExecObject = objShell.Exec(strCommand)
> strText = objExecObject.StdOut.ReadAll()
> WScript.Echo strText
> end if
>
> If it does not work on your machine then you should insert
> some strategic wscript.echo statements to see how far it gets.
>
> If this was my code then I would make it more robust by coding
> If InStr(uCase(strText), "REPLY") > 0 Then instead of
> If InStr(strText, "Reply") > 0 Then
>
>
Thank you both. Script works now.