【PowerShell】Start-Processで起動したプロセスを操作する【AutoITX】
海外の記事で面白いものを見つけたのでメモ
Start-Processで起動したプロセスを掴む
Start-Processコマンドレットに-PassThruオプションをつけるとStartしたProcessがそのまま返ります。
1 |
$Process = Start-Process notepad.exe -PassThru |
この$ProcessはGet-Processで掴むのと同じObjectのようです。
MemberにMainWindowHandle,MainWindowTitleが含まれています。
ということは、このProcessを外側から操作できます。
因みにGet-Memberした結果がこちら :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
$Process|Get-Member
TypeName: System.Diagnostics.Process Name MemberType Definition ---- ---------- ---------- Handles AliasProperty Handles = Handlecount Name AliasProperty Name = ProcessName NPM AliasProperty NPM = NonpagedSystemMemorySize PM AliasProperty PM = PagedMemorySize VM AliasProperty VM = VirtualMemorySize WS AliasProperty WS = WorkingSet Disposed Event System.EventHandler Disposed(System.Object, System.EventArgs) ErrorDataReceived Event System.Diagnostics.DataReceivedEventHandler ErrorDataReceived(System.Object, System.Diagnostics.DataReceivedEventArgs) Exited Event System.EventHandler Exited(System.Object, System.EventArgs) OutputDataReceived Event System.Diagnostics.DataReceivedEventHandler OutputDataReceived(System.Object, System.Diagnostics.DataReceivedEventArgs) BeginErrorReadLine Method void BeginErrorReadLine() BeginOutputReadLine Method void BeginOutputReadLine() CancelErrorRead Method void CancelErrorRead() CancelOutputRead Method void CancelOutputRead() Close Method void Close() CloseMainWindow Method bool CloseMainWindow() CreateObjRef Method System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType) Dispose Method void Dispose(), void IDisposable.Dispose() Equals Method bool Equals(System.Object obj) GetHashCode Method int GetHashCode() GetLifetimeService Method System.Object GetLifetimeService() GetType Method type GetType() InitializeLifetimeService Method System.Object InitializeLifetimeService() Kill Method void Kill() Refresh Method void Refresh() Start Method bool Start() ToString Method string ToString() WaitForExit Method bool WaitForExit(int milliseconds), void WaitForExit() WaitForInputIdle Method bool WaitForInputIdle(int milliseconds), bool WaitForInputIdle() __NounName NoteProperty System.String __NounName=Process BasePriority Property int BasePriority {get;} Container Property System.ComponentModel.IContainer Container {get;} EnableRaisingEvents Property bool EnableRaisingEvents {get;set;} ExitCode Property int ExitCode {get;} ExitTime Property datetime ExitTime {get;} Handle Property System.IntPtr Handle {get;} HandleCount Property int HandleCount {get;} HasExited Property bool HasExited {get;} Id Property int Id {get;} MachineName Property string MachineName {get;} MainModule Property System.Diagnostics.ProcessModule MainModule {get;} MainWindowHandle Property System.IntPtr MainWindowHandle {get;} MainWindowTitle Property string MainWindowTitle {get;} MaxWorkingSet Property System.IntPtr MaxWorkingSet {get;set;} MinWorkingSet Property System.IntPtr MinWorkingSet {get;set;} Modules Property System.Diagnostics.ProcessModuleCollection Modules {get;} NonpagedSystemMemorySize Property int NonpagedSystemMemorySize {get;} NonpagedSystemMemorySize64 Property long NonpagedSystemMemorySize64 {get;} PagedMemorySize Property int PagedMemorySize {get;} PagedMemorySize64 Property long PagedMemorySize64 {get;} PagedSystemMemorySize Property int PagedSystemMemorySize {get;} PagedSystemMemorySize64 Property long PagedSystemMemorySize64 {get;} PeakPagedMemorySize Property int PeakPagedMemorySize {get;} PeakPagedMemorySize64 Property long PeakPagedMemorySize64 {get;} PeakVirtualMemorySize Property int PeakVirtualMemorySize {get;} PeakVirtualMemorySize64 Property long PeakVirtualMemorySize64 {get;} PeakWorkingSet Property int PeakWorkingSet {get;} PeakWorkingSet64 Property long PeakWorkingSet64 {get;} PriorityBoostEnabled Property bool PriorityBoostEnabled {get;set;} PriorityClass Property System.Diagnostics.ProcessPriorityClass PriorityClass {get;set;} PrivateMemorySize Property int PrivateMemorySize {get;} PrivateMemorySize64 Property long PrivateMemorySize64 {get;} PrivilegedProcessorTime Property timespan PrivilegedProcessorTime {get;} ProcessName Property string ProcessName {get;} ProcessorAffinity Property System.IntPtr ProcessorAffinity {get;set;} Responding Property bool Responding {get;} SessionId Property int SessionId {get;} Site Property System.ComponentModel.ISite Site {get;set;} StandardError Property System.IO.StreamReader StandardError {get;} StandardInput Property System.IO.StreamWriter StandardInput {get;} StandardOutput Property System.IO.StreamReader StandardOutput {get;} StartInfo Property System.Diagnostics.ProcessStartInfo StartInfo {get;set;} StartTime Property datetime StartTime {get;} SynchronizingObject Property System.ComponentModel.ISynchronizeInvoke SynchronizingObject {get;set;} Threads Property System.Diagnostics.ProcessThreadCollection Threads {get;} TotalProcessorTime Property timespan TotalProcessorTime {get;} UserProcessorTime Property timespan UserProcessorTime {get;} VirtualMemorySize Property int VirtualMemorySize {get;} VirtualMemorySize64 Property long VirtualMemorySize64 {get;} WorkingSet Property int WorkingSet {get;} WorkingSet64 Property long WorkingSet64 {get;} PSConfiguration PropertySet PSConfiguration {Name, Id, PriorityClass, FileVersion} PSResources PropertySet PSResources {Name, Id, Handlecount, WorkingSet, NonPagedMemorySize, PagedMemorySize, PrivateMemorySize, VirtualMemorySize, Threads.Count, TotalProcessorTime} Company ScriptProperty System.Object Company {get=$this.Mainmodule.FileVersionInfo.CompanyName;} CPU ScriptProperty System.Object CPU {get=$this.TotalProcessorTime.TotalSeconds;} Description ScriptProperty System.Object Description {get=$this.Mainmodule.FileVersionInfo.FileDescription;} FileVersion ScriptProperty System.Object FileVersion {get=$this.Mainmodule.FileVersionInfo.FileVersion;} Path ScriptProperty System.Object Path {get=$this.Mainmodule.FileName;} Product ScriptProperty System.Object Product {get=$this.Mainmodule.FileVersionInfo.ProductName;} ProductVersion ScriptProperty System.Object ProductVersion {get=$this.Mainmodule.FileVersionInfo.ProductVersion;} |
ProcessをAutoITXで操作する
ウィンドウタイトルとウィンドウハンドルがわかれば以前記事に書いたAutoITXで操作できます。
AutoITXのPowerShell Tutorialをこれで書き換えるとこんな感じになります。
メモ帳など言語環境でウィンドウタイトルが変わるようなアプリの操作に便利そうです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# Import the AutoIt PowerShell module Import-Module AutoItX # Start notepad.exe $Process = Start-Process notepad.exe -PassThru $Process|Get-Member # Wait for an untitled notepad window and get the handle $notepadTitle = $Process.MainWindowTitle Wait-AU3Win -Title $notepadTitle $winHandle = $Process.MainWindowHandle # Activate the window Show-AU3WinActivate -WinHandle $winHandle # Get the handle of the notepad text control for reliable operations $controlHandle = Get-AU3ControlHandle -WinHandle $winhandle -Control "Edit1" # Change the edit control Set-AU3ControlText -ControlHandle $controlHandle -NewText "Hello! This is being controlled by AutoIt and PowerShell!" -WinHandle $winHandle # Send some keystrokes to the edit control Send-AU3ControlKey -ControlHandle $controlHandle -Key "{ENTER}simulate key strokes - line 1" -WinHandle $winHandle Send-AU3ControlKey -ControlHandle $controlHandle -Key "{ENTER}simulate key strokes - line 2" -WinHandle $winHandle Send-AU3ControlKey -ControlHandle $controlHandle -Key "{ENTER}{ENTER}" -WinHandle $winHandle view raw |
“【PowerShell】Start-Processで起動したプロセスを操作する【AutoITX】” に対して1件のコメントがあります。