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
Finally I found the answer.
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.
Then your program will be started after automatic restart.
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") {
};
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