GetProcessSample02 サンプル
このサンプルでは、ローカル コンピューター上のプロセスを取得するコマンドレットを記述する方法を示します。 取得する Name
プロセスを指定するために使用できるパラメーターが提供されます。 このコマンドレットは、Windows PowerShell Get-Process
2.0 で提供されるコマンドレットの簡略化されたバージョンです。
を使用してサンプルをビルドするVisual Studio。
Windows PowerShell 2.0 SDK がインストールされている場合は、GetProcessSample02 フォルダーに移動します。 既定の場所は C:\Program Files (x86)\Microsoft SDK\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\GetProcessSample02 です。
ソリューション (.sln) ファイルのアイコンをダブルクリックします。 これにより、サンプル プロジェクトが Visual Studio。
[ビルド] メニューで、 [ソリューションのビルド] を選択します。
サンプルのライブラリは、既定の \bin フォルダーまたは \bin\debug フォルダーにビルドされます。
サンプルを実行する方法
次のモジュール フォルダーを作成します。
[user]/documents/windowspowershell/modules/GetProcessSample02
サンプル アセンブリをモジュール フォルダーにコピーします。
Windows PowerShell を起動します。
次のコマンドを実行して、アセンブリを読み込Windows PowerShell。
import-module getprossessample02
次のコマンドを実行してコマンドレットを実行します。
get-proc
必要条件
このサンプルでは、Windows PowerShell 2.0 が必要です。
対象
このサンプルでは、次の例を示します。
コマンドレット属性を使用したコマンドレット クラスの宣言。
Parameter 属性を使用したコマンドレット パラメーターの宣言。
パラメーターの位置を指定する。
パラメーター入力の検証属性の宣言。
例
このサンプルでは、 パラメーターを含む Get-Procコマンドレットの実装を示 Name
します。
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 retrieved by the cmdlet.
/// </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)]
[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 process objects to the pipeline.
/// </summary>
protected override void ProcessRecord()
{
// If no process names are passed to the cmdlet, get all
// processes.
if (this.processNames == null)
{
WriteObject(Process.GetProcesses(), true);
}
else
{
// If process names are passed to cmdlet, get and write
// the associated processes.
foreach (string name in this.processNames)
{
WriteObject(Process.GetProcessesByName(name), true);
}
} // End if (processNames...).
} // End ProcessRecord.
#endregion Cmdlet Overrides
} // End GetProcCommand class.
#endregion GetProcCommand
}
参照
Writing a Windows PowerShell Cmdlet (Windows PowerShell コマンドレットの記述)
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示