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

[玩转系统] GetProcessSample04 示例

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

GetProcessSample04 示例


此示例演示如何实现检索本地计算机上进程的 cmdlet。如果检索进程时发生错误,它会生成非终止错误。此 cmdlet 是 Windows PowerShell 2.0 提供的 Get-Process cmdlet 的简化版本。

如何使用 Visual Studio 构建示例

  1. 安装 Windows PowerShell 2.0 SDK 后,导航到 GetProcessSample04 文件夹。默认位置为 C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\GetProcessSample04。

  2. 双击解决方案 (.sln) 文件的图标。这将在 Visual Studio 中打开示例项目。

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

如何运行示例

  1. 创建以下模块文件夹:

    [user]\Documents\WindowsPowerShell\Modules\GetProcessSample04
  2. 将示例程序集复制到模块文件夹。

  3. 启动 Windows PowerShell。

  4. 运行以下命令将程序集加载到 Windows PowerShell 中:

    Import-Module getprossessample04
  5. 运行以下命令来运行 cmdlet:

    get-proc

要求

此示例需要 Windows PowerShell 2.0。

示范

该示例演示了以下内容。

  • 使用 Cmdlet 属性声明 cmdlet 类。

  • 使用 Parameter 属性声明 cmdlet 参数。

  • 指定参数的位置。

  • 指定参数从管道获取输入。输入可以从对象中获取,也可以从属性名称与参数名称相同的对象的属性中获取值。

  • 声明参数输入的验证属性。

  • 捕获非终止错误并将错误消息写入错误流。

例子

此示例演示如何创建处理非终止错误并将错误消息写入错误流的 cmdlet。

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 to act on.
       /// </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,
         ValueFromPipeline = true,
         ValueFromPipelineByPropertyName = true)]
      [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 processes to the pipeline.
      /// </summary>
      protected override void ProcessRecord()
      {
          // If no process names are passed to cmdlet, get all
          // processes.
          if (this.processNames == null)
          {
              WriteObject(Process.GetProcesses(), true);
          }
          else
          {
              // If process names are passed to the cmdlet, get and write
              // the associated processes.
              // If a non-terminating error occurs while retrieving processes,
              // call the WriteError method to send an error record to the
              // error stream.
              foreach (string name in this.processNames)
              {
                  Process[] processes;

                  try
                  {
                      processes = Process.GetProcessesByName(name);
                  }
                  catch (InvalidOperationException ex)
                  {
                      WriteError(new ErrorRecord(
                         ex,
                         "UnableToAccessProcessByName",
                         ErrorCategory.InvalidOperation,
                         name));
                      continue;
                  }

                  WriteObject(processes, true);
              } // foreach (string name...
          } // else
      } // ProcessRecord

      #endregion Overrides
    } // End GetProcCommand class.

   #endregion GetProcCommand
}

参见

  • 编写 Windows PowerShell Cmdlet

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

取消回复欢迎 发表评论:

关灯