【问题标题】:Open firefox from powershell从 powershell 打开 Firefox
【发布时间】:2018-09-06 18:23:37
【问题描述】:

我可以打开 Internet Explorer 这样做,是否可以用 firefox 做同样的事情?

$ie = new-object -comobject InternetExplorer.Application;

$ie.visible = $true;
#$ie2 = $ie.Width = 200;

$ie.top = 0; $ie.width = 1000; $ie.height = 600 ; $ie.Left = 200;

$ie.navigate('https://google.com');

【问题讨论】:

    标签: powershell firefox


    【解决方案1】:

    查看此讨论并回答

    Setting window size and position in PowerShell 5 and 6

    # Call Firefox and set to window position on its process
    Start-Process -FilePath 'C:\Program Files\Mozilla Firefox\firefox.exe'
    Start-Sleep -Seconds 2
    Set-Window -ProcessName firefox -x 1 -y 1 -Width 615 -Height 345 -Passthru
    

    【讨论】:

    • 发布前已在不同机器上进行了全面测试。所以,我知道它有效。
    • 好的!我让它工作了。我需要更改我的 powerShell 的 ExecutionPolicy。谢谢!
    • 我还需要先加载 Set-Window 脚本
    • 不用担心,许多人也被其他努力的执行策略设置所困扰,是的,所有模块,功能必须加载(如果无法自动加载)才能工作为你。因此,如果这是您可以使用的答案。也将其标记为已回答,以供参考,因为其他人可能会这样做。答案几乎适用于任何正在运行的进程 GUI 应用程序,而不仅仅是 Firefox。至少来自我使用的测试库。
    【解决方案2】:

    Firefox 有宽度和高度的命令行参数,但我找不到任何窗口位置。

    这适用于 61.0.2。您可能需要根据您的使用情况将参数修改为FindWindow()

    请注意,这不是有史以来最强大的代码,但它可能适合您的需求。

    & "C:\Program Files\Mozilla Firefox\firefox.exe" -width 1000 -height 600 https://google.com
    
    $Assem = ( 
        "System.Runtime.InteropServices"
        ) 
    
    $Source = @" 
    using System;
    using System.Runtime.InteropServices; // For the P/Invoke signatures.
    
    namespace Code42 {
    
        public static class PositionWindowDemo
        {
    
            // P/Invoke declarations.
    
            [DllImport("user32.dll", SetLastError = true)]
            static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
            [DllImport("user32.dll", SetLastError = true)]
            static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
    
            const uint SWP_NOSIZE = 0x0001;
            const uint SWP_NOZORDER = 0x0004;
    
            public static void MoveWindow(string name)
            {
                // Find (the first-in-Z-order) Notepad window.
                IntPtr hWnd = FindWindow(null, name);
    
                // If found, position it.
                if (hWnd != IntPtr.Zero)
                {
                    // Move the window to (0,0) without changing its size or position
                    // in the Z order.
                    SetWindowPos(hWnd, IntPtr.Zero, 0, 0, 0, 200, SWP_NOSIZE | SWP_NOZORDER);
                }
            }
    
        }
    
    }
    "@ 
    
    Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp  
    
    [Code42.PositionWindowDemo]::MoveWindow("Google - Mozilla Firefox")
    

    来源:

    【讨论】:

    • 好的,谢谢!但确实需要设置窗口位置
    • 再次感谢!它不会在给定位置打开,它会在您关闭 Firefox 的相同位置打开
    猜你喜欢
    • 1970-01-01
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    相关资源
    最近更新 更多