GetProcessSample03 Sample
This sample shows how to implement a cmdlet that retrieves the processes on the local computer. It
provides a Name
parameter that can accept an object from the pipeline or a value from a property
of an object whose property name is the same as the parameter name. This cmdlet is a simplified
version of the Get-Process
cmdlet provided by Windows PowerShell 2.0.
How to build the sample using Visual Studio
With the Windows PowerShell 2.0 SDK installed, navigate to the GetProcessSample03 folder. The default location is
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\GetProcessSample03
.Double-click the icon for the solution (.sln) file. This opens the sample project in Visual Studio.
In the Build menu, select Build Solution to build the library for the sample in the default
\bin
or\bin\debug
folders.
How to run the sample
Create the following module folder:
[user]\Documents\WindowsPowerShell\Modules\GetProcessSample03
Copy the sample assembly to the module folder.
Start Windows PowerShell.
Run the following command to load the assembly into Windows PowerShell:
Import-Module getprossessample03
Run the following command to run the cmdlet:
get-proc
Requirements
This sample requires Windows PowerShell 2.0.
Demonstrates
This sample demonstrates the following.
Declaring a cmdlet class using the Cmdlet attribute.
Declaring a cmdlet parameter using the Parameter attribute.
Specifying the position of the parameter.
Specifying that the parameter takes input from the pipeline. The input can be taken from an object or a value from a property of an object whose property name is the same as the parameter name.
Declaring a validation attribute for the parameter input.
Example
This sample shows an implementation of the Get-Proc cmdlet that includes a Name
parameter that
accepts input from the pipeline.
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 retrieved by the cmdlet.
/// </summary>
private string[] processNames;
/// <summary>
/// Gets or sets the names of the
/// process that the cmdlet will retrieve.
/// </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 the 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.
foreach (string name in this.processNames)
{
WriteObject(Process.GetProcessesByName(name), true);
}
} // End if (processNames ...)
} // End ProcessRecord.
#endregion Overrides
} // End GetProcCommand.
#endregion GetProcCommand
}
See Also
PowerShell