【发布时间】:2019-01-24 09:00:09
【问题描述】:
我知道如何运行一个 exe 文件,很简单。 我还想出了如何在 powershell 关闭时运行命令:
Register-EngineEvent PowerShell.Exiting –Action { 'Code' }
但我不知道如何使用 Powershell 运行程序,然后让它在关闭时在 Powershell 中执行命令,然后关闭 Powershell 本身。
$job = Start-Job { net start wuauserv }
Wait-Job $job
Receive-Job $job
Start-Process -FilePath "C:\Program Files (x86)\CpuCoreParking\CpuCoreParking3.exe" -Wait
Register-EngineEvent PowerShell.Exiting –Action {
$job = Start-Job { net stop wuauserv }
Wait-Job $job
Receive-Job $job
Start-Sleep -Seconds 2
}
到目前为止,这是我的代码,但它似乎并没有最终停止服务。什么都没有发生,它只是在我关闭程序后自行关闭。它确实做了一些事情,但......不确定是什么。
【问题讨论】:
-
Start-Processcmdlet 有一个-Wait参数,它可以为您提供所需的一切。你试过了吗? [咧嘴一笑] -
不是重复的。我知道如何在退出时运行命令。我在问如何让 Powershell 等待在其中运行的程序关闭,然后它会自动与它一起关闭。
标签: powershell cmd