Powershell 将 f5 键发送到 Edge

问题描述 投票:0回答:3

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

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 它将创建时间戳,我希望它仅发送到边缘,有什么办法解决吗?

powershell key send
3个回答
2
投票

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

$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
    之前
    AppActivate
    ),并且
  • 窗口激活后立即发送密钥。

0
投票

这对我来说部分有效,我们所做的改变是让它看起来像这样:

$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 ( $true ) ### activate the desired window
{
    $wshell.AppActivate($medge.Id)
    $wshell.SendKeys('{f5}')             ### send keys only if successfully activated
    Start-Sleep -Seconds 15
}

希望它对任何人都有帮助!


0
投票

这对我来说太棒了

function RefreshEdgePage {
    # Get the Microsoft Edge process
    $edgeProcess = Get-Process -Name msedge -ErrorAction SilentlyContinue
    # If the Edge process is running
    if ($edgeProcess) {
        # Try to activate the Edge window using the title
        $edgeWindow = Get-Process -Name msedge | Where-Object { $_.MainWindowTitle -ne "" } | Select-Object MainWindowTitle -First 1
        if ($edgeWindow) {
            $wshell = New-Object -ComObject WScript.Shell
            $wshell.AppActivate($edgeWindow.MainWindowTitle)
            Start-Sleep -Seconds 1 # Wait for a second to ensure Edge is active
            $wshell.SendKeys('{F5}')
        }
        else {
            Write-Host "Could not find the Microsoft Edge window."
        }
    }
    else {
        Write-Host "Microsoft Edge is not running."
    }
}

如果您想进行频繁的、不确定的更新,请使用以下命令并以秒为单位修改时间

function RefreshEdgePage {
    # Get the Microsoft Edge process
    $edgeProcess = Get-Process -Name msedge -ErrorAction SilentlyContinue
    # If the Edge process is running
    if ($edgeProcess) {
        # Try to activate the Edge window using the title
        $edgeWindow = Get-Process -Name msedge | Where-Object { $_.MainWindowTitle -ne "" } | Select-Object MainWindowTitle -First 1
        if ($edgeWindow) {
            $wshell = New-Object -ComObject WScript.Shell
            $wshell.AppActivate($edgeWindow.MainWindowTitle)
            Start-Sleep -Seconds 1 # Wait for a second to ensure Edge is active
            $wshell.SendKeys('{F5}')
        }
        else {
            Write-Host "Unable to find the Microsoft Edge window."
        }
    }
    else {
        Write-Host "Microsoft Edge is not running."
    }
}

# Define the time interval in seconds (5 seconds)
$interval = 5

# Execute the action at each time interval
while ($true) {
    RefreshEdgePage
    Start-Sleep -Seconds $interval
}

希望你觉得它有用

© www.soinside.com 2019 - 2024. All rights reserved.