共用方式為


GetProcessSample01 範例

此範例示範如何實作 Cmdlet,以擷取本機電腦上的進程。 此 Cmdlet 是 Windows PowerShell 2.0 所提供的簡化 Get-Process Cmdlet 版本。

如何使用 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) 檔案的圖示。 這會在 Visual Studio 中開啟範例專案Microsoft。

  3. 在 [建置] 功能表中,選取 [建置方案],以在預設 \bin\bin\debug 資料夾中建置範例的連結庫。

如何執行範例

  1. 開啟命令提示字元視窗。

  2. 流覽至包含範例 .dll 檔案的目錄。

  3. 執行 installutil "GetProcessSample01.dll"

  4. 啟動 Windows PowerShell。

  5. 執行下列命令,將嵌入式管理單元新增至殼層。

    Add-PSSnapin GetProcPSSnapIn01

  6. 輸入下列命令以執行 Cmdlet。 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 或更新版本。

演示

此範例示範下列專案。

  • 建立基本範例 Cmdlet。

  • 使用 Cmdlet 屬性定義 Cmdlet 類別。

  • 建立適用於 Windows PowerShell 1.0 和 Windows PowerShell 2.0 的嵌入式管理單元。 後續範例會使用模組,而不是嵌入式管理單元,因此需要 Windows PowerShell 2.0。

範例

此範例示範如何建立簡單的 Cmdlet 及其嵌入式管理單元。

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
}

另請參閱