当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] GetProcessSample01 示例

作者:精品下载站 日期:2024-12-14 02:48:44 浏览:14 分类:玩电脑

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) 文件的图标。这将在 Microsoft Visual Studio 中打开示例项目。

  3. 构建菜单中,选择构建解决方案在默认的\bin\bin\debug 文件夹。

如何运行示例

  1. 打开命令提示符窗口。

  2. 导航到包含示例 .dll 文件的目录。

  3. 运行installutil“GetProcessSample01.dll”

  4. 启动 Windows PowerShell。

  5. 运行以下命令将管理单元添加到 shell。

    Add-PSSnapin GetProcPSSnapIn01
  6. 输入以下命令来运行 cmdlet。 获取过程

    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
}

参见

  • 编写 Windows PowerShell Cmdlet

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯