gpt4 book ai didi

powershell - 如何在 Powershell 中获取新创建的 Firefox 窗口的正确 PID?

转载 作者:行者123 更新时间:2023-12-05 03:28:17 24 4
gpt4 key购买 nike

这是我遇到的问题的一个非常简单的例子:

$process = Start-Process 'C:\Program Files\Mozilla Firefox\firefox.exe' -argumentlist "-new-window https://google.com -foreground" -PassThru

Write-Host $process.Id

firefox 窗口将启动并按预期工作,它会返回一个进程 ID,但当我实际检查正在运行的进程时,我看不到该 PID 的结果。

我试着添加这个只是为了看看,

while (-not $process.HasExited){
Write-Host "..."
}

Write-Host $process.HasExited

看起来进程确实在退出前可能运行了几毫秒。

我认为这可能与 Firefox 处理其自身进程的方式有关。因为我用其他一些随机应用程序测试了类似的设置,它们都按预期工作。

对于如何在 Firefox 中解决这个问题有什么想法吗?

最佳答案

有几个挑战需要克服:

  • 最终显示实际浏览器窗口的 firefox 进程不同于最初启动 的进程。也就是说,正如您所观察到的,已启动的进程会生成其他进程,并且自身会快速退出。

  • 作为Olaf指出,现代浏览器通常启动多个非 transient 进程,因此挑战在于如何识别代表浏览器窗口的进程。

  • 浏览器可以重用现有进程,因此单个进程可以显示多个窗口/选项卡,关闭其中一个不会终止整个进程。

    • 如果您需要确保使用专用的新进程,您有两个选择:

      • (a) 确保没有预先存在的 Firefox 实例正在运行,要么通过出错,要么 - 如果您的用例可接受 - 首先强制终止所有现有实例(Stop-Process -Name firefox).
      • (b) 通过更多的努力,创建一个专用的临时 Firefox 配置文件,您可以使用 -new-instance 选项启动它,它允许多个独立的 Firefox 实例并发运行,其生命周期可以单独跟踪。

以下 - 繁琐 - 解决方案实现了选项 (b):

  • 如果没有找到firefox进程,则不用担心创建独立实例,Firefox可以正常启动。

  • 否则,将创建一个临时配置文件,并通过 -new-instance-profile 启动 options以确保使用进程来呈现新的浏览器窗口。

  • 启动初始进程后,循环直到 later 启动的 firefox 进程出现,并且有一个非空窗口标题,然后假定这是真正感兴趣的过程。

  • 然后您可以等待该进程的终止,以了解专用浏览器窗口何时关闭。如果必须创建临时配置文件,则会在之后进行清理。

# Comment this statement out to silence the verbose messages below.
$VerbosePreference = 'Continue'

$now = Get-Date
$url = 'https://example.org' # URL to open.

# Launch a (new) Firefox instance.
if ($alreadyRunning = [bool] (Get-Process -ErrorAction Ignore firefox)) {
# Determine the path for a temporary profile with a unique name.
$tempProfilePath = Join-Path ([IO.Path]::GetTempPath()) ([datetime]::utcnow.tostring('o') -replace '\D')
Write-Verbose "Firefox is already running. Creating temp. profile $tempProfilePath..."
# Note: Creating an empty directory for the profile is seemingly enough.
$null = New-Item -Type Directory $tempProfilePath
Write-Verbose "and starting a new instance with it..."
Start-Process firefox "-new-instance -profile $tempProfilePath $url"
} else {
Write-Verbose 'Firefox isn''t running. Starting normally...'
Start-Process firefox $url
}

# Find the newly launched process that is the actual browser window.
Write-Verbose 'Waiting for a recently launched Firefox process with a nonempty window title to appear...'
while (-not (
$ps = Get-Process firefox |
Where-Object StartTime -gt $now |
Where-Object MainWindowTitle
)) {
Write-Host -NoNewLine .
Start-Sleep -MilliSeconds 500
}
Write-Host

Write-Verbose "Found. Waiting for process to exit..."
$ps.WaitForExit()

Write-Verbose 'Process has exited.'

if ($alreadyRunning) {
Write-Verbose "Cleaning up temporary profile $tempProfilePath..."
do {
# The profile dir. is typically held on to for a little while longer by associated processes that may not have terminated yet.
Start-Sleep -MilliSeconds 200
Remove-Item -ErrorAction Ignore -Literalpath $tempProfilePath -Recurse -Force
}
while (Test-Path -LiteralPath $tempProfilePath)
}

关于powershell - 如何在 Powershell 中获取新创建的 Firefox 窗口的正确 PID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71278141/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com