次のように、powershell で com オブジェクトを作成します。
$application = new-object -ComObject "word.application"
起動された MS Word インスタンスの PID (またはその他の一意の識別子) を取得する方法はありますか?
たとえば、パスワードを要求するモーダル ダイアログによってプログラムがブロックされているかどうかを確認したいのですが、PowerShell 内では実行できません。
次のように、powershell で com オブジェクトを作成します。
$application = new-object -ComObject "word.application"
起動された MS Word インスタンスの PID (またはその他の一意の識別子) を取得する方法はありますか?
たとえば、パスワードを要求するモーダル ダイアログによってプログラムがブロックされているかどうかを確認したいのですが、PowerShell 内では実行できません。
わかりました。方法はわかりました。Windows API を呼び出す必要があります。秘訣は、Word ではなく Excel と PowerPoint で公開されている HWND を取得することです。これを取得する唯一の方法は、アプリケーション ウィンドウの名前を一意の名前に変更し、「FindWindow」を使用して検索することです。次に、「GetWindowThreadProcessId」関数を使用して PID を取得できます。
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public static class Win32Api
{
[System.Runtime.InteropServices.DllImportAttribute( "User32.dll", EntryPoint = "GetWindowThreadProcessId" )]
public static extern int GetWindowThreadProcessId ( [System.Runtime.InteropServices.InAttribute()] System.IntPtr hWnd, out int lpdwProcessId );
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
}
"@
$application = new-object -ComObject "word.application"
# word does not expose its HWND, so get it this way
$caption = [guid]::NewGuid()
$application.Caption = $caption
$HWND = [Win32Api]::FindWindow( "OpusApp", $caption )
# print pid
$myPid = [IntPtr]::Zero
[Win32Api]::GetWindowThreadProcessId( $HWND, [ref] $myPid );
"PID=" + $myPid | write-host
あなたは使用できるかもしれません
get-process -InputObject <Process[]>