前景浏览器窗口

时间:2019-06-21 09:29:47

标签: powershell

我正在尝试编写一个脚本,该脚本可以执行一些基本的键盘和鼠标事件,然后“打开”当前的Chrome窗口,然后从那里继续。

唯一的问题是我找不到将Chrome窗口置于前台的方法。该代码可用于简单的事情,例如记事本,但无论我尝试输入多少名称,它都无法找到Chrome。

我环顾四周,找不到解决此问题的方法。

我当前的代码:

Add-Type @"
using System;
using System.Runtime.InteropServices;
public class Tricks {
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool SetForegroundWindow(IntPtr hWnd);
}
"@
sleep -sec 2
$h = (Get-Process Chrome).MainWindowHandle
[void] [Tricks]::SetForegroundWindow($h)

如果我使用Get-Process Chrome,则会引发此错误:

Cannot convert argument "hWnd", with value: "System.Object[]", for
"SetForegroundWindow" to type "System.IntPtr": "Cannot convert the
"System.Object[]" value of type "System.Object[]" to type "System.IntPtr"."
At line:12 char:1
+ [void] [Tricks]::SetForegroundWindow($h)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

如果我使用其他任何东西,都会引发此错误:

Get-Process : Cannot find a process with the name "Google Chrome". Verify the
process name and call the cmdlet again.
At line:11 char:7
+ $h = (Get-Process "Google Chrome").MainWindowHandle
+       ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Google Chrome:String) [Get-Process], ProcessCommandException
    + FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand

以及先前的错误。

但是,如果我使用“记事本”之类的东西,则不会引发任何错误,并且程序可以按预期工作。

编辑:

我什至设法找到了该脚本的简化版本:

(New-Object -ComObject WScript.Shell).AppActivate((get-process Notepad).MainWindowTitle)

但是,像以前一样,它可以与记事本之类的东西一起使用,但是当我尝试使用“ Chrome”时,我得到了:

Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))
At line:1 char:1
+ (New-Object -ComObject WScript.Shell).AppActivate((get-process Chrome ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

Firefox也是如此。

1 个答案:

答案 0 :(得分:0)

我还没有Chrome,但是有了Firefox(您说的是一样的

PS C:\Users\peter> Get-Process firefox

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    503      46   101704     131672      48.55  11500   1 firefox
    906      61    36184      65604       0.72  25312   1 firefox
    907      74    74264     122448       9.81  35828   1 firefox
   1730     116   172892     241800      36.11  36044   1 firefox
    697      59    73552     126796      15.77  39864   1 firefox
    520      33    20632      45052       0.25  41996   1 firefox


PS C:\Users\peter> Get-Process notepad

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    230      13     2812      14044       0.09  50796   1 notepad

即,有多个Firefox进程。

不幸的是,我不清楚如何挑选“正确的人”。试试这个:

 Get-Process firefox | %{ (New-Object -ComObject WScript.Shell).AppActivate($_.MainWindowTitle)}

只是试图将所有进程置于最前面。它对我有用,但效率不高-我相信您可以改善。