BatchPatch Custom Script Integration – Install Windows Updates Only After Stopping a Specified Service

Today we’re going to look at another example of how you can integrate a custom script into BatchPatch to create an effect that you could not accomplish with the built-in actions alone.

The Goal

Using a combination of the Job Queue, a Deployment, and a custom script, instruct the target computer(s) to only install Windows Updates after successfully stopping a running service. If the target computer fails to stop the service, don’t install the Windows Updates.

Summary:

Use the BatchPatch Job Queue to execute the following steps:

  1. Deploy a script to target computers that returns 0 if the specified service stops successfully.
  2. Use the Job Queue feature ‘Terminate queue if previous action fails/errors’
  3. Execute ‘Download and install updates + reboot if required’ (or any desired action)

How to do it:

  1. I’ve created a very simple vb script that stops a specified service. If the script is successful it returns 0, otherwise it returns a non-0 value. The contents of my script are below:
    'Stops the specified service and returns 0 if successful else returns non-0
    'Cocobolo Software LLC February 2018.
    
    'Usage: cscript.exe "C:\Your Script Repository\StopService.vbs" "Your service display name goes here"
    
    'The first argument from the command line is assigned to strServiceDisplayName
    strServiceDisplayName = WScript.Arguments(0)
     
    on error resume next
    Err.Clear
     
    Set objWMIService = GetObject("winmgmts:\\localhost\root\cimv2")
     
    Set colServices = objWMIService.ExecQuery("Select * from Win32_Service where DisplayName='" & strServiceDisplayName & "'")
    	For Each objService in colServices
    		ReturnValue = objService.StopService()
    			wscript.quit(ReturnValue)			
    	Next
  2. Save the script. The contents of the script above need to be saved in a text file with a .vbs file extension. For the sake of this example my script is called “StopService.vbs”
  3. Create a deployment. The deployment will be used to copy the vbscript to the target computers, execute it, and retrieve the exit code. To create your deployment select ‘Actions > Deploy > Create / modify.’
  4. Browse to the location of your StopService.vbs file, and then give the deployment a title. Click the ‘>>’ button to save the deployment. The screenshot below shows the configured deployment. Note, the DisplayName of the desired service to be stopped is in the ‘Parameters’ field in quotes.
  5. With your deployment created and saved you can now setup your Job Queue. Go to ‘Actions > Job Queue > Create / modify.
  6. Select the desired steps of the queue. The first step executes the deployment that we created earlier. The second step tells BatchPatch to halt the queue if the previous action fails/errors (a script is considered failed/errored if it returns any non-zero value). The third and final step of the script is to execute whatever action is desired such as ‘Download and install updates.’ The screenshot below shows what your queue should look like:
  7. All we have to do now is execute the queue. Click ‘Execute now’ (or alternatively save the queue first and then execute it directly from the BatchPatch Job Queue menu). When the queue executes, the target computer will first attempt to stop the ‘DNS Client’ service. If successful, it will then install Windows Updates. If unsuccessful then the queue will terminate without installing updates. By the way, there is no good reason that you would ever want or need to stop the ‘DNS Client’ service before installing updates. I only used this particular service in this example. You will, of course, specify the service that you desire to stop.
  8. Notes:

    What if you want to start a service instead of stop a service? In your vb script you can use

    ReturnValue = objService.StartService()

    instead of

    ReturnValue = objService.StopService()

    What if you want to change the start mode of the service from Automatic to Manual?

    ReturnValue = objService.ChangeStartMode("manual")

    instead of

    ReturnValue = objService.StopService()
This entry was posted in Blog, General, Tutorials and tagged , , . Bookmark the permalink. Both comments and trackbacks are currently closed.