基本構文(13)
MSHが備えている目新しい特徴の1つに「ShouldProcess」があります。
「ShouldProcess」は、オペレーションの実行前に処理の検証を行う仕組みです。
「ShouldProcess」をサポートしているCmdletには、
- stop-process
- start-service
- restart-service
- set-service
- remove-variable
などがあります。
※他にもあるかもしれません。
「stop-process」で「ShouldProcess」を試してみましょう。
stop-process(通常)
プロセスを強制終了する
メモ帳を起動して以下を実行します。
MSH C:\> ps | where {$_.Name -like "*note*"} | stop-process
- メモ帳が終了します。
stop-process(whatif)
強制終了する対象プロセスの情報を出力する(1)
メモ帳を起動して以下を実行します。
MSH C:\> ps | where {$_.Name -like "*note*"} | stop-process -whatif What if: Performing operation "stop-process" on Target "notepad (2520)".
- stop-processの対象となるプロセスの情報を出力します。終了処理は行いません。
- メモ帳は起動したままです。
強制終了する対象プロセスの情報を出力する(2)
MSH C:\> ps | where {$_.handlecount -ge 500} | stop-process -whatif What if: Performing operation "stop-process" on Target "CCAPP (1848)". What if: Performing operation "stop-process" on Target "CSRSS (180)". What if: Performing operation "stop-process" on Target "IEXPLORE (1644)". What if: Performing operation "stop-process" on Target "msh (1084)". What if: Performing operation "stop-process" on Target "SERVICES (228)".
- 基本は(1)と同じですが、handlecountの範囲を指定しています。
stop-process(confirm)
プロセスを強制終了するかどうか1つずつ確認する
MSH C:\> ps | where {$_.handlecount -ge 500} | stop-process -confirm Confirm Are you sure you want to perform this action? Performing operation "stop-process" on Target "CCAPP (1848)". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): n Confirm Are you sure you want to perform this action? Performing operation "stop-process" on Target "CSRSS (180)". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): n Confirm Are you sure you want to perform this action? Performing operation "stop-process" on Target "IEXPLORE (1644)". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y Confirm Are you sure you want to perform this action? Performing operation "stop-process" on Target "msh (1084)". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): n Confirm Are you sure you want to perform this action? Performing operation "stop-process" on Target "SERVICES (228)". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): n
- IEXPLORE(IE)だけを強制終了しました。