1

I would like to ask how to prevent shutdown, when running a script or at least give a popup that will ask whenever or not you want to shutdown (like when you open a notepad and write a char, but doesn't save it and the click shutdown).

I have been creating scripts that runs installers silent, but some of them still seems to activate windows shutdown (this can happen if they are missing prerequisites).

Here is the code I use for the installation:

# --- Install ---
$fileExtension = (Get-ChildItem -path $installationFilePath).Extension

if(".msi" -eq $fileExtension)
{
    [string[]]$Private:args = New-Object string[] 4
    $args[0] = "/qn"
    $args[1] = "TRANSFORM=1033.mst"
    $args[2] = "REBOOT=Suppress"
    $args[3] = "/l*v $errorLogPath"

    $process = Start-Process $installationFilePath -ArgumentList $args -PassThru
}

if(".exe" -eq $fileExtension)
{
    [string[]]$Private:args = New-Object string[] 2
    $args[0] = '/v"' + "/qn TRANSFORM=1033.mst REBOOT=Suppress /l*v $errorLogPath" + '"'
    $args[1] = "/s"

    $process = Start-Process $installationFilePath -ArgumentList $args -PassThru
} 

$processActive = $process

while ($processActive -ne $null)
{
    Start-Sleep -Seconds 1
    Write-Host '.' -NoNewline
    $processActive = Get-Process -Id $processActive.Id -ErrorAction SilentlyContinue
}

I know this should be possible, but I have yet to find out how.

  • [This thread](http://stackoverflow.com/questions/629240/prevent-windows-from-going-into-sleep-when-my-program-is-running) might help, even though I don't see how the question is connected to C#. – Markus Deibel Mar 31 '17 at 09:42
  • It is relative easy to use C# and CMD together with Powershell of course it is preferable if the code is written in Powershell. Thx. for the answer and I will look into this some more, because I'm uncertain the method will work on all versions of windows (Windows XP or newer). –  Mar 31 '17 at 10:08
  • You can abort a shutdown using `shutdown.exe /a` – Bali C Mar 31 '17 at 10:23
  • Thanks for the anwser and it seems like a good idea in theory, but do the shutdown have a delay or will it be activated as fast as possible? Because it would be bad if windows did close all or some of the processes before the shutdown were prevented. –  Mar 31 '17 at 11:07
  • @rpriisholm You could just do a `sleep` and delay for however long you need, then run the shutdown command? – Bali C Mar 31 '17 at 11:51
  • @BaliC I could easily run the command for preventing shutdown after the installation had finished, but there will still be some delay (a little more than 1. sec in this script) and in that time the shutdown can be initiated, which could cause some processes to close. However if there is a delay in windows the method might work otherwise it would be risky at best (I will check if there is a delay), anyway it might be an answer to my problem. –  Mar 31 '17 at 14:04
  • @rpriisholm You could chain the commands as part of cmd, then they are executed immediately after, rather than powershell starting each individually. Like `cmd /c msi yourfile && shutdown /a`, if you wait on the msi to finish. It's always going to be risky though, there's no clean way of doing it that I know of. – Bali C Mar 31 '17 at 14:11
  • @rpriisholm See my answer, hopefully that makes more sense than my comment! – Bali C Mar 31 '17 at 14:17

1 Answers1

0

Here is an example of aborting shutdown after the install has finished:

Start-Process yourprogram.exe -Wait
shutdown /a

You could even loop the abort a few times to make sure you hit it.

for($i=0;$i -lt 5;$i++)
{
    shutdown /a
    sleep 1
}
Bali C
  • 30,582
  • 35
  • 123
  • 152