此示例演示如何实现检索本地计算机上的进程的 cmdlet。 此 cmdlet 是 Windows PowerShell 2.0 提供的 Get-Process
cmdlet 的简化版本。
如何使用 Visual Studio 生成示例
安装 Windows PowerShell 2.0 SDK 后,导航到 GetProcessSample01 文件夹。 默认位置为
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\GetProcessSample01
。双击解决方案(.sln)文件的图标。 这将在 Visual Studio 中打开示例项目Microsoft。
在 生成 菜单中,选择 “生成解决方案”,以在默认
\bin
或\bin\debug
文件夹中为示例生成库。
如何运行示例
打开命令提示符窗口。
导航到包含示例 .dll 文件的目录。
运行
installutil "GetProcessSample01.dll"
。启动 Windows PowerShell。
运行以下命令,将管理单元添加到 shell。
Add-PSSnapin GetProcPSSnapIn01
输入以下命令以运行 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
}