4
1
I'm running a Powershell script that requires a reboot in the middle of the script. After the reboot, the script is executing some commands (that can't be executed before the reboot!). I'm rebooting the computer with Restart-Computer, and the reboot start immediately. However, the first commands after the reboot command will be executed to!
So the reboot is started, but the script is still running (for a few seconds..). Is there a way to hold the script, so it won't continue while the computer is shutting down?
Restart-Computer -Force
"This is some content" >> C:\Users\Me\Desktop\Log.txt
In the example above, the computer will restart, but the text file (with "this is some content") is also created. I want to restart the computer, without the text file being created. So without continuing the script. Now I'm doing this with Start-Sleep, but there must be a better way..?
Thank you.
3Unless you are doing something to cause it to restart, the script will not restart after the reboot anyway. So, just take all the code after the reboot out. If you are causing the script to restart the script after the reboot then you should be restarting it with a command line parameter that indicates it is post reboot and execute code based on the presence of that parameter. – EBGreen – 2014-11-04T15:01:12.370
1You can do as suggested above or end the script at the reboot and create a second script and have it autorun after reboot. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run for example – cybernard – 2014-11-05T05:19:13.117
I Have a way to continue my script after the reboot, that's not the question. The question is how to restart the computer, right after the Restart-Command (without executing the commands after that command).. not possible I think? – Jente – 2014-11-05T10:10:20.730
2@Jente you have a way to restart your script, or continue it from after the reboot line? Either way, maybe it's better to just separate the two into different files/scripts. – Breakthrough – 2014-11-05T14:12:54.680
There is also a RunOnce registry key that I would use instead of Run. i.e. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce which will only run the second half the script on the first reboot. Read Here
– Tyson – 2014-11-05T14:44:14.2172Yes, definitely split it into two separate files or put the pre reboot code and post reboot code into functions then call the proper function based on a command line parameter. – EBGreen – 2014-11-05T15:10:39.530
1I don't know if it needs to be two separate files, but the two behaviors of the script should be separated by a switch or parameter. A
-RunAfterReboot
switch with a conditional in the script itself would be sufficient. – Bacon Bits – 2014-11-08T15:40:01.313