使用PowerShell杀死Windows上的子进程

问题描述 投票:0回答:1

我有下面的脚本,但它似乎不会杀死子进程。我如何杀死子进程?

当它运行时,它看起来很好,但后面还有对话框,然后当我检查任务管理器时,原始进程仍在运行。这让我觉得如果脚本运行正常。另一个奇怪的部分是,我自然会看到这些进程(.exe程序)在任务栏上运行,但在脚本运行后看起来它们没有运行。但是,我再次检查任务管理器,果然,它们仍在运行。

$ErrorActionPreference = "SilentlyContinue"
Stop-Transcript | Out-Null
$ErrorActionPreference = "Continue"
Start-Transcript -Path C:\Scripts\Errors\errors.log -Append
$process = Get-Process -Name "IVR1","IVR2","IVR3"
$IVR1path = "C:\IVR1"
$IVR2path = "C:\IVR2"
$IVR3path = "C:\IVR3"
if ($process) {
    $Process | Stop-Process -Force
    Start-Sleep -s 5
    cd $IVR1path
    Start-Process ".\IVR1.exe"
    cd IVR2path
    Start-Process ".\IVR2.exe"
    cd IVR3path
    Start-Process ".\IVR3.exe"
    cd ..
    cd ..
    $From = "[email protected]"
    $To = "[email protected]"
    $cc = "[email protected]"
    $Subject = "**TEST** - IVR1 has been recovered"
    $Body = "The IVR has been successfully recovered"
    $SMTPServer = "mail.example.com.au"
    Send-MailMessage -From $From -to $To -cc $cc -Subject $Subject -Body $Body -SmtpServer $SMTPServer
    Stop-Transcript
}

有人会有什么建议吗?可能是由于子进程未被杀死或原始进程被挂起?

powershell powershell-v4.0
1个回答
0
投票

我认为你滥用工具来实现目标。使用https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-process?view=powershell-4.0和页面末尾列出的函数(所有关于进程生命周期)。附注:添加更多详细程度(写入输出变量值)到您的脚本所做的事情,这样您就会知道发生了什么(或使用调试器,因为我不确定您粘贴的大部分代码是否都被执行)。

© www.soinside.com 2019 - 2024. All rights reserved.