Solved

If/Else condition help vbscript

Posted on 2008-06-11
15
2,070 Views
Last Modified: 2010-04-21
I am trying to figure out why my if statement does not seem to return a result when a service is not present.
I've modified a script I found here or EE: http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_21651144.html
Below is what I have modified the script to do so far,and I'd like the script to do an additional check if the service is returned TRUE.

If the service does exist, I'd like it grab the pathname output from win32_service objItem.PathName, and use the CIM_Datafile class to retrieve the version
Set colFiles = objWMIService.ExecQuery _
 ("Select * from CIM_Datafile Where Name = 'C:\\Program Files\\Network Associates\\VirusScan\\scan32.exe'")  <--- replace this with the PathName from win32_service

And add it to the CSV output
objOutputFile.WriteLine "Computer, " & "State, " & "Started, " &  "Status," & "Version"

Right now, if mcafee isn't installed but PC is reachable, there is no output for that PC, if the pc is unreachable "strComputer unreachable" is output. Feel free to change mcafee to another service if necessary.
SubNet    = "10.1.1."

FirstNode = "55"                       '--- change to suit your needs

LastNode  = "56"                       '--- change to suit your needs

NextNode  = ""

 

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objOutputFile = objFSO.CreateTextFile("McAfee.csv")

Set oShell = Wscript.CreateObject("Wscript.Shell")

 objOutputFile.WriteLine "Computer, " & "State, " & "Started, " &  "Status"

Do While True

  ' build the next IP address

  If NextNode = "" Then

   NextNode = FirstNode

  Else

   NextNode = CStr(CInt(NextNode) + 1)

  End If

  ' check that we are still within the range of addresses we're interested in

  If CInt(NextNode) > CInt(LastNode) Then

   Exit Do

  End If

  strComputer = SubNet & NextNode

  ' check to see of the address is pingable (is online and available)

  Set oExecObj = oShell.Exec("%comspec% /c ping -a -n 1 " & strComputer)

  strText = oExecObj.StdOut.ReadAll()

  If Instr(strText, "Reply from") = 0 Then

   objOutputFile.WriteLine strComputer & ", " & "not reachable, " & "N/A, " & "N/A"

  Else

    If (strComputer <> "") Then

     On Error Resume Next

     Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}\\" & strComputer & "\root\cimv2")

     If Err.Number <> 0 Then

      objOutputFile.WriteLine strComputer & ", " & "WMI failed to connect, " & "N/A, " & "N/A"

      Err.Clear

     Else

      On Error GoTo 0

      Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service")

      blnFound = False

      For Each objItem In colItems

       If (objItem.Name = "McShield") Then blnFound = True

        If (blnFound = True) Then

        objOutputFile.Write strComputer & ", " & objItem.State & ", " & objItem.Started & ", " &  objItem.Status

        objOutputFile.WriteLine

       Else

        objOutputFile.WriteLine strComputer & ", " & "Not Installed, " & "N/A, " & "N/A"

       End If

      Next

     End If

    End If

   End If

Loop

Open in new window

0
Comment
Question by:Rich Rumble
  • 8
  • 7
15 Comments
 
LVL 38

Author Comment

by:Rich Rumble
Comment Utility
Actually the output is:
Computer, State, Started, Status
10.1.1.55, Not Installed, N/A, N/A
10.1.1.55, Not Installed, N/A, N/A
10.1.1.55, Not Installed, N/A, N/A
10.1.1.55, Not Installed, N/A, N/A
10.1.1.55, Not Installed, N/A, N/A
10.1.1.55, Not Installed, N/A, N/A
10.1.1.55, Not Installed, N/A, N/A
etc.... for each service on that PC
But if I change: Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service")
to:
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service WHERE DisplayName LIKE '%Mcshield%'")

no output is returned for that pc that does not have mcafee...
This is the code below
SubNet    = "10.1.1."

FirstNode = "55"                       '--- change to suit your needs

LastNode  = "56"                       '--- change to suit your needs

NextNode  = ""
 

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objOutputFile = objFSO.CreateTextFile("McAfee.csv")

Set oShell = Wscript.CreateObject("Wscript.Shell")

 objOutputFile.WriteLine "Computer, " & "State, " & "Started, " &  "Status"

Do While True

  ' build the next IP address

  If NextNode = "" Then

   NextNode = FirstNode

  Else

   NextNode = CStr(CInt(NextNode) + 1)

  End If

  ' check that we are still within the range of addresses we're interested in

  If CInt(NextNode) > CInt(LastNode) Then

   Exit Do

  End If

  strComputer = SubNet & NextNode

  ' check to see of the address is pingable (is online and available)

  Set oExecObj = oShell.Exec("%comspec% /c ping -a -n 1 " & strComputer)

  strText = oExecObj.StdOut.ReadAll()

  If Instr(strText, "Reply from") = 0 Then

   objOutputFile.WriteLine strComputer & ", " & "not reachable, " & "N/A, " & "N/A"

  Else

    If (strComputer <> "") Then

     On Error Resume Next

     Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}\\" & strComputer & "\root\cimv2")

     If Err.Number <> 0 Then

      objOutputFile.WriteLine strComputer & ", " & "WMI failed to connect, " & "N/A, " & "N/A"

      Err.Clear

     Else

      On Error GoTo 0

      Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service WHERE DisplayName LIKE '%Mcshield%'")

      blnFound = False

      For Each objItem In colItems

       If (objItem.Status Like "OK") Then blnFound = True

        If (blnFound = True) Then

        objOutputFile.Write strComputer & ", " & objItem.State & ", " & objItem.Started & ", " &  objItem.Status

        objOutputFile.WriteLine

       Else

        objOutputFile.WriteLine strComputer & ", " & "Not Installed, " & "N/A, " & "N/A"

       End If

      Next

     End If

    End If

   End If

Loop

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
Hi, what if you use the original script you posted, and change this line:
       If (objItem.Name = "McShield") Then blnFound = True

to this
       If InStr(LCase(objItem.Name), LCase("McShield")) > 0 Then blnFound = True


Regards,

Rob .
0
 
LVL 38

Author Comment

by:Rich Rumble
Comment Utility
It act's similar still, the output is like:
10.10.251.56       Not Installed       N/A       N/A
10.10.251.56       Not Installed       N/A       N/A
10.10.251.56       Not Installed       N/A       N/A
10.10.251.56       Paused       True       OK  <--------This is the actual mcafee service, pause to tell it from others
10.10.251.56       Running       True       OK
10.10.251.56       Stopped       False       OK
10.10.251.56       Stopped       False       OK
etc... each service seems to be returned... I've attached the script as I just ran it, to make sure I did it right. Also feel free to change the script around or whatever if you think there is a better way.
-rich
SubNet    = "10.1.1."

FirstNode = "55"                       '--- change to suit your needs

LastNode  = "56"                       '--- change to suit your needs

NextNode  = ""

 

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objOutputFile = objFSO.CreateTextFile("McAfee.csv")

Set oShell = Wscript.CreateObject("Wscript.Shell")

 objOutputFile.WriteLine "Computer, " & "State, " & "Started, " &  "Status"

Do While True

  ' build the next IP address

  If NextNode = "" Then

   NextNode = FirstNode

  Else

   NextNode = CStr(CInt(NextNode) + 1)

  End If

  ' check that we are still within the range of addresses we're interested in

  If CInt(NextNode) > CInt(LastNode) Then

   Exit Do

  End If

  strComputer = SubNet & NextNode

  ' check to see of the address is pingable (is online and available)

  Set oExecObj = oShell.Exec("%comspec% /c ping -a -n 1 " & strComputer)

  strText = oExecObj.StdOut.ReadAll()

  If Instr(strText, "Reply from") = 0 Then

   objOutputFile.WriteLine strComputer & ", " & "not reachable, " & "N/A, " & "N/A"

  Else

    If (strComputer <> "") Then

     On Error Resume Next

     Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}\\" & strComputer & "\root\cimv2")

     If Err.Number <> 0 Then

      objOutputFile.WriteLine strComputer & ", " & "WMI failed to connect, " & "N/A, " & "N/A"

      Err.Clear

     Else

      On Error GoTo 0

      Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service")

      blnFound = False

      For Each objItem In colItems

       If InStr(LCase(objItem.Name), LCase("McShield")) > 0 Then blnFound = True

        If (blnFound = True) Then

        objOutputFile.Write strComputer & ", " & objItem.State & ", " & objItem.Started & ", " &  objItem.Status

        objOutputFile.WriteLine

       Else

        objOutputFile.WriteLine strComputer & ", " & "Not Installed, " & "N/A, " & "N/A"

       End If

      Next

     End If

    End If

   End If

Loop

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
Do you actually want to report on all services, or only any McAfee services?

If you want only McAfee services, we can change:
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service")

to
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service WHERE Caption LIKE '%MCAFEE%'")

Rob.
0
 
LVL 38

Author Comment

by:Rich Rumble
Comment Utility
Correct, just McAfee, and that worked well... my second script I posted was almost correct!
I used McShield rather than mcafee, as it has 3 total services when using mcafee for the like operand, and I am only interested in one, but the output is correct now!
Computer       State       Started       Status
10.1.1.55       not reachable       N/A       N/A
10.1.1.56       Paused       True       OK

Can we add another if/else, if mcshield is found, then look at the objItem.pathname and use CIM_Data file to find the version?
Set colFiles = objWMIService.ExecQuery _
 ("Select * from CIM_Datafile Where Name = 'C:\\Program Files\\Network Associates\\VirusScan\\scan32.exe'")  <--- replace this with the PathName from win32_service

here is what all the items for mcshield return:
AcceptPause: True
AcceptStop: True
Caption: McAfee McShield
CheckPoint: 0
CreationClassName: Win32_Service
Description: Provides McAfee On-Access scanning protection of your computer system.
DesktopInteract: False
DisplayName: McAfee McShield
ErrorControl: Normal
ExitCode: 0
Name: McShield
PathName: "C:\Program Files\McAfee\VirusScan Enterprise\Mcshield.exe"  <---- This
ProcessId: 1832
ServiceSpecificExitCode: 0
ServiceType: Own Process
Started: True
StartMode: Auto
StartName: LocalSystem
State: Paused
Status: OK
SystemCreationClassName: Win32_ComputerSystem
SystemName: SEC-LT
TagId: 0
WaitHint: 0

Thanks a million!
-rich
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
Should be able to....instead of using CIM_DataFile, we'll try just the GetFileVersion method of the objFSO object.....

Regards,

Rob.
SubNet    = "10.1.1."

FirstNode = "55"                       '--- change to suit your needs

LastNode  = "56"                       '--- change to suit your needs

NextNode  = ""

 

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objOutputFile = objFSO.CreateTextFile("McAfee.csv")

Set oShell = Wscript.CreateObject("Wscript.Shell")

 objOutputFile.WriteLine "Computer, " & "State, " & "Started, " &  "Status, " & "Version"

Do While True

  ' build the next IP address

  If NextNode = "" Then

   NextNode = FirstNode

  Else

   NextNode = CStr(CInt(NextNode) + 1)

  End If

  ' check that we are still within the range of addresses we're interested in

  If CInt(NextNode) > CInt(LastNode) Then

   Exit Do

  End If

  strComputer = SubNet & NextNode

  ' check to see of the address is pingable (is online and available)

  Set oExecObj = oShell.Exec("%comspec% /c ping -a -n 1 " & strComputer)

  strText = oExecObj.StdOut.ReadAll()

  If Instr(strText, "Reply from") = 0 Then

   objOutputFile.WriteLine strComputer & ", " & "not reachable, " & "N/A, " & "N/A, " & "N/A"

  Else

    If (strComputer <> "") Then

     On Error Resume Next

     Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}\\" & strComputer & "\root\cimv2")

     If Err.Number <> 0 Then

      objOutputFile.WriteLine strComputer & ", " & "WMI failed to connect, " & "N/A, " & "N/A, " & "N/A"

      Err.Clear

     Else

      On Error GoTo 0

      Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service WHERE Caption LIKE '%mcshield%'")

      blnFound = False

      For Each objItem In colItems

       blnFound = True

       strPathName = objItem.PathName

      Next

      If (blnFound = True) Then

        objOutputFile.Write strComputer & ", " & objItem.State & ", " & objItem.Started & ", " &  objItem.Status & ", " & objFSO.GetFileVersion(strPath)

        objOutputFile.WriteLine

      Else

        objOutputFile.WriteLine strComputer & ", " & "Not Installed, " & "N/A, " & "N/A, " & "N/A"

      End If

     End If

    End If

   End If

Loop

Open in new window

0
 
LVL 38

Author Comment

by:Rich Rumble
Comment Utility
error'd
(43, 9) Microsoft VBScript runtime error: Object required: 'objItem'
objOutputFile.Write strComputer & ", " & objItem.State & ", " & objItem.Started & ", " &  objItem.Status & ", " & objFSO.GetFileVersion(strPath)

I found that after the "next" it seems to error when I try things like this with this script... I really don't know any programming, the scriptomatic does most of my work for me, or wmic :)
-rich
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
Oh that was my fault, I moved the Next statement....but got it wrong....try this.

Rob.
SubNet    = "10.1.1."

FirstNode = "55"                       '--- change to suit your needs

LastNode  = "56"                       '--- change to suit your needs

NextNode  = ""

 

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objOutputFile = objFSO.CreateTextFile("McAfee.csv")

Set oShell = Wscript.CreateObject("Wscript.Shell")

 objOutputFile.WriteLine "Computer, " & "State, " & "Started, " &  "Status, " & "Version"

Do While True

  ' build the next IP address

  If NextNode = "" Then

   NextNode = FirstNode

  Else

   NextNode = CStr(CInt(NextNode) + 1)

  End If

  ' check that we are still within the range of addresses we're interested in

  If CInt(NextNode) > CInt(LastNode) Then

   Exit Do

  End If

  strComputer = SubNet & NextNode

  ' check to see of the address is pingable (is online and available)

  Set oExecObj = oShell.Exec("%comspec% /c ping -a -n 1 " & strComputer)

  strText = oExecObj.StdOut.ReadAll()

  If Instr(strText, "Reply from") = 0 Then

   objOutputFile.WriteLine strComputer & ", " & "not reachable, " & "N/A, " & "N/A, " & "N/A"

  Else

    If (strComputer <> "") Then

     On Error Resume Next

     Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}\\" & strComputer & "\root\cimv2")

     If Err.Number <> 0 Then

      objOutputFile.WriteLine strComputer & ", " & "WMI failed to connect, " & "N/A, " & "N/A, " & "N/A"

      Err.Clear

     Else

      On Error GoTo 0

      Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service WHERE Caption LIKE '%mcshield%'")

      blnFound = False

      For Each objItem In colItems

       blnFound = True

       strPathName = objItem.PathName

       objOutputFile.Write strComputer & ", " & objItem.State & ", " & objItem.Started & ", " &  objItem.Status & ", " & objFSO.GetFileVersion(strPath)

       objOutputFile.WriteLine

      Next

      If blnFound = False Then

        objOutputFile.WriteLine strComputer & ", " & "Not Installed, " & "N/A, " & "N/A, " & "N/A"

      End If

     End If

    End If

   End If

Loop

Open in new window

0
 
LVL 38

Author Comment

by:Rich Rumble
Comment Utility
(41, 8) Microsoft VBScript runtime error: Invalid procedure call or argument
is the error now
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
Oh bugger!

Please change this line
       objOutputFile.Write strComputer & ", " & objItem.State & ", " & objItem.Started & ", " &  objItem.Status & ", " & objFSO.GetFileVersion(strPath)

to this
       objOutputFile.Write strComputer & ", " & objItem.State & ", " & objItem.Started & ", " &  objItem.Status & ", " & objFSO.GetFileVersion(strPathName)

strPath should have been strPathName

Rob.
0
 
LVL 38

Author Comment

by:Rich Rumble
Comment Utility
No error, but no version is output.
Computer       State       Started       Status       Version
10.1.1.55       not reachable       N/A       N/A       N/A
10.1.1.56       Paused       True       OK       
thanks again for keeping at it!
-rich
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
Comment Utility
Ah, OK, the path name had quotes around it when pulled from Win32_Service....

Rob.
SubNet    = "10.1.1."

FirstNode = "55"                       '--- change to suit your needs

LastNode  = "56"                       '--- change to suit your needs

NextNode  = ""

 

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objOutputFile = objFSO.CreateTextFile("McAfee.csv")

Set oShell = Wscript.CreateObject("Wscript.Shell")

 objOutputFile.WriteLine "Computer, " & "State, " & "Started, " &  "Status, " & "Version"

Do While True

  ' build the next IP address

  If NextNode = "" Then

   NextNode = FirstNode

  Else

   NextNode = CStr(CInt(NextNode) + 1)

  End If

  ' check that we are still within the range of addresses we're interested in

  If CInt(NextNode) > CInt(LastNode) Then

   Exit Do

  End If

  strComputer = SubNet & NextNode

  ' check to see of the address is pingable (is online and available)

  Set oExecObj = oShell.Exec("%comspec% /c ping -a -n 1 " & strComputer)

  strText = oExecObj.StdOut.ReadAll()

  If Instr(strText, "Reply from") = 0 Then

   objOutputFile.WriteLine strComputer & ", " & "not reachable, " & "N/A, " & "N/A, " & "N/A"

  Else

    If (strComputer <> "") Then

     On Error Resume Next

     Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}\\" & strComputer & "\root\cimv2")

     If Err.Number <> 0 Then

      objOutputFile.WriteLine strComputer & ", " & "WMI failed to connect, " & "N/A, " & "N/A, " & "N/A"

      Err.Clear

     Else

      On Error GoTo 0

      Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service WHERE Caption LIKE '%mcshield%'")

      blnFound = False

      For Each objItem In colItems

       blnFound = True

       strPathName = Replace(objItem.PathName, """", "")

       objOutputFile.Write strComputer & ", " & objItem.State & ", " & objItem.Started & ", " &  objItem.Status & ", " & objFSO.GetFileVersion(strPathName)

       objOutputFile.WriteLine

      Next

      If blnFound = False Then

        objOutputFile.WriteLine strComputer & ", " & "Not Installed, " & "N/A, " & "N/A, " & "N/A"

      End If

     End If

    End If

   End If

Loop

Open in new window

0
 
LVL 38

Author Comment

by:Rich Rumble
Comment Utility
I couldn't ask for more! thanks so much! This is perfect!
Computer       State       Started       Status       Version
10.1.1.55       not reachable       N/A       N/A       N/A
10.1.1.56       Paused       True       OK       13.3.1.100

-rich
0
 
LVL 38

Author Closing Comment

by:Rich Rumble
Comment Utility
Wonderful work, thanks for hanging in there mate!
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
Great. Thanks for the grade.....and for the record, I've seen through solutions more than 100 posts long!  But they all balance out, because I've also done plenty of one liners!

Rob.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now