skip to Main Content

I have an dotnet application on Windows Server in Azure. I added scheduled task to restart the Windows. I used code which included below. Windows shuts down but does not restart at the scheduled time. When I try the same code part in normal Windows computer, it runs smoothly. Could you help me to solve this problem?

ProcessStartInfo startInfo = new("shutdown", "/r")    
{       
    WindowStyle = ProcessWindowStyle.Hidden,       
    ErrorDialog = false    
};

I want to restart a Windows server after rebooting. Should I change some settings in Azure Portal to restart it automatically?

2

Answers


  1. Chosen as BEST ANSWER

    Finally I found the answer.

    ProcessStartInfo startInfo = new("shutdown", "/r")    
    {       
        WindowStyle = ProcessWindowStyle.Hidden,       
        ErrorDialog = false    
    };
    

    This code part is still working for restart but it didn't enough for itself. The azure VM need to log on to run my bat file.

    I used this autologon program. And It worked liked charm.

    https://learn.microsoft.com/en-us/sysinternals/downloads/autologon

    If you need to run a cmd file on startup. Firstly create a bat file and put your command line prompt inside of it.

    1. Press Start, type Run, and press Enter
    2. In the Run window, type shell:startup to open the Startup folder.
    3. Once the Startup folder is opened, click the Home tab at the top of the folder. Then, select Paste to paste the shortcut file into the Startup folder.(https://www.computerhope.com/issues/ch000322.htm#:~:text=Run%20a%20batch%20file%20at%20loading%20of%20Windows%208%20and%2010&text=Press%20Start%2C%20type%20Run%2C%20and,file%20into%20the%20Startup%20folder.)

    Then your program will be started after automatic restart.


  2. The /t switch in the shutdown command requires a time delay in seconds before the restart or shutdown occurs. However, in your code, the /t switch is used without specifying any time delay, which might be the reason why the server is shutting down immediately but not restarting.

    Try this one

    ProcessStartInfo startInfo = new ProcessStartInfo("shutdown", "/r /t 60") {

     WindowStyle = ProcessWindowStyle.Hidden,
    
     ErrorDialog = false
    

    };

    This will restart the server after a delay of 60 seconds

    UPDATE
    If the connection in Royal TSX is getting disconnected and cannot start again after running the restart code, it is likely because the restart process is terminating the application or service responsible for handling the remote desktop connection. we can try to logoff the server then restarting it, I believe this will not interrupt the remote connection and it can start back up.

    Try this updated code

     ProcessStartInfo logoffInfo = new ProcessStartInfo("logoff", "/f")
    {
        WindowStyle = ProcessWindowStyle.Hidden,
        ErrorDialog = false
    };
    
    Process.Start(logoffInfo).WaitForExit();
    
    ProcessStartInfo restartInfo = new ProcessStartInfo("shutdown", "/r /t 60")
    {
        WindowStyle = ProcessWindowStyle.Hidden,
        ErrorDialog = false
    };
    
    Process.Start(restartInfo);
    `
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search