Stopping Windows Management Instrumentation with ServiceController

G

Guest

I use the following method to stop the WMI service:

string serviceName = "Windows Management Instrumentation"
public static void stopService(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController svc in services)
{
if (svc.DisplayName == serviceName)
{
try
{
svc.Stop();
}
catch (System.ServiceProcess.TimeoutException ex)
{
Console.WriteLine(ex.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("The {0} process was stopped.",
serviceName);
}

}
}

According to Event Viewer the system is sent a stop control to WMI, and then
the service stops. A second or so later the WMI service restarts for no
apparent reason. Any advice on how to keep the WMI service stopped would be
greatly appreciated. I am new to C# so if you see any problems with my
method or can suggest a better way to do this please let me know. Thanks in
advance.
 
W

Willy Denoyette [MVP]

Primera said:
I use the following method to stop the WMI service:

string serviceName = "Windows Management Instrumentation"
public static void stopService(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController svc in services)
{
if (svc.DisplayName == serviceName)
{
try
{
svc.Stop();
}
catch (System.ServiceProcess.TimeoutException ex)
{
Console.WriteLine(ex.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("The {0} process was stopped.",
serviceName);
}

}
}

According to Event Viewer the system is sent a stop control to WMI, and
then
the service stops. A second or so later the WMI service restarts for no
apparent reason. Any advice on how to keep the WMI service stopped would
be
greatly appreciated. I am new to C# so if you see any problems with my
method or can suggest a better way to do this please let me know. Thanks
in
advance.

As you can stop a service, other applications and services can start a
service, right?
WMI is s system service you should not stop it unless you realy have good
reasons to do so. If you don't want WMI to run at all disable the service,
but note that I said it's a "system service" that means other services and
applications may depend on it.

Willy.
 
P

Primera

Yes...I agree that WMI is definitely a required service. I have to stop
it briefly so that I can delete all contents of the Repository folder. Then
when WMI is restarted the correct Repository data is regenerated. We have
experienced a unique problem where WMI becomes corrupted and the SMS Client
app will not work properly. The fix is to stop the WMI service, delete the
data in Repository, restart the WMI service, remove/clean any current SMS
Client, and reinstall the SMS Client. So...the purpose of this app is to
automate that procedure. Would you happen to have any advice on how to accomplish
this? All advice and help is greatly appreciated.



Hello Willy Denoyette [MVP],
I use the following method to stop the WMI service:

string serviceName = "Windows Management Instrumentation"
public static void stopService(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController svc in services)
{
if (svc.DisplayName == serviceName)
{
try
{
svc.Stop();
}
catch (System.ServiceProcess.TimeoutException ex)
{
Console.WriteLine(ex.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("The {0} process was stopped.",
serviceName);
}
}
}
According to Event Viewer the system is sent a stop control to WMI,
and
then
the service stops. A second or so later the WMI service restarts for
no
apparent reason. Any advice on how to keep the WMI service stopped
would
be
greatly appreciated. I am new to C# so if you see any problems with
my
method or can suggest a better way to do this please let me know.
Thanks
in
advance.
As you can stop a service, other applications and services can start a
service, right?
WMI is s system service you should not stop it unless you realy have
good
reasons to do so. If you don't want WMI to run at all disable the
service,
but note that I said it's a "system service" that means other services
and
applications may depend on it.
Willy.
 
W

Willy Denoyette [MVP]

Only "work-around" I see is to disable WMI before you stop it, else SMS will
restart it.
Note that the only real solution to this problem is to find out who's
corrupting the repository.
Also note that you should not write anything yourself to do this, the
command line utility sc.exe is the tool for this.

Willy.

Primera said:
Yes...I agree that WMI is definitely a required service. I have to stop
it briefly so that I can delete all contents of the Repository folder.
Then when WMI is restarted the correct Repository data is regenerated. We
have experienced a unique problem where WMI becomes corrupted and the SMS
Client app will not work properly. The fix is to stop the WMI service,
delete the data in Repository, restart the WMI service, remove/clean any
current SMS Client, and reinstall the SMS Client. So...the purpose of
this app is to automate that procedure. Would you happen to have any
advice on how to accomplish this? All advice and help is greatly
appreciated.



Hello Willy Denoyette [MVP],
I use the following method to stop the WMI service:

string serviceName = "Windows Management Instrumentation"
public static void stopService(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController svc in services)
{
if (svc.DisplayName == serviceName)
{
try
{
svc.Stop();
}
catch (System.ServiceProcess.TimeoutException ex)
{
Console.WriteLine(ex.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("The {0} process was stopped.",
serviceName);
}
}
}
According to Event Viewer the system is sent a stop control to WMI,
and
then
the service stops. A second or so later the WMI service restarts for
no
apparent reason. Any advice on how to keep the WMI service stopped
would
be
greatly appreciated. I am new to C# so if you see any problems with
my
method or can suggest a better way to do this please let me know.
Thanks
in
advance.
As you can stop a service, other applications and services can start a
service, right?
WMI is s system service you should not stop it unless you realy have
good
reasons to do so. If you don't want WMI to run at all disable the
service,
but note that I said it's a "system service" that means other services
and
applications may depend on it.
Willy.
 
A

Adam Stocks

Well, this is a rather rare occurrence, and I certainly agree about finding
the corrupting item. I have not been able to identify what has corrupted
WMI, but after taking these steps we have never had a recurrence of the issue
on the same machine. Could you offer any advice on how to programmatically
disable WMI so that I could keep the service stopped long enough to delete
these items? Thanks for your help.
Adam

Hello Willy Denoyette [MVP],
Only "work-around" I see is to disable WMI before you stop it, else
SMS will
restart it.
Note that the only real solution to this problem is to find out who's
corrupting the repository.
Also note that you should not write anything yourself to do this, the
command line utility sc.exe is the tool for this.
Willy.

Yes...I agree that WMI is definitely a required service. I have to
stop it briefly so that I can delete all contents of the Repository
folder. Then when WMI is restarted the correct Repository data is
regenerated. We have experienced a unique problem where WMI becomes
corrupted and the SMS Client app will not work properly. The fix is
to stop the WMI service, delete the data in Repository, restart the
WMI service, remove/clean any current SMS Client, and reinstall the
SMS Client. So...the purpose of this app is to automate that
procedure. Would you happen to have any advice on how to accomplish
this? All advice and help is greatly appreciated.

Hello Willy Denoyette [MVP],
I use the following method to stop the WMI service:

string serviceName = "Windows Management Instrumentation"
public static void stopService(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController svc in services)
{
if (svc.DisplayName == serviceName)
{
try
{
svc.Stop();
}
catch (System.ServiceProcess.TimeoutException ex)
{
Console.WriteLine(ex.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("The {0} process was stopped.",
serviceName);
}
}
}
According to Event Viewer the system is sent a stop control to WMI,
and
then
the service stops. A second or so later the WMI service restarts
for
no
apparent reason. Any advice on how to keep the WMI service stopped
would
be
greatly appreciated. I am new to C# so if you see any problems
with
my
method or can suggest a better way to do this please let me know.
Thanks
in
advance.
As you can stop a service, other applications and services can start
a
service, right?
WMI is s system service you should not stop it unless you realy have
good
reasons to do so. If you don't want WMI to run at all disable the
service,
but note that I said it's a "system service" that means other
services
and
applications may depend on it.
Willy.
 
W

Willy Denoyette [MVP]

Well the only correct way to keep your repository in good shape is by
applying a back-up strategy. For instance you could create a job object that
is run by the scheduler every day at xx, the job can be a simple command
line script that creates a back-up of the repository by issuing the "winmgmt
/backup..." command. Writes to the repository are blocked during back-up,
reads are handled though, clients keep their session if any, only thing that
can happen is a timeout when issuing a write (create/modify object) action,
this should be handled by the application anyway.
Whenever something happens with the repository you can restore the latest
back-up by means of the "winmgmt /restore..." command. Reads and writes from
clients are blocked during restore, but clients aren't disconected.

Note that these commands can also be issued through the
System.Diagnostics.Start method.

Look-up the MSDN docs for details on the winmgmt command line switches.

Willy.



Adam Stocks said:
Well, this is a rather rare occurrence, and I certainly agree about
finding the corrupting item. I have not been able to identify what has
corrupted WMI, but after taking these steps we have never had a recurrence
of the issue on the same machine. Could you offer any advice on how to
programmatically disable WMI so that I could keep the service stopped long
enough to delete these items? Thanks for your help.
Adam

Hello Willy Denoyette [MVP],
Only "work-around" I see is to disable WMI before you stop it, else
SMS will
restart it.
Note that the only real solution to this problem is to find out who's
corrupting the repository.
Also note that you should not write anything yourself to do this, the
command line utility sc.exe is the tool for this.
Willy.

Yes...I agree that WMI is definitely a required service. I have to
stop it briefly so that I can delete all contents of the Repository
folder. Then when WMI is restarted the correct Repository data is
regenerated. We have experienced a unique problem where WMI becomes
corrupted and the SMS Client app will not work properly. The fix is
to stop the WMI service, delete the data in Repository, restart the
WMI service, remove/clean any current SMS Client, and reinstall the
SMS Client. So...the purpose of this app is to automate that
procedure. Would you happen to have any advice on how to accomplish
this? All advice and help is greatly appreciated.

Hello Willy Denoyette [MVP],


I use the following method to stop the WMI service:

string serviceName = "Windows Management Instrumentation"
public static void stopService(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController svc in services)
{
if (svc.DisplayName == serviceName)
{
try
{
svc.Stop();
}
catch (System.ServiceProcess.TimeoutException ex)
{
Console.WriteLine(ex.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("The {0} process was stopped.",
serviceName);
}
}
}
According to Event Viewer the system is sent a stop control to WMI,
and
then
the service stops. A second or so later the WMI service restarts
for
no
apparent reason. Any advice on how to keep the WMI service stopped
would
be
greatly appreciated. I am new to C# so if you see any problems
with
my
method or can suggest a better way to do this please let me know.
Thanks
in
advance.
As you can stop a service, other applications and services can start
a
service, right?
WMI is s system service you should not stop it unless you realy have
good
reasons to do so. If you don't want WMI to run at all disable the
service,
but note that I said it's a "system service" that means other
services
and
applications may depend on it.
Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top