Disable service _vbs with VBS implementation

Source: Internet
Author: User
Ask:
Hello, Scripting Guy! How do I disable a service?
--DS
For:
Hello, DS. Did you know that the Scripting Guys seem to have stumbled upon a good idea. In the last installment, we've covered how to disable LMHosts files. Today, we will show you how to disable the service. If we go on like this, we'll soon be able to introduce you to all of us on how to disable the computer. Imagine: No more network problems, no more customer service center calls, no more users overwriting files that should not be overwritten. All we have to do is disable everything, and all these problems will disappear! This could be our ticket to a happy time.
It is undeniable that disabling everything on your computer may cause other problems in your organization. But it's just an internal matter that you have to deal with.
In addition, we may be busy counting money to help you.
Of course, we still have to write this column before the money starts to roll, and we still have to pay the bill. With this in mind, we provide the following script to disable the Alerter service on the computer:
Copy Code code as follows:

strcomputer =  "."  
Set objwmiservice = getobject ("winmgmts:\\"  & strComputer &   "\root\cimv2")  
set colservicelist = objwmiservice.execquery _ 
     ("select * from win32_service where name = " Alerter ' ")  
for each objservice in colservicelist 
     If objService.State =  "Running"  Then 
         objservice.stopservice ()  
        wscript.sleep  5000 
    End If 
    errreturncode =  objservice.changestartmode ("Disabled")     
next 
As you can see, disabling a service does not require a lot of code, and a small piece of code that is required is very basic code. However, there is a tricky part, which is why we will take a few minutes to introduce you step-by-step to this script.
In addition, Rolls Royce distribution rights will not be open for an hour.
The script starts with nothing fancy: we just connect to the WMI service on the local computer (although we can also disable the services on the remote computer). We then issue the following query, which returns a collection that contains only one item: the Alerter service:
Set Colservicelist = objWMIService.ExecQuery _
("SELECT * FROM Win32_Service where Name = ' Alerter '")
By the way, make sure you include the WHERE clause as shown above. Why? Imagine, suppose you omit the Where clause. In this case, your collection will contain all the services installed on your computer, and your script will dutifully attempt to disable each service.
You're right: this is usually not a good thing. In addition, the Scripting Guys may sue you; after all, disabling everything on the computer is our idea!
After we get the collection, we set up a For Each loop to iterate through all the items within the collection. (Yes, we know: There is only one item in the collection.) But we still need to use the For Each loop. Below is the tricky part. You can disable a running service, but the service is not actually disabled until it stops running. If you disable a running service, the service will continue to run as usual, and the change will not take effect until the service is stopped.
Because of this, we use the following line of code to see if the Alerter service is running:
If objservice.state = "Running" Then
If the service is running, then we call the StopService () method to stop the service. Then we pause for 5 seconds (5,000 milliseconds), and the service has time to really stop until we continue with the operation to disable it. That's what we do here:
Objservice.stopservice ()
Wscript.Sleep 5000
Note: We did something to make the sample script as short as possible. Let's assume that your service is in the "Run" or "stop" state only. In fact, the service may also contain other states, most notably "pause" or "continue." To write a more secure script, you might also want to check these states (and take the appropriate action). However, because the service is in the running or stopping state in 99.9% of cases, you can usually use the script we give you to fudge the past.
Once the service has stopped, we can disable it using the following line of code:
Errreturncode = Objservice.changestartmode ("Disabled")
As you can see, we just use the ChangeStartMode method to change the value of the StartMode property. We want to disable the service, so we pass the parameter "Disabled" to ChangeStartMode. We can also set the StartMode to "Manual" or "Automatic", which will set the StartMode to either manual or automatic.
Remember that when a service is stopped and disabled, it cannot be restarted; the only way to restart the service is to re-enable it (by changing the StartMode to "manual" or "Automatic"). However, we will wait until we convince everyone to disable everything on their computers before we explain how to re-enable them. After all, we might be able to double the cost of these scripts.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.