powershell - 如何通过Powershell中的进程获取所有窗口句柄?

标签 powershell

我有这个脚本

Get-Process | Where-Object {$_.MainWindowTitle -like "*total*"}  

产生这些信息
Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName                                                                     
-------  ------    -----      ----- -----   ------     -- -----------                                                                     
    362      23    19432      32744   324     3.86   6880 TotalCmd64                                                                      

所以我有进程ID。
*Total*应用程序有许多自己打开的窗口。

问题

我如何迭代(使用 powershell)它的所有窗口(这样我才能得到它们的窗口句柄)?

注意:我的目标是什么?

enter image description here :

在 Visual Studio 中寻找(例如):我正在运行应用程序。
但应用程序有自己的打开窗口。

我希望子窗口是 TOPMOsT。我已经有了在最上面制作窗口的脚本。但我需要它的句柄号。

最佳答案

首先,您应该查看 WASP,看看它是否适合您的需求:http://wasp.codeplex.com/

其次,我修改了这里的代码 http://social.technet.microsoft.com/Forums/windowsserver/en-US/c3cd3982-ffc5-4c17-98fc-a09c555e121c/get-all-child-window-titles?forum=winserverpowershell

创建一个将 MainWindowHandle 作为输入的函数,并将返回一个带有子句柄 ID 的对象(它还将列出任何窗口标题,如果有的话)。

我希望这些方法之一能给你你需要的东西:)

function Get-ChildWindow{
[CmdletBinding()]
param (
    [Parameter(ValueFromPipeline = $true, ValueFromPipelinebyPropertyName = $true)]
    [ValidateNotNullorEmpty()]
    [System.IntPtr]$MainWindowHandle
)

BEGIN{
    function Get-WindowName($hwnd) {
        $len = [apifuncs]::GetWindowTextLength($hwnd)
        if($len -gt 0){
            $sb = New-Object text.stringbuilder -ArgumentList ($len + 1)
            $rtnlen = [apifuncs]::GetWindowText($hwnd,$sb,$sb.Capacity)
            $sb.tostring()
        }
    }

    if (("APIFuncs" -as [type]) -eq $null){
        Add-Type  @"
        using System;
        using System.Runtime.InteropServices;
        using System.Collections.Generic;
        using System.Text;
        public class APIFuncs
          {
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern int GetWindowText(IntPtr hwnd,StringBuilder lpString, int cch);

            [DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
            public static extern IntPtr GetForegroundWindow();

            [DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
            public static extern Int32 GetWindowThreadProcessId(IntPtr hWnd,out Int32 lpdwProcessId);

            [DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
            public static extern Int32 GetWindowTextLength(IntPtr hWnd);

            [DllImport("user32")]
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);
            public static List<IntPtr> GetChildWindows(IntPtr parent)
            {
               List<IntPtr> result = new List<IntPtr>();
               GCHandle listHandle = GCHandle.Alloc(result);
               try
               {
                   EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
                   EnumChildWindows(parent, childProc,GCHandle.ToIntPtr(listHandle));
               }
               finally
               {
                   if (listHandle.IsAllocated)
                       listHandle.Free();
               }
               return result;
           }
            private static bool EnumWindow(IntPtr handle, IntPtr pointer)
           {
               GCHandle gch = GCHandle.FromIntPtr(pointer);
               List<IntPtr> list = gch.Target as List<IntPtr>;
               if (list == null)
               {
                   throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
               }
               list.Add(handle);
               //  You can modify this to check to see if you want to cancel the operation, then return a null here
               return true;
           }
            public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);
           }
"@
        }
}

PROCESS{
    foreach ($child in ([apifuncs]::GetChildWindows($MainWindowHandle))){
        Write-Output (,([PSCustomObject] @{
            MainWindowHandle = $MainWindowHandle
            ChildId = $child
            ChildTitle = (Get-WindowName($child))
        }))
    }
}
}

您可以直接从 Get-Process 的结果管道它,如下所示:
Get-Process | Where-Object {$_.ProcessName -eq 'OUTLOOK'} | Get-ChildWindow

关于powershell - 如何通过Powershell中的进程获取所有窗口句柄?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25369285/

相关文章:

unit-testing - dotnet test 仅测试解决方案上的项目

powershell - 如何在没有管理员权限的情况下运行 powershell 脚本?

Powershell 排序命令未按预期工作

powershell - 调用自定义类的静态方法

powershell - 向 csv 文件添加条目会引发错误 :does not contain a method named 'op_Addition'

arrays - 将 PowerShell 数组传递给函数时如何展开它

azure - 如何使用 Get-AzWebApp 命令列出所有自定义域和订阅名称

powershell - 在 Azure 上每小时运行一次 Powershell 脚本

powershell - Active Directory Powershell - 获取 OU 详细信息

json - 如何在Powershell中存储多级JSON