i am trying to shutdown a remote computer on my network from a c# windows service. When i run the code below in a test app it works fine but when its run from the service nothing happens.
I have a feeling it may have something to do with permissions but not sure.
Does anyone have any suggestions?
Process p = new Process();
p.StartInfo.FileName = "shutdown";
p.StartI开发者_如何学Gonfo.Arguments = @"/s /f /m \\pc-name /t 0";
p.Start();
The service normaly runs with other credentials (Local System, Network Service etc) than your test app. You should consider creating a domain user that has the proper rights and add that user to your service.
shutdown will fail if the account that you are logging in to on the remote PC doesn't have the SE_SHUTDOWN_NAME privilege.
This MSDN article contains a code snippet showing you how to set the SE_SHUTDOWN_NAME privilege from a program (and how to shutdown/restart the PC using the Windows API instead of calling the shutdown command).
But since you are running remotely and you may not be able to set those permissions programmatically, you can do so by logging in to the remote machine interactively and:
- Run secpol.msc to start the "Local Security Settings" application
- Go to Security Settings/Local Policies/User Rights Assignment in the tree on the left
- On the right, double click the Shut down the system policy to open up its properties
- Ensure that the account that will be calling shutdown is listed as having that privilege
You will only have to do that once.
Good luck.
精彩评论