Starting and Stopping Services Using VBScript and WMI

صورة foaad
أرسل من قبل foaad في الثلاثاء, 2006/03/14 - 5:37am.

Recently I've been asked to write a script file to stop some services and another one to start them back. I've never used VBScript before but it's fairly simple and straight forward. Of course VBScript doesn't provide any methods to manage services or any other Windows resources; for this kind of management you need to use WMI (Microsoft® Windows® Management Instrumentation) and in this case WMI scripting library.
I tried to keep the script as simple as possible. And I've encapsulated the VBScript code inside a WSF file:

<job>
   <script language="VBScript">
    Function ControlService(ServiceName, Start)
        Set objSWbemServices = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
        Set colSWbemObjectSet = objSWbemServices.InstancesOf("Win32_Service")
        For Each objSWbemObject In colSWbemObjectSet
            If objSWbemObject.Name = ServiceName Then
                If Start Then
                    objSWbemObject.StartService
                Else
                    objSWbemObject.StopService
                End If
            End If
        Next
    End Function
    
    'This line will stop the service
    ControlService "ServiceName", False
    
    'This line will start the service
    'ControlService "ServiceName", True
   </script>
</job>

Write the previous code inside a .wsf file and replace "ServiceName" with the name (not Display Name) of the service you want to stop, care to use the correct casing because the comparison here is case sensitive.
If you want to start the service comment out the 19th line and uncomment the 22th line.
If you want to use a .vbs file omit the first and last two lines.

The code can be written in a much better way but this will do the job.

Further Reading:
WMI Scripting Primer: Part 1 and Part 2


خيارات عرض التعليقات

اختر طريقتك المفضلة لعرض التعليقات و اضغط "حفظ الإعدادات" لتفعيل تغييراتك.
صورة foaad
أرسل من قبل foaad في الثلاثاء, 2006/03/14 - 5:44am.

هالمدونة هي تتمة لهاد الموضوع
حبيت حطها هون مشان اذا حدا عم يبحث من خارج الموقع ممكن يوصل لهون.


صورة أيمن
أرسل من قبل أيمن في الأربعاء, 2006/03/15 - 6:48pm.

Thanks for the effort, but why does it have to be so much complicated? Under many *nix systems it's simply:

 /etc/init.d/service stop
/etc/init.d/service start


صورة foaad
أرسل من قبل foaad في الخميس, 2006/03/16 - 12:22am.

Actually it doesn't have to be complicated.
In Windows there is a command line program called SC

USAGE:
        sc <server> [command] [service name] <option1> <option2>...
EXAMPLE:
        sc start MyService

But the WMI scripting library is useful in case you don't want to use an external program (sc.exe) or -for some reason- sc.exe is missing or not working, and it provides more sophisticated features than the sc command line program.


صورة أيمن
أرسل من قبل أيمن في الخميس, 2006/03/16 - 1:46pm.

I see, thanks.

On *nix systems, shell scripting languages and commandline programs are tightly integrated, making such scripts easier to write.