简体   繁体   English

防止停机 - C#,Powershell或CMD / Bat

[英]Prevent Shutdown - C#, Powershell or CMD/Bat

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). 我一直在创建运行安装程序静默的脚本,但其中一些似乎仍然激活了windows shutdown(如果它们缺少必备条件,就会发生这种情况)。

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. 我知道这应该是可能的,但我还没有找到方法。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM