私はこのスクリプトを持っています
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を使用して)反復するにはどうすればよいですか(ウィンドウハンドルを取得できるように)?
NB:私の目標は何ですか?
:
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を持つオブジェクトを返す関数を作成します(ウィンドウタイトルがある場合はそれも一覧表示します)。
これらの方法の1つがあなたに必要なものを与えることを願っています:)
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
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加