Restart-Computer and hold script
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:UsersMeDesktopLog.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.
script powershell reboot
|
show 2 more comments
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:UsersMeDesktopLog.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.
script powershell reboot
3
Unless 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
Nov 4 '14 at 15:01
1
You 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_MACHINESOFTWAREMicrosoftWindowsCurrentVersionRun for example
– cybernard
Nov 5 '14 at 5:19
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
Nov 5 '14 at 14:12
2
Yes, 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
Nov 5 '14 at 15:10
1
I 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
Nov 8 '14 at 15:40
|
show 2 more comments
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:UsersMeDesktopLog.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.
script powershell reboot
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:UsersMeDesktopLog.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.
script powershell reboot
script powershell reboot
edited Nov 5 '14 at 13:58
Jente
asked Nov 4 '14 at 10:20
JenteJente
2411718
2411718
3
Unless 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
Nov 4 '14 at 15:01
1
You 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_MACHINESOFTWAREMicrosoftWindowsCurrentVersionRun for example
– cybernard
Nov 5 '14 at 5:19
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
Nov 5 '14 at 14:12
2
Yes, 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
Nov 5 '14 at 15:10
1
I 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
Nov 8 '14 at 15:40
|
show 2 more comments
3
Unless 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
Nov 4 '14 at 15:01
1
You 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_MACHINESOFTWAREMicrosoftWindowsCurrentVersionRun for example
– cybernard
Nov 5 '14 at 5:19
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
Nov 5 '14 at 14:12
2
Yes, 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
Nov 5 '14 at 15:10
1
I 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
Nov 8 '14 at 15:40
3
3
Unless 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
Nov 4 '14 at 15:01
Unless 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
Nov 4 '14 at 15:01
1
1
You 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_MACHINESOFTWAREMicrosoftWindowsCurrentVersionRun for example
– cybernard
Nov 5 '14 at 5:19
You 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_MACHINESOFTWAREMicrosoftWindowsCurrentVersionRun for example
– cybernard
Nov 5 '14 at 5:19
2
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
Nov 5 '14 at 14:12
@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
Nov 5 '14 at 14:12
2
2
Yes, 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
Nov 5 '14 at 15:10
Yes, 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
Nov 5 '14 at 15:10
1
1
I 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
Nov 8 '14 at 15:40
I 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
Nov 8 '14 at 15:40
|
show 2 more comments
0
active
oldest
votes
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f835705%2frestart-computer-and-hold-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f835705%2frestart-computer-and-hold-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Unless 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
Nov 4 '14 at 15:01
1
You 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_MACHINESOFTWAREMicrosoftWindowsCurrentVersionRun for example
– cybernard
Nov 5 '14 at 5:19
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
Nov 5 '14 at 14:12
2
Yes, 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
Nov 5 '14 at 15:10
1
I 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
Nov 8 '14 at 15:40