Solved

If/Else condition help vbscript

Posted on 2008-06-11
15
2,077 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
15 Comments
 
LVL 38

Author Comment

by:Rich Rumble
ID: 21760876
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
ID: 21764445
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
ID: 21765662
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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 65

Expert Comment

by:RobSampson
ID: 21765745
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
ID: 21765818
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
ID: 21765910
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
ID: 21765927
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 21765935
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
ID: 21766002
(41, 8) Microsoft VBScript runtime error: Invalid procedure call or argument
is the error now
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 21766013
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
ID: 21766116
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
ID: 21766156
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
ID: 21766243
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
ID: 31466185
Wonderful work, thanks for hanging in there mate!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 21766555
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

761 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