Powershell脚本仅在单击窗口时有效

时间:2016-12-02 15:23:32

标签: powershell scripting

这是我创建的脚本:

add-type -AssemblyName System.Windows.Forms     
$ie = New-Object -ComObject "InternetExplorer.Application"  
$ie.Navigate("http://www.microsoft.com")
$ie.Navigate2("http://www.google.com" , 2048)
$ie.Navigate2("http://www.outlook.com" , 2048)
$ie.Navigate2("https://studer-ksg.intranet.koerber.de/de/nc/fritz-studer-ag.html" , 2048)
$ie.Visible = $true

start-sleep -Milliseconds 4000
[System.Windows.Forms.SendKeys]::SendWait('{F11}')

do
{
    start-sleep -Milliseconds 4000
    [System.Windows.Forms.SendKeys]::SendWait('^{TAB}')
    start-sleep -Milliseconds 100
    [System.Windows.Forms.SendKeys]::SendWait("{F5}")
}
While ($i -ne 0)

它打开一个IE窗口,全屏显示4个选项卡,并在它们之间切换。当我启动脚本时,窗口打开但直到我点击它才会发生任何事情。我怎样才能自动生成这个,所以你只需要运行脚本,一切都可以自行完成?

1 个答案:

答案 0 :(得分:0)

您需要将IE窗口置于前面,以便在SendKeys工作之前具有焦点:

Add-Type -AssemblyName System.Windows.Forms
Add-Type -Assembly "Microsoft.VisualBasic"

$ie = New-Object -ComObject "InternetExplorer.Application"  
$ie.Navigate("http://www.microsoft.com")
$ie.Navigate2("http://www.google.com" , 2048)
$ie.Navigate2("http://www.outlook.com" , 2048)
$ie.Visible = $true

$IEProcess = Get-Process | Where { $_.MainWindowHandle -eq $ie.HWND }
[Microsoft.VisualBasic.Interaction]::AppActivate($IEProcess.Id)

Start-Sleep -Milliseconds 4000
[System.Windows.Forms.SendKeys]::SendWait('{F11}')

do
{
    Start-Sleep -Milliseconds 4000
    [System.Windows.Forms.SendKeys]::SendWait('^{TAB}')
    Start-Sleep -Milliseconds 100
    [System.Windows.Forms.SendKeys]::SendWait("{F5}")
}
While ($i -ne 0)