次の方法で共有


GetProcessSample01 サンプル

このサンプルでは、ローカル コンピューター上のプロセスを取得するコマンドレットを実装する方法を示します。 このコマンドレットは、Windows PowerShell 2.0 によって提供される Get-Process コマンドレットの簡略化されたバージョンです。

Visual Studio を使用してサンプルをビルドする方法

  1. Windows PowerShell 2.0 SDK がインストールされたら、GetProcessSample01 フォルダーに移動します。 既定の場所は C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\GetProcessSample01です。

  2. ソリューション (.sln) ファイルのアイコンをダブルクリックします。 これにより、Microsoft Visual Studio でサンプル プロジェクトが開きます。

  3. [ビルド] メニューの [ソリューションのビルド] を選択して、既定の \bin フォルダーまたは \bin\debug フォルダーにサンプルのライブラリをビルドします。

サンプルを実行する方法

  1. コマンド プロンプト ウィンドウを開きます。

  2. サンプル .dll ファイルが含まれているディレクトリに移動します。

  3. installutil "GetProcessSample01.dll"を実行します。

  4. Windows PowerShell を起動します。

  5. 次のコマンドを実行して、スナップインをシェルに追加します。

    Add-PSSnapin GetProcPSSnapIn01

  6. 次のコマンドを入力してコマンドレットを実行します。 Get-Proc

    Get-Proc

    これは、次の手順に従った結果のサンプル出力です。

    Id              Name            State      HasMoreData     Location             Command
    --              ----            -----      -----------     --------             -------
    1               26932870-d3b... NotStarted False                                 Write-Host "A f...
    
    
    Set-Content $Env:TEMP\test.txt "This is a test file"
    
    A file was created in the TEMP directory
    

必要条件

このサンプルには、Windows PowerShell 1.0 以降が必要です。

対象

このサンプルでは、次の例を示します。

  • 基本的なサンプル コマンドレットの作成。

  • コマンドレット属性を使用してコマンドレット クラスを定義する。

  • Windows PowerShell 1.0 と Windows PowerShell 2.0 の両方で動作するスナップインを作成する。 以降のサンプルでは、スナップインではなくモジュールを使用するため、Windows PowerShell 2.0 が必要です。

このサンプルでは、単純なコマンドレットとそのスナップインを作成する方法を示します。

using System;
using System.Diagnostics;
using System.Management.Automation;             //Windows PowerShell namespace
using System.ComponentModel;

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 a 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
}

こちらもご覧ください