从 Powershell 脚本打开 IE 的最大化窗口?

3
抱歉,如果这个问题太简单了,但我在帮助文件或在线搜索中没有找到关于如何做到这一点的任何信息。我正在打开一个新的浏览器窗口来测试基于Web的应用程序的登录/注销功能,但我想以最大化的模式打开IE窗口。我可以设置大小为: $ie.height = 1024 $ie.width - 768
但是是否有关键字或任何东西可以自动最大化它,还是我需要先查询屏幕大小,然后从该查询中填充值?
/matt
6个回答

4
解决了启动IE窗口最大化的问题,方法如下:
Function maxIE
{
param($ie)
$asm = [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

    $screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds
    $ie.Width = $screen.width
    $ie.Height =$screen.height
    $ie.Top =  0
    $ie.Left = 0
}


cls
$ie = new-object -com "InternetExplorer.Application"
$ie.visible = $true
maxIE $ie
while ($ie.busy) {sleep -milliseconds 50}
$ie.navigate("http://www.google.com")

4

(new-object -com wscript.shell).run("url",3)


这会打开IE,但不会最大化它 - 至少在我的Win7电脑上是这样。 - Keith Hill
它会启动默认浏览器,在我的Win7机器上是最大化的,但那是在Firefox中。我将更改它以启动IE并查看结果。非常感谢你们两个的意见,我很感激。 /马特 - Matt Dewey
将我的默认浏览器设置为IE后,它以最大化模式启动。完美! - Matt Dewey
1
如果您已经打开了IE并且没有将其最大化,则此方法*不起作用。它只会向现有的IE添加另一个选项卡。如果您没有打开IE,则可以使用内置的cmdlet:Start-Process "url" -WindowStyle Maximized。 - Keith Hill

2

我没有找到任何特定的答案来解决问题,但是我找到了一种组合方法。调用它们的顺序很重要,否则它不起作用。

完整示例:

$ie = New-Object -Com "InternetExplorer.Application"
$urls = @("http://www.google.com","http://www.yahoo.com")
$ie.Visible = $true
CLS
write-output "Loading pages now..."
#Maximize IE window
$asm = [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds
$ie.height = $screen.height
#Open webpages
foreach ($link in $urls) {
   $ie.Navigate2($link, 0x1000) 
}
#close first blank tab
$sa = New-Object -ComObject Shell.Application
$tab = $sa.windows() | Where {$_.Name -match 'Internet Explorer' -and     $_.LocationName -eq ''}
$tab.quit()

1

如果您在PowerShell 2.0上安装了PowerShell社区扩展1.2(PSCX),我已经验证过这个可以工作:

Pscx\Start-Process IExplore.exe; Start-Sleep 3; $hwnd = Get-ForegroundWindow
$sig = @'
[DllImport("user32.dll")] 
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
'@
Add-Type -MemberDefinition $sig -name NativeMethods -namespace Win32
[Win32.NativeMethods]::ShowWindowAsync($hwnd, 3)

这有点棘手,因为它使用了一个等待(start-sleep)3秒钟的方法来等待IE打开,然后使用PSCX cmdlet获取前台窗口的句柄。 如果您只运行了一个IExplore实例,则可以使用此方法获取该句柄:

@(Get-Process IExplore)[0].MainWindowHandle

需要PowerShell 2.0才能支持Add-Type,从而允许我们调用Win32 API。

顺便说一句,通过快速的Bing搜索,似乎让IE启动最大化是一个相当普遍的问题。例如,使用Start-Process可以指定-WindowStyle Maximized,但IE不会遵守这个设置。


谢谢您的建议,我会尝试添加并查看结果。有点尴尬,您在Bing上轻松找到了信息,而我在Google上搜索了多种打开窗口最大化大小的变体,大多数返回结果都是处理打开PowerShell窗口的!这就是提问方式的不同吧! - Matt Dewey
说实话,我只是在必应上搜索“打开IE全屏”,发现即使在PowerShell的上下文之外,人们也会遇到这个问题。 - Keith Hill

1

如果有人需要帮助,我最终只是调用了最大化的iexplore,然后连接到它。你需要睡眠,因为它调用太快了,无法连接。我相信有更好的方法,但我想不出来。

start iexplore -WindowStyle maximized
Start-Sleep -seconds 1
$ie = (New-Object -COM "Shell.Application").Windows() | ? { $_.Name -eq "Internet Explorer" }
$ie.navigate("URL")


0
#We will use the Win32 API function ShowWindowAsync, and spawn an IE Window Maximized.
#Parameters can be used for ShowWindowAsync
$Hide = 0
$Normal = 1
$Minimized = 2
$Maximized = 3
$ShowNoActivateRecentPosition = 4
$Show = 5
$MinimizeActivateNext = 6
$MinimizeNoActivate = 7
$ShowNoActivate = 8
$Restore = 9
$ShowDefault = 10
$ForceMinimize = 11

#Specify an interwebs address :)
$URL="http://www.google.com/"
#Create internetexplorer.application object
$IE=new-object -com internetexplorer.application
#Set some parameters for the internetexplorer.application object
$IE.TheaterMode = $False
$IE.AddressBar = $True
$IE.StatusBar = $False
$IE.MenuBar = $True
$IE.FullScreen = $False
$IE.visible = $True
#Navigate to the URL
$IE.navigate2($URL)

#the C#-style signature of an API function
$code = '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);'

#add signature as new type to PowerShell (for this session)
$type = Add-Type -MemberDefinition $code -Name myAPI -PassThru

#Magic:
$type::ShowWindowAsync($IE.HWND[0], $Maximized)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接