[英]powershell how to close a .exe process
I want to start a .exe file from powershell and wait for it to finish and then continue to the next line in the powershell. 我想从Powershell启动.exe文件,然后等待它完成,然后继续执行Powershell中的下一行。 However after the .exe file finished it's job the window remain there to be manually closed. 但是,.exe文件完成后,窗口仍然存在,需要手动关闭。 Just like notepad. 就像记事本一样。 Here is what I got 这就是我得到的
start-process -FilePath notepad -Wait -NoNewWindow
echo "it's done!"
I specified -NoNewWindow but it still comes with a window and the powershell have to wait until I manually close it. 我指定了-NoNewWindow,但它仍然带有一个窗口,而powershell必须等到我手动关闭它。 How can I close it automatically? 如何自动关闭? The .exe file is a third party app that I have no control over. .exe文件是我无法控制的第三方应用程序。
Please note that I want to wait for the task that the .exe file completes and then automatically close that window so that I can continue to the next line in powershell. 请注意,我要等待.exe文件完成的任务,然后自动关闭该窗口,以便我可以继续执行Powershell中的下一行。
I also tried -WindowStyle hidden. 我还尝试了-WindowStyle隐藏。
start-process -FilePath notepad -Wait -WindowStyle hidden
echo "it's done!"
But this is even worse I have to go to task manager to kill the hidden window without knowing it is completed or not. 但是,更糟糕的是,我必须去任务管理器杀死隐藏的窗口,而不知道它是否完成。
Is there a parameter better than -NoNewWindow that can force no new window show up? 是否有比-NoNewWindow更好的参数可以强制不显示新窗口?
Thanks, 谢谢,
This is what I use to kill iTunes automatically. 这就是我用来自动杀死iTunes的方式。 But, as mentioned in my comment, if you want to wait for it to be done, you'd have to add either a sleep
or do some application-specific check to see that its job is done. 但是,正如我的评论中提到的那样,如果您想等待它完成,则必须添加一个sleep
或进行一些特定于应用程序的检查以查看其工作是否完成。 What that check is depends on what the exe is doing. 检查的内容取决于exe在做什么。
$itunesProc = Get-Process -name iTunes -ErrorAction "Ignore"
if ($itunesProc -ne $null) {
echo "Stopping iTunes..."
Stop-Process -Name iTunes
} else {
echo "No running iTunes process found"
}
The script below will: 以下脚本将:
You will need to install AutoIt . 您将需要安装AutoIt 。 (I selected "x86" in the installer BTW.) (我在安装程序BTW中选择了“ x86”。)
You will also need to adapt the Get-AU3ControlHandle
call to suit your needs. 您还需要调整Get-AU3ControlHandle
调用以适合您的需求。 The AutoIt Window Info Tool will be useful there. AutoIt窗口信息工具将在此处有用。 You may have to do some reading of the AutoIt documentation. 您可能需要阅读AutoIt文档。
See also AutoIt Cmdlets for Windows PowerShell . 另请参阅Windows PowerShell的AutoIt Cmdlet 。
import-module "${env:ProgramFiles(x86)}\AutoIt3\AutoItX\AutoItX.psd1"
$p = Start-Process notepad -PassThru
# Wait for the window to open
while($p.MainWindowHandle -eq 0)
{
Start-Sleep 1
}
$c = Get-AU3ControlHandle -WinHandle $p.MainWindowHandle -Control "Edit1"
while($true)
{
$t = Get-AU3ControlText $p.MainWindowHandle $c
if ($p.HasExited)
{
break;
}
elseif ($t -eq "Finished")
{
$p.CloseMainWindow();
break;
}
else
{
Write-Output "Waiting 1 sec."
Start-Sleep 1
}
}
Write-Output "Done"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.