How to shutdown a remote computer from a windows service

I'm trying to shutdown a windows computer in my network, from a windows service running on another windows computer.

In the console, I can do that:

net use \\ComputerName Password /u:User
shutdown /s /t 0 /m \\ComputerName

But when I try to do it from my windows service:

Process.Start("net", "use \\ComputerName Password /u:User");
Process.Start("shutdown", "/s /t 0 /m \\ComputerName");

Nothing happens.

I know there is a permissions problem because my service is running on LocalSystem account, but I tried to change it to my local user and still not working.

Any help will be appreciated.

Upvotes: 0

Views: 2743

Answers (2)

Bukes
Bukes

Reputation: 3708

For what it is worth, you can do this progmatically using the documented WMI Win32_OperatingSystem/Win32Shutdown method.

The linked article also as has links to examples on how to use WMI from C++

Upvotes: 1

Dan Puzey
Dan Puzey

Reputation: 34200

If you use Process.Start you are calling each command in a separate session. You need instead to launch a single process for your command prompt, and invoke both commands through the same session. cmd /? is your friend.

cmd /c [commands]

... one can chain commands with & or ;.

Upvotes: 0

Related Questions