بحثالمحتوياتتسجيل دخولفي الموقع حالياهناك حاليا عضو واحد و 9 ضيوف في الموقع.
الأعضاء المتصلون |
Starting and Stopping Services Using VBScript and WMIأرسل من قبل 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. The code can be written in a much better way but this will do the job. Further Reading: ( معلوماتية )
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
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. I see, thanks.
On *nix systems, shell scripting languages and commandline programs are tightly integrated, making such scripts easier to write. |
هالمدونة هي تتمة لهاد الموضوع
حبيت حطها هون مشان اذا حدا عم يبحث من خارج الموقع ممكن يوصل لهون.