【问题标题】:Open IE maximized from Powershell script?从 Powershell 脚本打开 IE 最大化?
【发布时间】:2009-08-27 00:44:10
【问题描述】:

如果这是一个太简单的问题,请原谅我,但到目前为止,我在帮助文件或网上没有找到任何关于这样做的内容。我正在打开一个新的浏览器窗口来测试基于 Web 的应用程序的登录/注销功能,但我想以最大化模式打开 IE 窗口。我可以将大小设置为:

$ie.height = 1024 $ie.width - 768

但是是否有一个关键字或任何我可以用来自动最大化打开它的关键字或任何东西,或者我需要先查询屏幕大小,然后填写该查询中的值?

/马特

【问题讨论】:

    标签: powershell


    【解决方案1】:

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

    【讨论】:

    • 这会打开 IE,但不会最大化它 - 至少在我的 Win7 PC 上。
    • 它启动默认浏览器,在我的情况下,它在我的 Win7 机器上被最大化 - 但那是在 Firefox 中。我将更改它以启动 IE,看看我得到了什么。谢谢两位的意见,我很感激。 /马特
    • 将我的默认浏览器设置为 IE,它以最大化模式启动。完美!
    • 因此,如果您已经打开了一个 IE 实例并且未最大化,它不会工作。它只是向现有的 IE 添加了另一个选项卡。如果您没有打开 IE,那么只需使用内置 cmdlet:Start-Process "url" -WindowStyle Maximized。
    【解决方案2】:

    解决了启动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")
    

    【讨论】:

      【解决方案3】:

      我无法得到任何具体的工作答案,但确实得到了一个工作组合。调用它们的顺序很重要,否则它不起作用。

      完整示例:

      $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()
      

      【讨论】:

        【解决方案4】:

        如果您在 PowerShell 2.0 上安装了 PowerShell Community Extensions 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)
        

        这有点冒险,因为它使用 3 秒的等待(开始-睡眠)来等待 IE 打开,然后它使用 PSCX cmdlet 来获取前台窗口的窗口句柄。如果您只有一个 IExplore 实例正在运行,那么您可以使用它来获取该句柄:

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

        添加类型支持需要 PowerShell 2.0,它允许我们调用 Win32 API。

        顺便说一句,快速 Bing 搜索似乎让 IE 开始最大化是一个很常见的问题。例如,使用 Start-Process 您可以指定 -WindowStyle Maximized 但 IE 不支持。

        【讨论】:

        • 谢谢你的建议,我会弹出来看看我得到了什么。有点尴尬,您在 Bing 上如此轻松地找到信息,我在 Google 上搜索了以最大化尺寸打开窗口的多种变体,并且大多数回报都与打开 Powershell 窗口有关!我猜你怎么问!
        • 很清楚,我刚刚在 Bing 中搜索了“Open IExplore Maximized”,并注意到即使在 PowerShell 的上下文之外,人们也会遇到这个问题。
        【解决方案5】:

        以防万一其他人需要帮助,我最终只是将 iexplore 最大化然后连接到它。您需要睡觉,因为它调用太快而无法连接。我确定有更好的方法,但我想不通。

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

        【讨论】:

          【解决方案6】:
          #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)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2023-04-03
            • 1970-01-01
            • 1970-01-01
            • 2023-03-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多