Compartir a través de


PSCommand Class

Definition

Defines a PowerShell command / script object which can be used with PowerShell object.

public ref class PSCommand sealed
public sealed class PSCommand
type PSCommand = class
Public NotInheritable Class PSCommand
Inheritance
PSCommand

Constructors

PSCommand()

Creates an empty PSCommand; a command or script must be added to this PSCommand before it can be executed.

Properties

Commands

Gets the collection of commands from this PSCommand instance.

Methods

AddArgument(Object)

Adds an argument to the last added command. For example, to construct a command string "get-process | select-object name"

PSCommand command = new PSCommand("get-process")
    .AddCommand("select-object")
    .AddArgument("name");

This will add the value "name" to the positional parameter list of "select-object" cmdlet. When the command is invoked, this value will get bound to positional parameter 0 of the "select-object" cmdlet which is "Property".

AddCommand(Command)

Add a Command element to the current command pipeline.

AddCommand(String, Boolean)

Add a cmdlet to construct a command pipeline. For example, to construct a command string "get-process | sort-object",

PSCommand command = new PSCommand("get-process").AddCommand("sort-object");
AddCommand(String)

Add a command to construct a command pipeline. For example, to construct a command string "get-process | sort-object",

PSCommand command = new PSCommand("get-process").AddCommand("sort-object");
AddParameter(String, Object)

Add a parameter to the last added command. For example, to construct a command string "get-process | select-object -property name"

PSCommand command = new PSCommand("get-process")
    .AddCommand("select-object")
    .AddParameter("property", "name");
AddParameter(String)

Adds a switch parameter to the last added command. For example, to construct a command string "get-process | sort-object -descending"

PSCommand command = new PSCommand("get-process")
    .AddCommand("sort-object")
    .AddParameter("descending");
AddScript(String, Boolean)

Add a piece of script to construct a command pipeline. For example, to construct a command string "get-process | foreach { $_.Name }"

PSCommand command = new PSCommand("get-process")
    .AddScript("foreach { $_.Name }", true);
AddScript(String)

Add a piece of script to construct a command pipeline. For example, to construct a command string "get-process | foreach { $_.Name }"

PSCommand command = new PSCommand("get-process")
    .AddScript("foreach { $_.Name }", true);
AddStatement()

Adds an additional statement for execution

For example,

Runspace rs = RunspaceFactory.CreateRunspace();
PowerShell ps = PowerShell.Create();

ps.Runspace = rs;
ps.AddCommand("Get-Process").AddArgument("idle");
ps.AddStatement().AddCommand("Get-Service").AddArgument("audiosrv");
ps.Invoke();
Clear()

Clears the command(s).

Clone()

Creates a shallow copy of the current PSCommand.

Applies to