Powershell 在屏幕上以特定大小和位置打开internet explorer

Powershell 在屏幕上以特定大小和位置打开internet explorer,powershell,internet-explorer,Powershell,Internet Explorer,我想在屏幕上的特定位置以特定的大小打开Internet Explorer 我找到了两种方法: 我对第一个的问题是,它做的一切都很好,但它开始滞后,使它无法使用,我不得不终止程序。我不知道为什么,因为我使用相同的代码打开Firefox,它工作正常: . C:\Users\user\git\testPowerShell/Set-Window.ps1 Start-Process -FilePath 'C:/Program Files/internet explorer/iexplore.exe' -A

我想在屏幕上的特定位置以特定的大小打开Internet Explorer

我找到了两种方法:

我对第一个的问题是,它做的一切都很好,但它开始滞后,使它无法使用,我不得不终止程序。我不知道为什么,因为我使用相同的代码打开Firefox,它工作正常:

. C:\Users\user\git\testPowerShell/Set-Window.ps1
Start-Process -FilePath 'C:/Program Files/internet explorer/iexplore.exe' -ArgumentList https://www.facebook.com/
Start-Sleep -Seconds 0
Set-Window -ProcessName iexplore -x 100 -y 5 -Width 1176 -Height 715 -Passthru
第二个问题是,打开的窗口没有焦点,我真的需要Internet Explorer在所有内容上打开:

$ie = new-object -comobject InternetExplorer.Application;
$ie.visible = $true;
#$ie2 = $ie.Width = 200;
$ie.top = 15; $ie.width = 1180; $ie.height = 710; $ie.Left = 192;
$ie.navigate('https://www.facebook.com/');
任何一种方法都有解决方案吗


另外,Set-Windows.ps1是这样的

Function Set-Window {
    <#
        .SYNOPSIS
            Sets the window size (height,width) and coordinates (x,y) of
            a process window.

        .DESCRIPTION
            Sets the window size (height,width) and coordinates (x,y) of
            a process window.

        .PARAMETER ProcessName
            Name of the process to determine the window characteristics

        .PARAMETER X
            Set the position of the window in pixels from the top.

        .PARAMETER Y
            Set the position of the window in pixels from the left.

        .PARAMETER Width
            Set the width of the window.

        .PARAMETER Height
            Set the height of the window.

        .PARAMETER Passthru
            Display the output object of the window.

        .NOTES
            Name: Set-Window
            Author: Boe Prox
            Version History
                1.0//Boe Prox - 11/24/2015
                    - Initial build
                1.1//JosefZ (https://superuser.com/users/376602/josefz) - 19.05.2018
                    - treats more process instances of supplied process name properly

        .OUTPUT
            System.Automation.WindowInfo

        .EXAMPLE
            Get-Process powershell | Set-Window -X 2040 -Y 142 -Passthru

            ProcessName Size     TopLeft  BottomRight
            ----------- ----     -------  -----------
            powershell  1262,642 2040,142 3302,784   

            Description
            -----------
            Set the coordinates on the window for the process PowerShell.exe

    #>
    [OutputType('System.Automation.WindowInfo')]
    [cmdletbinding()]
    Param (
        [parameter(ValueFromPipelineByPropertyName=$True)]
        $ProcessName,
        [int]$X,
        [int]$Y,
        [int]$Width,
        [int]$Height,
        [switch]$Passthru
    )
    Begin {
        Try{
            [void][Window]
        } Catch {
        Add-Type @"
              using System;
              using System.Runtime.InteropServices;
              public class Window {
                [DllImport("user32.dll")]
                [return: MarshalAs(UnmanagedType.Bool)]
                public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

                [DllImport("User32.dll")]
                public extern static bool MoveWindow(IntPtr handle, int x, int y, int width, int height, bool redraw);
              }
              public struct RECT
              {
                public int Left;        // x position of upper-left corner
                public int Top;         // y position of upper-left corner
                public int Right;       // x position of lower-right corner
                public int Bottom;      // y position of lower-right corner
              }
"@
        }
    }
    Process {
        $Rectangle = New-Object RECT
        $Handles = (Get-Process -Name $ProcessName).MainWindowHandle   ### 1.1//JosefZ
        foreach ( $Handle in $Handles ) {                              ### 1.1//JosefZ
            if ( $Handle -eq [System.IntPtr]::Zero ) { Continue }      ### 1.1//JosefZ
            $Return = [Window]::GetWindowRect($Handle,[ref]$Rectangle)
            If (-NOT $PSBoundParameters.ContainsKey('Width')) {            
                $Width = $Rectangle.Right - $Rectangle.Left            
            }
            If (-NOT $PSBoundParameters.ContainsKey('Height')) {
                $Height = $Rectangle.Bottom - $Rectangle.Top
            }
            If ($Return) {
                $Return = [Window]::MoveWindow($Handle, $x, $y, $Width, $Height,$True)
            }
            If ($PSBoundParameters.ContainsKey('Passthru')) {
                $Rectangle = New-Object RECT
                $Return = [Window]::GetWindowRect($Handle,[ref]$Rectangle)
                If ($Return) {
                    $Height = $Rectangle.Bottom - $Rectangle.Top
                    $Width = $Rectangle.Right - $Rectangle.Left
                    $Size = New-Object System.Management.Automation.Host.Size -ArgumentList $Width, $Height
                    $TopLeft = New-Object System.Management.Automation.Host.Coordinates -ArgumentList $Rectangle.Left, $Rectangle.Top
                    $BottomRight = New-Object System.Management.Automation.Host.Coordinates -ArgumentList $Rectangle.Right, $Rectangle.Bottom
                    If ($Rectangle.Top -lt 0 -AND $Rectangle.LEft -lt 0) {
                        Write-Warning "Window is minimized! Coordinates will not be accurate."
                    }
                    $Object = [pscustomobject]@{
                        ProcessName = $ProcessName
                        Size = $Size
                        TopLeft = $TopLeft
                        BottomRight = $BottomRight
                    }
                    $Object.PSTypeNames.insert(0,'System.Automation.WindowInfo')
                    $Object            
                }
            }
        }
    }
}
函数集窗口{
[OutputType('System.Automation.WindowInfo')]
[cmdletbinding()]
Param(
[参数(ValueFromPipelineByPropertyName=$True)]
$ProcessName,
[int]$X,
[int]$Y,
[int]$Width,
[int]$Height,
[交换机]$Passthru
)
开始{
试一试{
[作废][窗口]
}抓住{
添加类型@”
使用制度;
使用System.Runtime.InteropServices;
公共类窗口{
[DllImport(“user32.dll”)]
[返回:Marshallas(UnmanagedType.Bool)]
公共静态外部bool GetWindowRect(IntPtr hWnd,out RECT lpRect);
[DllImport(“User32.dll”)]
公共外部静态布尔移动窗口(IntPtr句柄、intx、inty、intwidth、intheight、布尔重绘);
}
公共结构矩形
{
public int Left;//左上角的x位置
public int Top;//左上角的y位置
public int Right;//右下角的x位置
public int Bottom;//右下角y位置
}
"@
}
}
过程{
$Rectangle=新对象RECT
$Handles=(获取进程-名称$ProcessName).MainWindowHandle####1.1//JosefZ
foreach($Handles中的Handle){1.1//JosefZ
if($Handle-eq[System.IntPtr]::Zero){Continue}###1.1//JosefZ
$Return=[Window]::GetWindowRect($Handle,[ref]$Rectangle)
If(-NOT$PSBoundParameters.ContainsKey('Width')){
$Width=$Rectangle.Right-$Rectangle.Left
}
If(-NOT$PSBoundParameters.ContainsKey('Height')){
$Height=$Rectangle.Bottom-$Rectangle.Top
}
如果($返回){
$Return=[Window]::MoveWindow($Handle、$x、$y、$Width、$Height、$True)
}
If($PSBoundParameters.ContainsKey('Passthru')){
$Rectangle=新对象RECT
$Return=[Window]::GetWindowRect($Handle,[ref]$Rectangle)
如果($返回){
$Height=$Rectangle.Bottom-$Rectangle.Top
$Width=$Rectangle.Right-$Rectangle.Left
$Size=新对象System.Management.Automation.Host.Size-参数列表$Width,$Height
$TopLeft=新对象System.Management.Automation.Host.Coordinates-ArgumentList$Rectangle.Left、$Rectangle.Top
$BottomRight=新对象System.Management.Automation.Host.Coordinates-参数列表$Rectangle.Right、$Rectangle.Bottom
如果($Rectangle.Top-lt 0-和$Rectangle.LEft-lt 0){
写入警告“窗口已最小化!坐标将不准确。”
}
$Object=[pscustomobject]@{
ProcessName=$ProcessName
大小=$Size
TopLeft=$TopLeft
BottomRight=$BottomRight
}
$Object.PSTypeNames.insert(0,'System.Automation.WindowInfo')
$Object
}
}
}
}
}

对于第二种方法,您可以使用:

Add-Type -TypeDefinition @"
    using System;
    using System.Runtime.InteropServices;

    public class Win32SetWindow {
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
    }
"@

$ie = new-object -comobject InternetExplorer.Application;
$ie.visible = $true;
#$ie2 = $ie.Width = 200;
$ie.top = 15; $ie.width = 1180; $ie.height = 710; $ie.Left = 192;

     [Win32SetWindow]::SetForegroundWindow($ie.HWND) # <--

$ie.navigate('https://www.facebook.com/');
添加类型-TypeDefinition@”
使用制度;
使用System.Runtime.InteropServices;
公共类Win32SetWindow{
[DllImport(“user32.dll”)]
[返回:Marshallas(UnmanagedType.Bool)]
公共静态外部bool setforegroundindow(IntPtr hWnd);
}
"@
$ie=新对象-comobject InternetExplorer.Application;
$ie.visible=$true;
#$ie2=$ie.Width=200;
$ie.top=15$宽度=1180$即高度=710$即左=192;

[Win32SetWindow]::setforegroundindow($ie.HWND)#尝试使用下面的脚本

$ie=新对象-comobject InternetExplorer.Application
$ie.visible=$true
#$ie2=$ie.Width=200
$ie.top=100$宽度=500$即高度=500$即左=100

$ie.navigate(”http://google.com)你说它开始滞后是什么意思?鼠标光标一直在“思考”,我不能滚动或做任何事情。你应该看看你的IE为什么这样做。这不正常。我的有什么不同?我试过了,但不起作用。我得到“宽度”、“高度”、“左侧”的无效操作错误。并且在尝试执行[Win32SetWindow]时也会出现空值::setforegroughindow($ie.HWND)$ie.navigate(');这只是对代码应该是什么样子的提取。我编辑了我的帖子并粘贴了完整的代码。太棒了!谢谢