• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2101
  • Last Modified:

If/Else condition help vbscript

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
Rich Rumble
Asked:
Rich Rumble
  • 8
  • 7
1 Solution
 
Rich RumbleSecurity SamuraiAuthor Commented:
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
 
RobSampsonCommented:
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
 
Rich RumbleSecurity SamuraiAuthor Commented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RobSampsonCommented:
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
 
Rich RumbleSecurity SamuraiAuthor Commented:
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
 
RobSampsonCommented:
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
 
Rich RumbleSecurity SamuraiAuthor Commented:
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
 
RobSampsonCommented:
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
 
Rich RumbleSecurity SamuraiAuthor Commented:
(41, 8) Microsoft VBScript runtime error: Invalid procedure call or argument
is the error now
0
 
RobSampsonCommented:
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
 
Rich RumbleSecurity SamuraiAuthor Commented:
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
 
RobSampsonCommented:
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
 
Rich RumbleSecurity SamuraiAuthor Commented:
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
 
Rich RumbleSecurity SamuraiAuthor Commented:
Wonderful work, thanks for hanging in there mate!
0
 
RobSampsonCommented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now