0

I have a function in a WCF service hosted in IIS 7.5 which it task is shutdown or restart computer when it is being called.

I use the built in command line and execute it by passing "shutdown.exe -t 0 -r" to restart or "shutdown.exe -t 0 -s" to shutdown.

    Try
        Using process As New System.Diagnostics.Process()
            Dim startInfo As New System.Diagnostics.ProcessStartInfo()
            With startInfo
                .WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
                .CreateNoWindow = True
                .UseShellExecute = False
                .RedirectStandardError = True
                .RedirectStandardInput = True
                .RedirectStandardOutput = True
                .FileName = "shutdown.exe"
                .Arguments = "-t 0 -r"
            End With
            If System.Environment.OSVersion.Version.Major >= 6 Then process.StartInfo.Verb = "runas"
            process.StartInfo = startInfo
            process.Start()
            process.WaitForExit()
            If process.ExitCode = 0 Then
                Return True
            Else
                Return False
            End If
        End Using
    Catch ex As Exception
        Return False
    End Try

The command line works fine if execute in command prompt manually. However it doesn't work when it is being execute inside WCF service.

Buzz
  • 321
  • 2
  • 3
  • 20
  • What identity is the WCF service running as? You might also want to add `-f` to the arguments. – slugster Nov 24 '15 at 06:31
  • The wcf application pool run as localsystem. What is -f parameter? – Buzz Nov 24 '15 at 06:34
  • It forces running applications to close: https://technet.microsoft.com/en-us/library/bb491003.aspx – slugster Nov 24 '15 at 06:39
  • @slugster It still doesn't work. I thought by using localsystem identity we will have all administrator privileges. But it seems it is not how it works. – Buzz Nov 24 '15 at 07:00

2 Answers2

2

Ok, trying everything and keep failed, and ended up on this guy who use bat file. And it works..!

It is so weird and still don't know why it works. For now...who cares :)...later when I have spare time I will try to figure it out.

        ....
        Dim strCmd As String = "SHUTDOWN -m {0} -f -t 0 -r"           
        File.WriteAllText(_pathCmd & "cmd_restart.bat", String.Format(strCmd, "127.0.0.1"))
        Return ExecuteCommand(_pathCmd & "cmd_restart.bat", "")
Buzz
  • 321
  • 2
  • 3
  • 20
1

I guess that your service doesn't have permission to shutdown pc (I guess user that owes permission is network user and can't do this). Check out Event Log for details, I am pretty sure it contains answer on your question.

Check out there how to run wcf service under specific account.

Community
  • 1
  • 1
RredCat
  • 5,259
  • 5
  • 60
  • 100
  • I tried to run the process with provide specific administrator credential. Then I get 4 new event log saying: 1. A logon was attempted using explicit credentials. 2. An account was successfully log on. 3. Special previledge assigned to new log on. 4. An account was logged off. – Buzz Nov 24 '15 at 06:52
  • But nothing happen. No restart, no shutdown. Nothing. – Buzz Nov 24 '15 at 06:54