问题描述
我正在尝试创建一个刷新边缘的脚本:
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 到我所在的窗口,例如如果我在记事本中它会按 f5 它会创建时间戳,我希望它只发送到边缘,有什么办法可以解决吗?
解决方法
以下代码片段可以完成这项工作。您需要告诉哪个窗口应该被激活,例如a s 如下:
$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
beforeAppActivate
),以及 - 在窗口激活后立即发送密钥。