Powershell 将 f5 键发送到边缘

Powershell send f5 key to edge

我正在尝试创建一个刷新边缘的脚本:

start microsoft-edge:https://login.salesforce.com/?locale=eu
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('Microsoft Edge') 

while ($true)
{
Sleep 15
$wshell.SendKeys('{f5}') 
}

问题是它发送 F5 到 windows 即时消息,例如,如果即时消息在记事本中,它将按 F5,它将创建时间戳,我希望它只发送到边缘,有没有办法解决是吗?

以下代码片段可以完成这项工作。你需要告诉 哪个 window 应该被激活,例如如下:

$medge = Start-Process -PassThru -FilePath microsoft-edge:https://login.salesforce.com/?locale=eu
$wshell = New-Object -ComObject wscript.shell

Start-Sleep -Seconds 15                  ### increase time for slow connection
while ( $wshell.AppActivate($medge.Id) ) ### activate the desired window
{
    $wshell.SendKeys('{f5}')             ### send keys only if successfully activated
    Start-Sleep -Seconds 15
}

以上代码片段(源自相关代码片段)每 15 秒刷新一次 microsoft-edge

时机很重要:

  • 授予 microsoft-edge 足够的时间 运行 并建立连接(参见 Start-Sleep before AppActivate), 和
  • window激活后立即发送密钥。