88 lines
3.5 KiB
C#
88 lines
3.5 KiB
C#
// <copyright file="Runspace08.cs" company="Microsoft Corporation">
|
|
// Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
|
// </copyright>
|
|
// DISCLAIMER OF WARRANTY: The software is licensed “as-is.” You
|
|
// bear the risk of using it. Microsoft gives no express warranties,
|
|
// guarantees or conditions. You may have additional consumer rights
|
|
// under your local laws which this agreement cannot change. To the extent
|
|
// permitted under your local laws, Microsoft excludes the implied warranties
|
|
// of merchantability, fitness for a particular purpose and non-infringement.
|
|
|
|
namespace Microsoft.Samples.PowerShell.Runspaces
|
|
{
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Management.Automation;
|
|
using System.Management.Automation.Runspaces;
|
|
using PowerShell = System.Management.Automation.PowerShell;
|
|
|
|
/// <summary>
|
|
/// This class contains the Main entry point for this host application.
|
|
/// </summary>
|
|
internal class Runspace08
|
|
{
|
|
/// <summary>
|
|
/// This sample shows how to use a PowerShell object to run commands. The
|
|
/// PowerShell object builds a pipeline that include the get-process cmdlet,
|
|
/// which is then piped to the sort-object cmdlet. Parameters are added to the
|
|
/// sort-object cmdlet to sort the HandleCount property in descending order.
|
|
/// </summary>
|
|
/// <param name="args">Parameter is not used.</param>
|
|
/// <remarks>
|
|
/// This sample demonstrates:
|
|
/// 1. Creating a PowerShell object
|
|
/// 2. Adding individual commands to the PowerShell object.
|
|
/// 3. Adding parameters to the commands.
|
|
/// 4. Running the pipeline of the PowerShell object synchronously.
|
|
/// 5. Working with PSObject objects to extract properties
|
|
/// from the objects returned by the commands.
|
|
/// </remarks>
|
|
private static void Main(string[] args)
|
|
{
|
|
Collection<PSObject> results; // Holds the result of the pipeline execution.
|
|
|
|
// Create the PowerShell object. Notice that no runspace is specified so a
|
|
// new default runspace is used.
|
|
PowerShell powershell = PowerShell.Create();
|
|
|
|
// Use the using statement so that we can dispose of the PowerShell object
|
|
// when we are done.
|
|
using (powershell)
|
|
{
|
|
// Add the get-process cmdlet to the pipeline of the PowerShell object.
|
|
powershell.AddCommand("get-process");
|
|
|
|
// Add the sort-object cmdlet and its parameters to the pipeline of
|
|
// the PowerShell object so that we can sort the HandleCount property
|
|
// in descending order.
|
|
powershell.AddCommand("sort-object").AddParameter("descending").AddParameter("property", "handlecount");
|
|
|
|
// Run the pipeline of the Powerhell object synchronously.
|
|
results = powershell.Invoke();
|
|
}
|
|
|
|
// Even after disposing of the PowerShell object, we still
|
|
// need to set the powershell variable to null so that the
|
|
// garbage collector can clean it up.
|
|
powershell = null;
|
|
|
|
Console.WriteLine("Process HandleCount");
|
|
Console.WriteLine("--------------------------------");
|
|
|
|
// Display the results returned by the commands.
|
|
foreach (PSObject result in results)
|
|
{
|
|
Console.WriteLine(
|
|
"{0,-20} {1}",
|
|
result.Members["ProcessName"].Value,
|
|
result.Members["HandleCount"].Value);
|
|
}
|
|
|
|
System.Console.WriteLine("Hit any key to exit...");
|
|
System.Console.ReadKey();
|
|
}
|
|
}
|
|
}
|
|
|