Disable services with vbs

Source: Internet
Author: User

Q:
Hello, script expert! How do I disable a service?
-- DS
A:
Hello, DS. You know, the scripting experts seem to have accidentally discovered a good idea. In the previous column, we introduced how to disable the LMHosts file. Today, we will introduce how to disable the service. If we continue this way, we will soon be able to introduce how to disable everything on the computer. Imagine that there are no network problems, no call from the customer service center, and no user overwrites files that should not be overwritten. All we have to do is disable everything and all these problems will disappear! This is our admission ticket to happy time.
It is undeniable that disabling everything on the computer may cause other problems in your organization. But this is only the internal transaction that you need to handle.
In addition, we may be busy counting money and cannot help you.
Of course, we still have to write this column before the money starts to roll in, and we still have to pay for it. Because of this, we provide a script to disable the Alerter Service on the computer below: Copy codeThe Code is as follows: strComputer = "."
Set ob1_miservice = GetObject ("winmgmts: \" & strComputer & "\ root \ cimv2 ")
Set colServiceList = obw.miservice. 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 short piece of code is a very basic code. However, there is a tricky part, which is why we will spend a few minutes gradually introducing this script to you.
In addition, no matter how Rolls royalty is granted, it will not be open for one more hour.
There is nothing strange at the beginning of the script: We just connect to the WMI Service on the Local Computer (although we can also disable the service on the remote computer ). Then, we issue the following query, which returns a set containing only one item: Alerter Service:
Set colServiceList = obw.miservice. ExecQuery _
("Select * from Win32_Service where Name = 'alerter '")
By the way, make sure that the Where clause shown above is included. Why? Suppose you have omitted the Where clause. In this case, your collection will contain all services installed on your computer, so your script will try to disable each service with due diligence.
You are right: this is usually not a good thing. In addition, script experts may accuse you; after all, it is our idea to disable everything on the computer!
After getting the set, we create a For Each loop to traverse all the items in the set. (Yes, we know that there is only one item in the collection. But we still need to use the For Each loop .) The following are the tricky parts. You can disable a running service. However, the service is not actually disabled before it is stopped. If you disable a running service, the service continues to run. The change takes effect only after the service is stopped.
Because of this, we use the following line of code to check whether the Alerter service is running:
If objService. State = "Running" Then
If the service is running, call the StopService () method to stop the service. Then we pause for 5 seconds (5,000 ms), so that the service has time to actually stop before we continue to disable it. We did this as follows:
ObjService. StopService ()
Wscript. Sleep 5000
Note: We have made some effort to make the sample script as short as possible. Assume that your service is only in the "running" or "STOPPED" status. In fact, the service may also contain other statuses, most notably "paused" or "continue ". To write a more secure script, you may also need to check the status (and take appropriate measures ). However, In 99.9%, services are both running or stopped, so you can use the script we provided to bypass them.
After the service is stopped, we can use the following line of code to disable it:
ErrReturnCode = objService. ChangeStartMode ("Disabled ")
As you can see, we only use the ChangeStartMode method to change the value of the StartMode attribute. We want to disable this service, so we pass the parameter "Disabled" to ChangeStartMode. We can also set StartMode to "Manual" or "Automatic"; this will set StartMode to "Manual" or "Automatic ".
Remember that the service cannot be restarted after it is stopped and disabled. The only way to restart the service is to re-enable it (by changing StartMode to manual or automatic "). However, we will not explain how to re-enable these things until we persuade everyone to disable everything on their computers. After all, we may be able to double the charges for 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.