首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将url传递给移动浏览器窗口的powershell脚本

将url传递给移动浏览器窗口的powershell脚本
EN

Stack Overflow用户
提问于 2018-09-11 01:36:07
回答 2查看 1K关注 0票数 1

我有一个powershell脚本,它在set-window.ps1的帮助下打开一个具有特定高度、宽度和屏幕上确切位置的firefox浏览器。

但我也希望浏览器在特定的url中打开。我该怎么做呢?

我的脚本:

代码语言:javascript
复制
   . C:/Users/user/Desktop/Set-Window.ps1
    Start-Process -FilePath 'C:\Program Files\Mozilla Firefox\firefox.exe'
    Start-Sleep -Seconds 0
    Set-Window -ProcessName firefox -x 160 -y 50 -Width 1210 -Height 690 -Passthru

和Set-Window.ps1

代码语言:javascript
复制
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            
                }
            }
        }
    }
}

要使用iexplorer执行相同的操作,我会这样做

代码语言:javascript
复制
$ie = new-object -comobject InternetExplorer.Application;
$ie.visible = $true;
#$ie2 = $ie.Width = 200;
$ie.top = 50; $ie.width = 1000; $ie.height = 600; $ie.Left = 50;
$ie.navigate('https://mail.google.com/mail/?tab=wm#inbox');

但我不知道如何将这两者结合起来。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-11 02:09:17

只需使用Start-Process cmdlet及其ArgumentList属性传递Firefox.exe的URL即可。如下所示:

代码语言:javascript
复制
    . C:/Users/user/Desktop/Set-Window.ps1
    Start-Process -FilePath 'C:\Program Files\Mozilla Firefox\firefox.exe' -ArgumentList 'https://mail.google.com/mail/?tab=wm#inbox'
    Start-Sleep -Seconds 0
    Set-Window -ProcessName firefox -x 160 -y 50 -Width 1210 -Height 690 -Passthru
票数 0
EN

Stack Overflow用户

发布于 2018-09-11 01:47:29

代码语言:javascript
复制
Start-Process -FilePath 'C:\Program Files\Mozilla Firefox\firefox.exe' -ArgumentList '-url https://mail.google.com/mail/?tab=wm#inbox'

# '-url' argument is optional and can be omitted : https://developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options#Browser

Start-Process -FilePath 'C:\Program Files\Mozilla Firefox\firefox.exe' -ArgumentList 'https://mail.google.com/mail/?tab=wm#inbox'
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52262847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档