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

时间:2017-03-31 09:01:17

标签: c# windows powershell batch-file system-shutdown

我想问一下如何防止关机,在运行脚本时或至少给出一个弹出窗口,无论你何时想要关机都会问(比如当你打开一个记事本并写一个字符,但是没有'保存它并单击关闭)。

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

以下是我用于安装的代码:

# --- 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
}

我知道这应该是可能的,但我还没有找到方法。

1 个答案:

答案 0 :(得分:0)

以下是安装完成后中止关闭的示例:

Start-Process yourprogram.exe -Wait
shutdown /a

你甚至可以循环中止几次以确保你击中它。

for($i=0;$i -lt 5;$i++)
{
    shutdown /a
    sleep 1
}