Solved

How to check a service is running then install an executable

Posted on 2011-02-22
3
417 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
[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
  • 2
3 Comments
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
ID: 34956858
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
ID: 34959175
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
ID: 34962756
Yes, that would be correct.
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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Suggested Courses
Course of the Month11 days, 15 hours left to enroll

623 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