Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Boolean Problem

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,59525
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Boolean Problem
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Boolean Problem - 4/25/2008 2:53:49 PM   
  mwebb

 

Posts: 35
Score: 0
Joined: 1/30/2007
Status: offline
I am trying to use a Boolean statement to test connections using "ping"

When I tested the following script it worked fine:
-----------------------------------------
'Ping Script
Dim PINGFlag
strComputer = "dc01"
Set WshShell = CreateObject("WScript.Shell")
If CheckConnection(strComputer) = True Then
WScript.Echo strComputer & " is connected" & PINGflag
Else
WScript.Echo strComputer & " is not connected" & PINGflag

End if
WScript.Echo "Done!!"
WScript.Quit

Function CheckConnection(strComputer)
PINGFlag = Not CBool(WshShell.run("ping -n 1 " & strComputer, 0, True))
IF PINGFlag =  True Then
 WScript.Echo "Successful Ping" & "  " & PINGFlag
Else
 WScript.Echo "Unsuccessful Ping" & "  " & PINGFlag
End If
CheckConnection = PINGFlag
End Function

---------------------------------------------------------------
But when I put it a larger program I cannot get a boolean PINGFlag value

Below I tried it both as a function and in the main program with the same results no True or False value for PINGFlag
I am stumped why is works in one place but not another

The problem area has a puzzled face.
-----------------------------------------
'Processor and memory query.
'This script queries each computer in the domain for info on the processor and
'physical memory on the computer. The results are written to a text file.
Option Explicit
On Error Resume Next
'Setup variables
Dim objComputer
Dim objItem
Dim strComputerName(5)
Dim strNewName
Dim strPassName
Dim strNewPassName
Dim intCounter
Dim intLength
Dim objWMI
Dim colItems
Dim objItem2
Dim strProcessor
Dim strMemory
Dim strComputer
Dim strTextF, strTextFile
Dim objFSO
Dim objTextF
Dim intI
Dim intMemory
Dim intMaxSpeed
Dim strProcName
Dim PINGFlag

'Get computer names from AD and place into a str array
intCounter = 0
Set objComputer = GetObject("LDAP://cn=Computers,dc=colfalls,dc=loc")
WScript.Echo "Storing Names in Array"
For Each objItem in objComputer

'WScript.Echo objItem.Name
strNewName = StringStuff(objItem.Name)
strComputerName(intCounter) = strNewName
'WScript.Echo "name from array  " & strComputerName(intCounter)
intCounter = intCounter + 1

Next
ReDim strComputerName(intCounter)

strTextFile = "C:\MemProcInfo2.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
For intI = 0 to (intCounter-1)
strComputer = strComputerName(intI)
WScript.Echo "Check Connection response: "
PINGFlag = Not CBool(WshShell.Run("ping -n 1" & StrComputer, 0, True))
WScript.Echo PINGflag
If PINGFlag = False Then
WScript.Echo "not connected: " & strComputer & PINGflag
intMemory = 0
intMaxSpeed = 0
strProcName = "no connection"
QueryToText
Else
Wscript.Echo "connected " & strComputer & PINGflag
Set objWMI = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & strComputer & "\root\cimv2")
'WScript.Echo strComputer
Set colItems = objWMI.ExecQuery("Select * From Win32_ComputerSystem",,48)
For Each objItem2 in colItems
intMemory = objItem2.TotalPhysicalMemory
'WScript.Echo intMemory

Next
Set colItems = objWMI.ExecQuery("Select * From Win32_Processor",,48)
For Each objItem2 in colItems
intMaxSpeed = objItem2.MaxClockSpeed
strProcName = objItem2.Name
'WScript.Echo intMaxSpeed & " " & strProcName

Next
QueryToText
End If
Next

WScript.Echo "Done!!!"
WScript.Quit

'This funtion strips out "cn=" from the computername and returns the result
Function StringStuff(strPassName)
'WScript.Echo "in sub  " & strPassName
intLength = len(strPassName)
intLength = intLength -3
strNewPassName = right(strPassName, intLength)
'WScript.Echo "new name " & strNewPassName
StringStuff = strNewPassName
End Function

'This sub writes the Info a text file
sub QueryToText
Set objTextF = objFSO.OpenTextFile(strTextFile, 8, True)
objTextF.WriteLine strComputer
objTextF.WriteLine "Total Physical Memory: " & intMemory
objTextF.WriteLine "Processor Max Speed: " & intMaxSpeed
objTextF.WriteLine "Processor Nomenclature: " & strProcName
objTextF.WriteLine
objTextF.Close
end Sub
'This function pings strComputer and returns a false if the ping was unsuccessful
'Function CheckConnection(strComputer)
'PINGFlag = Not CBool(WshShell.Run("ping -n 1" & strComputer, 0, True))
'WScript.Echo "in CheckConnection"
'WScript.Echo "PINGFlag: " & PINGflag
'CheckConnection = PINGFlag
'End Function

 
 
Post #: 1
 
 RE: Boolean Problem - 4/27/2008 3:51:00 AM   
  mcnd

 

Posts: 23
Score: 0
Joined: 4/27/2008
Status: offline
Try this

----------------------------------------------------------
Dim PINGFlag
   strComputer = "dc01"
   Set WshShell = CreateObject("WScript.Shell")
   PINGFlag = CheckConnection(strComputer)
   if PINGFlag then
       WScript.Echo strComputer + " is connected" + CStr( PINGflag )
   Else
       WScript.Echo strComputer + " is not connected" + CStr( PINGflag )
   End if

   WScript.Echo "Done!!"
   WScript.Quit

Function CheckConnection(strComputer)
   Checkconnection = Not CBool(WshShell.run("ping -n 1 " & strComputer, 0, True))
   IF Checkconnection =  True Then
        WScript.Echo "Successful Ping" & "  " & CStr( PINGFlag )
   Else
        WScript.Echo "Unsuccessful Ping" & "  " & CStr( PINGFlag )
   End If
End Function

-------------------------------------------------------------------------------

Implicit conversions from boolean to string does not always return what you expect

(in reply to mwebb)
 
 
Post #: 2
 
 RE: Boolean Problem - 4/27/2008 6:09:07 AM   
  mwebb

 

Posts: 35
Score: 0
Joined: 1/30/2007
Status: offline
Thanks.

That helps

(in reply to mcnd)
 
 
Post #: 3
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Boolean Problem Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts