Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Question (I Think)

Status
Not open for further replies.

ScarEye

MIS
Oct 22, 2003
33
0
0
US
Hey guys I gotta quick question hopefully someone will have the answer to this. I have this VB Script all it does is displays memory information.

strComputer = "192.168.1.1"

Set wbemServices = GetObject("winmgmts:\\" & strComputer)
Set wbemObjectSet = wbemServices.InstancesOf("Win32_LogicalMemoryConfiguration")

For Each wbemObject In wbemObjectSet
WScript.Echo "Total Physical Memory (kb): " & wbemObject.TotalPhysicalMemory
Next


Now what I would like to do is before it runs the script. I would like it to first ping the IP 192.168.1.1 (strComputer) if it's alive then continue. But if it's dead then display message No Connection.

Any help is appreciated.


Thanks
ScarEye
 
ScarEye,

You can use a simple function (I posted in the past or everybody can easily come up with similar) call to determine whether the url is alive/available or not at some moment.
Code:
surl="[URL unfurl="true"]www.google.com"[/URL]
'surl="localhost"
'surl="127.0.0.1"
wscript.echo urlAlive(surl)

function urlAlive(surl)
    'return true (-1) or false (0)
    signature="TTL"
    set wshshell=createobject("wscript.shell")
    set oexec=wshshell.exec("ping " & surl)
    do while oexec.status=0 : wscript.sleep 100 : loop
    soutput=oexec.stdout.readall
    if instr(1, soutput, signature, 1)<>0 then
        'wscript.echo "The url is alive."
        urlalive=true
    else
        'wscript.echo "The url is unavailable or unknown; or the response is blocked."
        urlalive=false
    end if
    set oexec=nothing : set wshshell=nothing
end function
(For xp/2003 wmi has built in ping facitlity, but one needs to have latest m/c!)

regards - tsuji
 
Thanks for you help. But tsuji, I should have also stated that I am a TOTAL n00b at this. So how can I put what you gave me to what I have listed up above.


Thanks for you help

And don't worry I am learning..



Thanks
 
Oh btw. I don't need a returned value. So if the ip is alive then go this code:

strComputer = "192.168.1.1"

Set wbemServices = GetObject("winmgmts:\\" & strComputer)
Set wbemObjectSet = wbemServices.InstancesOf("Win32_LogicalMemoryConfiguration")

For Each wbemObject In wbemObjectSet
WScript.Echo "Total Physical Memory (kb): " & wbemObject.TotalPhysicalMemory
Next

If ip is dead then display. IP Not Avalabile ( Or something like that )

Thanks you once again.
 
ScarEye,

You can do it like this.
Code:
strComputer = "192.168.1.1"
if urlAlive(strComputer) then
    Set wbemServices = GetObject("winmgmts:\\" & strComputer)
    Set wbemObjectSet = wbemServices.InstancesOf("Win32_LogicalMemoryConfiguration")

    For Each wbemObject In wbemObjectSet
        WScript.Echo "Total Physical Memory (kb): " & wbemObject.TotalPhysicalMemory
    Next
else
    WScript.Echo "IP " & strComputer & " Is Not Avalabile"
end if
- tsuji
 
I am getting an error

Type mismatch 'urlAlive'
 
I do not. What is your script?
- tsuji
 
I just took what you have up above, copied it pasted it in notepad put my IP in save it as a .vbs file. Double click on it and I get that message.

the last code is

end if



^ Is that correct, This is what my code looks like:

strComputer = "192.168.1.2"
if urlAlive(strComputer) then
Set wbemServices = GetObject("winmgmts:\\" & strComputer)
Set wbemObjectSet = wbemServices.InstancesOf("Win32_LogicalMemoryConfiguration")

For Each wbemObject In wbemObjectSet
WScript.Echo "Total Physical Memory (kb): " & wbemObject.TotalPhysicalMemory
Next
else
WScript.Echo "IP" & strComputer & "Is Not Avalabile"
end if



Thanks for your quick responses =)



 
ScarEye,

You have to paste the function urlAlive() down there, the code is posted earlier.

- tsuji
 
tsuji,


lol, I told you I was a n00b. So I got the function in there and now I see a quick dos window come up and disappear and it displays my memeory. GREAT...


BUT I put a bogus ip in strComputer and I DO NOT get this message

wscript.echo "The url is unavailable or unknown; or the response is blocked."



This is what my code looks like now.




strComputer = "192.168.1.25"
if urlAlive(strComptuer) then
Set wbemServices = GetObject("winmgmts:\\" & strComputer)
Set wbemObjectSet = wbemServices.InstancesOf("Win32_LogicalMemoryConfiguration")

For Each wbemObject In wbemObjectSet
WScript.Echo "Total Physical Memory (kb): " & wbemObject.TotalPhysicalMemory
Next
else
WScript.Echo "IP " & strComputer & " Is Not Avalabile"
end if
function urlAlive(strComputer)
'return true (-1) or false (0)
signature="TTL"
set wshshell=createobject("wscript.shell")
set oexec=wshshell.exec("ping " & surl)
do while oexec.status=0 : wscript.sleep 100 : loop
soutput=oexec.stdout.readall
if instr(1, soutput, signature, 1)<>0 then
'wscript.echo "The url is alive."
urlalive=true
else
'wscript.echo "The url is unavailable or unknown; or the response is blocked."
urlalive=false
end if
set oexec=nothing : set wshshell=nothing
end function


Once again thanks for your quick responses.

OH btw did you try putting in a bogus IP and see what the results were ?


Thank you once again.


 
btw, I just tried changing this

set oexec=wshshell.exec("ping " & surl)


to this

set oexec=wshshell.exec("ping " & strComputer)


Still no luck...

 
You do not put a live url/site which is not within your domain for getting their memory data or do you? The site is alive, but you have no permission to do anything with wmi. Consequently, wmi ends up in runtime error after minutes of struggling which is the default timeout. And, it is the reason why you ping the other end in the first place. Brief, it is understood you do not feed the script with whatever url, but url within your domain.

- tsuji
 
YES, Exactly that I understand. I know about domains and permissions.

I just don't know VBS =(


But I am learning..


Yes the ip is within the same domain.
 
Everybody is learning... who is not?

But, the way you change the in-parameter within the function shows you have some groundwork to cover.

- tsuji
 
So, I am feeding the script a url that is within my domain but the machine is turned off, because I would like to see a message saying that url is dead from the script, so that when I actually try to run it, IT doesn't crap out on me.


Thanks
ScarEye
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top