I want to create a script that with help a person determine if a pc is
online or not based upon the PC name entered.

My first thought was just.....

Set WshShell = WScript.CreateObject("WScript.Shell")
Dim PcnameVariable
PcnameVariable = Trim(InputBox("Enter a pc name ","Enter a PC name"))

Wshshell.run "ping "&PcnameVariable ,0, false 'surpress the ping output
window

Then use a MsgBox to indicate if that PC in on-line or not. No need for the
user to see anything else

Any Ideas, or is there a easier way?

thanks

Re: Does Ping.exe return any values? by Richard

Richard
Wed Jan 25 13:05:48 CST 2006

Jason wrote:

>I want to create a script that with help a person determine if a pc is
>online or not based upon the PC name entered.
>
> My first thought was just.....
>
> Set WshShell = WScript.CreateObject("WScript.Shell")
> Dim PcnameVariable
> PcnameVariable = Trim(InputBox("Enter a pc name ","Enter a PC name"))
>
> Wshshell.run "ping "&PcnameVariable ,0, false 'surpress the ping output
> window
>
> Then use a MsgBox to indicate if that PC in on-line or not. No need for
> the user to see anything else
>
> Any Ideas, or is there a easier way?

Hi,

I use the following function, based on work by other MVP's:

=======================
strComputer = "RemoteMachineNetBIOSName"

' Ping computer to see if online.
If (IsConnectible(strComputer, 1, 750) = True) Then
' Computer is online.
Else
' Computer is not online.
End If

Function IsConnectible(strHost, intPings, intTO)
' Returns True if strHost can be pinged.
Dim objFile, strResults

If intPings = "" Then intPings = 2
If intTO = "" Then intTO = 750

Const OpenAsDefault = -2
Const FailIfNotExist = 0
Const ForReading = 1

objShell.Run "%comspec% /c ping -n " & intPings & " -w " & intTO _
& " " & strHost & ">" & strTempFile, 0, True

Set objFile = objFSO.OpenTextFile(strTempFile, ForReading, _
FailIfNotExist, OpenAsDefault)
strResults = objFile.ReadAll
objFile.Close

Select Case InStr(strResults, "TTL=")
Case 0
IsConnectible = False
Case Else
IsConnectible = True
End Select
End Function

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net



Thanks by Jason

Jason
Wed Jan 25 15:25:28 CST 2006

Thanks, that worked
"Jason" <jason@someone.com> wrote in message
news:OSfjwkdIGHA.1424@TK2MSFTNGP12.phx.gbl...
>I want to create a script that with help a person determine if a pc is
>online or not based upon the PC name entered.
>
> My first thought was just.....
>
> Set WshShell = WScript.CreateObject("WScript.Shell")
> Dim PcnameVariable
> PcnameVariable = Trim(InputBox("Enter a pc name ","Enter a PC name"))
>
> Wshshell.run "ping "&PcnameVariable ,0, false 'surpress the ping output
> window
>
> Then use a MsgBox to indicate if that PC in on-line or not. No need for
> the user to see anything else
>
> Any Ideas, or is there a easier way?
>
> thanks
>



Re: Thanks by Ginolard

Ginolard
Thu Jan 26 01:49:18 CST 2006

You can also use WMI and save all the messing about writing temporary
files.

If IsConnectable("xxxxxx") Then
WScript.echo "ok"
Else
WScript.Echo "Offline"
End If

Function IsConnectable(PCName)
'IsConnectible=False

Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("select ReplySize from Win32_PingStatus where address
= '" & PCName & "'")

For Each objStatus in objPing
If Not IsNull (objStatus.ReplySize) Then
IsConnectible = True
End If
Next

End Function


Re: Thanks by alexxxn

alexxxn
Thu Jan 26 04:23:31 CST 2006

Many Thanks to Ginolard !!!
but in script there is misprint in the function name

Ginolard wrote:
skip...
> Function IsConnectable(PCName)
skip...
> IsConnectible = True
skip...


Re: Thanks by Ginolard

Ginolard
Thu Jan 26 04:44:41 CST 2006

Oops, quite right. My mistake ;) This is a better version

If IsOnline("HBRUWA39A") Then
WScript.echo "ok"
Else
WScript.Echo "Offline"
End If

Function IsOnline(PCName)
'IsConnectible=False

Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("select ReplySize from Win32_PingStatus where address
= '" & PCName & "'")

For Each objStatus in objPing
If Not IsNull (objStatus.ReplySize) Then
IsOnline = True
Else
IsOnline=False
End If
Next

End Function


Re: Thanks by TheoCoolen

TheoCoolen
Thu Jan 26 10:30:03 CST 2006

You should take care that the WMI version doesnt always work see thext info
from Microsoft (this is code from the portable script center) .

There is a shell solution for nt4 and lower OS


Ping Multiple Computers

Supported Platforms

Windows Server 2003: Yes
Windows XP: Yes
Windows 2000: No
Windows NT 4.0: No
Windows 98: No

strMachines = "atl-dc-01;atl-win2k-01;atl-nt4-01;atl-dc-02"
aMachines = split(strMachines, ";")

For Each machine in aMachines
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("select * from Win32_PingStatus where address = '"_
& machine & "'")
For Each objStatus in objPing
If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then
WScript.Echo("Computer " & machine & " is not reachable")
End If
Next
Next




"Ginolard" wrote:

> Oops, quite right. My mistake ;) This is a better version
>
> If IsOnline("HBRUWA39A") Then
> WScript.echo "ok"
> Else
> WScript.Echo "Offline"
> End If
>
> Function IsOnline(PCName)
> 'IsConnectible=False
>
> Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
> ExecQuery("select ReplySize from Win32_PingStatus where address
> = '" & PCName & "'")
>
> For Each objStatus in objPing
> If Not IsNull (objStatus.ReplySize) Then
> IsOnline = True
> Else
> IsOnline=False
> End If
> Next
>
> End Function
>
>

RE: Does Ping.exe return any values? by TheoCoolen

TheoCoolen
Thu Jan 26 10:36:04 CST 2006

'Ping For an older Os


Option Explicit

Dim Test, StrComputer, IntTimeOut, IntTryout

IntTimeOut = 1000
IntTryout = 2

StrComputer = "localhost"

if F_Ping(StrComputer, IntTryout, IntTimeOut) then
Wscript.echo strcomputer & " is on line"
Else
Wscript.echo strcomputer & " is NOT on line"
End If


'===================================================================
'===== Function : F_Ping
'===== Description : Pings a computer
'===== Release : 1.0
'===== Programmer : T. Coolen (The original source is from Microsoft)
'===== Returns : 1 if respond, 0 no response
'===== CATAGORY : Universal
'===== STATUS : Tested on XP OK (this is also w2k proof)
'===================================================================
Function F_Ping(StrComputer, IntNrOfEchoRequests, IntTimeOut)

Dim ObjShell, objScriptExec
Dim StrCommand, StrPingResults
Dim IntResponse

StrCommand = "ping -n " & IntNrOfEchoRequests & " -w " & IntTimeOut & " " &
StrComputer

'wscript.echo StrCommand

Set objShell = CreateObject("WScript.Shell")
Set objScriptExec = objShell.Exec(StrCommand)

strPingResults = LCase(objScriptExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then
If InStr(strPingResults, "destination net unreachable") Then
'WScript.Echo strComputer & "did not respond to ping."
IntResponse = 0
Else
'WScript.Echo strComputer & " responded to ping."
IntResponse = 1
End If
Else
IntResponse = 0
'WScript.Echo strComputer & " did not respond to ping."
End If

F_Ping = IntResponse
End Function
'===================================================================
'===== End of function: F_Ping
'===================================================================




"Jason" wrote:

> I want to create a script that with help a person determine if a pc is
> online or not based upon the PC name entered.
>
> My first thought was just.....
>
> Set WshShell = WScript.CreateObject("WScript.Shell")
> Dim PcnameVariable
> PcnameVariable = Trim(InputBox("Enter a pc name ","Enter a PC name"))
>
> Wshshell.run "ping "&PcnameVariable ,0, false 'surpress the ping output
> window
>
> Then use a MsgBox to indicate if that PC in on-line or not. No need for the
> user to see anything else
>
> Any Ideas, or is there a easier way?
>
> thanks
>
>
>

Re: Thanks by Joe_E

Joe_E
Fri Jan 27 16:31:10 CST 2006

Only thing to bear in mind here is that the Win32_PingStatus class is
only supported on Windows XP/Server 2003 and later operating systems.
Function will fail if you target a Windows 2000 box.
See this article for more info -->
http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept04/hey0914.mspx