Solved

How to check a service is running then install an executable

Posted on 2011-02-22
3
412 Views
Last Modified: 2012-05-11
Hi this is my first question here and I'm new to VBScript as well, so apologies before hand.

I'm trying to build a VBS logon script that will check to see if a particular service is running (e.g wuauserv), then based on the result true/false either continue with the rest of the script or install an .exe file.
Ideally I would like a message box to be displayed informing the user that the exe is installing and it will take a few minutes to complete.

Currently this is done with a batch similar to the below

       ECHO.
      ECHO Update
      ECHO.
      ECHO Installing so and so  this might take a few minutes

      SC query wuauserv | find "RUNNING"
      ECHO.%ERRORLEVEL%
      IF NOT ERRORLEVEL=1 goto service_OK
      "%logonserver%\netlogon\logon\wsus.exe"
      :Service_OK



OS is Windows XP and 7

Any help will be much appreciated.
0
Comment
Question by:Peanutter571
  • 2
3 Comments
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
Comment Utility
Hi, Peanetter571.

This should do it.
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select State from Win32_Service WHERE Name='wuauserv'",,48)
For Each objItem in colItems
    If objItem.State = "Running" Then
        WScript.Echo "Installing executable.  This may take a few minutes."
        'Your installation code goes here'
    End If
Next

Open in new window

0
 

Author Comment

by:Peanutter571
Comment Utility
Great thanks very much BlueDevilFan, would I be right in thinking then if I changed the code to

 If NOT objItem.State = "Running" Then
        WScript.Echo "Installing executable.  This may take a few minutes."
        'Your installation code goes here'
    End If

Open in new window


This would look to see if the service state was not running i.e not installed ?
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
Yes, that would be correct.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

728 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