次のコードは、GetProc01 サンプル コマンドレットの実装を示しています。 このコマンドレットは、プロセスの取得の実際の作業を System.Diagnostics.Process.Getprocesses* メソッドに任せると簡略化されます。
注
Windows Vista および .NET Framework 3.0 ランタイム コンポーネント用の Microsoft Windows ソフトウェア開発キットを使用して、この Get-Proc コマンドレットの C# ソース ファイル (getproc01.cs) をダウンロードできます。 ダウンロード手順については、「Windows PowerShell をインストールして Windows PowerShell SDKをダウンロードする方法」を参照してください。 ダウンロードしたソース ファイルは、<PowerShell Samples> ディレクトリにあります。
コード サンプル
using System;
using System.Diagnostics;
using System.Management.Automation; //Windows PowerShell namespace
using System.ComponentModel;
// This sample shows how to create a simple cmdlet. To test this
// cmdlet, the snapin must be registered. First, run the command:
// installutil GetProcessSample01.dll
// Then run:
// Add-PSSnapin GetProcessSnapIn01
// After the snapin has been loaded, you can run:
// get-proc
namespace Microsoft.Samples.PowerShell.Commands
{
#region GetProcCommand
/// <summary>
/// This class implements the Get-Proc cmdlet.
/// </summary>
[Cmdlet(VerbsCommon.Get, "Proc")]
public class GetProcCommand : Cmdlet
{
#region Cmdlet Overrides
/// <summary>
/// The ProcessRecord method calls the Process.GetProcesses
/// method to retrieve the processes of the local computer.
/// Then, the WriteObject method writes the associated processes
/// to the pipeline.
/// </summary>
protected override void ProcessRecord()
{
// Retrieve the current processes.
Process[] processes = Process.GetProcesses();
// Write the processes to the pipeline to make them available
// to the next cmdlet. The second argument (true) tells Windows
// PowerShell to enumerate the array and to send one process
// object at a time to the pipeline.
WriteObject(processes, true);
}
#endregion Overrides
} //GetProcCommand
#endregion GetProcCommand
#region PowerShell snap-in
/// <summary>
/// Create this sample as an PowerShell snap-in
/// </summary>
[RunInstaller(true)]
public class GetProcPSSnapIn01 : PSSnapIn
{
/// <summary>
/// Create an instance of the GetProcPSSnapIn01
/// </summary>
public GetProcPSSnapIn01()
: base()
{
}
/// <summary>
/// Get a name for this PowerShell snap-in. This name will be used in registering
/// this PowerShell snap-in.
/// </summary>
public override string Name
{
get
{
return "GetProcPSSnapIn01";
}
}
/// <summary>
/// Vendor information for this PowerShell snap-in.
/// </summary>
public override string Vendor
{
get
{
return "Microsoft";
}
}
/// <summary>
/// Gets resource information for vendor. This is a string of format:
/// resourceBaseName,resourceName.
/// </summary>
public override string VendorResource
{
get
{
return "GetProcPSSnapIn01,Microsoft";
}
}
/// <summary>
/// Description of this PowerShell snap-in.
/// </summary>
public override string Description
{
get
{
return "This is a PowerShell snap-in that includes the get-proc cmdlet.";
}
}
}
#endregion PowerShell snap-in
}
こちらもご覧ください
Windows PowerShell SDK の
GitHub で Microsoft と共同作業する
このコンテンツのソースは GitHub にあります。そこで、issue や pull request を作成および確認することもできます。 詳細については、共同作成者ガイドを参照してください。
PowerShell