GetProcessSample04 サンプル
このサンプルでは、ローカル コンピューター上のプロセスを取得するコマンドレットを実装する方法を示します。 プロセスの取得中にエラーが発生すると、終了しないエラーが生成されます。 このコマンドレットは、Windows PowerShell 2.0 によって提供される Get-Process
コマンドレットの簡略化されたバージョンです。
Visual Studio を使用してサンプルをビルドする方法
Windows PowerShell 2.0 SDK がインストールされたら、GetProcessSample04 フォルダーに移動します。 既定の場所は
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\GetProcessSample04
です。ソリューション (.sln) ファイルのアイコンをダブルクリックします。 Visual Studio でサンプル プロジェクトが開きます。
[ビルド] メニューの [ソリューションのビルド] を選択して、既定の
\bin
フォルダーまたは\bin\debug
フォルダーにサンプルのライブラリをビルドします。
サンプルを実行する方法
次のモジュール フォルダーを作成します。
[user]\Documents\WindowsPowerShell\Modules\GetProcessSample04
サンプル アセンブリをモジュール フォルダーにコピーします。
Windows PowerShell を起動します。
次のコマンドを実行して、アセンブリを Windows PowerShell に読み込みます。
Import-Module getprossessample04
次のコマンドを実行してコマンドレットを実行します。
Get-Proc
必要条件
このサンプルには、Windows PowerShell 2.0 が必要です。
対象
このサンプルでは、次の例を示します。
コマンドレット属性を使用してコマンドレット クラスを宣言する。
Parameter 属性を使用してコマンドレット パラメーターを宣言する。
パラメーターの位置を指定します。
パラメーターがパイプラインから入力を受け取っていることを指定します。 入力は、パラメーター名と同じプロパティ名を持つオブジェクトのプロパティから、オブジェクトまたは値から取得できます。
パラメーター入力の検証属性の宣言。
終了しないエラーをトラップし、エラー ストリームにエラー メッセージを書き込みます。
例
このサンプルでは、終了しないエラーを処理し、エラー ストリームにエラー メッセージを書き込むコマンドレットを作成する方法を示します。
namespace Microsoft.Samples.PowerShell.Commands
{
using System;
using System.Diagnostics;
using System.Management.Automation; // Windows PowerShell namespace.
#region GetProcCommand
/// <summary>
/// This class implements the Get-Proc cmdlet.
/// </summary>
[Cmdlet(VerbsCommon.Get, "Proc")]
public class GetProcCommand : Cmdlet
{
#region Parameters
/// <summary>
/// The names of the processes to act on.
/// </summary>
private string[] processNames;
/// <summary>
/// Gets or sets the list of process names on
/// which the Get-Proc cmdlet will work.
/// </summary>
[Parameter(
Position = 0,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true)]
[ValidateNotNullOrEmpty]
public string[] Name
{
get { return this.processNames; }
set { this.processNames = value; }
}
#endregion Parameters
#region Cmdlet Overrides
/// <summary>
/// The ProcessRecord method calls the Process.GetProcesses
/// method to retrieve the processes specified by the Name
/// parameter. Then, the WriteObject method writes the
/// associated processes to the pipeline.
/// </summary>
protected override void ProcessRecord()
{
// If no process names are passed to cmdlet, get all
// processes.
if (this.processNames == null)
{
WriteObject(Process.GetProcesses(), true);
}
else
{
// If process names are passed to the cmdlet, get and write
// the associated processes.
// If a non-terminating error occurs while retrieving processes,
// call the WriteError method to send an error record to the
// error stream.
foreach (string name in this.processNames)
{
Process[] processes;
try
{
processes = Process.GetProcessesByName(name);
}
catch (InvalidOperationException ex)
{
WriteError(new ErrorRecord(
ex,
"UnableToAccessProcessByName",
ErrorCategory.InvalidOperation,
name));
continue;
}
WriteObject(processes, true);
} // foreach (string name...
} // else
} // ProcessRecord
#endregion Overrides
} // End GetProcCommand class.
#endregion GetProcCommand
}
こちらもご覧ください
PowerShell