本文整理汇总了C#中System.Management.Automation.Runspaces.Runspace.GetCurrentlyRunningPipeline方法的典型用法代码示例。如果您正苦于以下问题:C# Runspace.GetCurrentlyRunningPipeline方法的具体用法?C# Runspace.GetCurrentlyRunningPipeline怎么用?C# Runspace.GetCurrentlyRunningPipeline使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Management.Automation.Runspaces.Runspace
的用法示例。
在下文中一共展示了Runspace.GetCurrentlyRunningPipeline方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InvokeOnRunspace
/// <summary>
/// Helper method to invoke a PSCommand on a given runspace. This method correctly invokes the command for
/// these runspace cases:
/// 1. Local runspace. If the local runspace is busy it will invoke as a nested command.
/// 2. Remote runspace.
/// 3. Runspace that is stopped in the debugger at a breakpoint.
///
/// Error and information streams are ignored and only the command result output is returned.
///
/// This method is NOT thread safe. It does not support running commands from different threads on the
/// provided runspace. It assumes the thread invoking this method is the same that runs all other
/// commands on the provided runspace.
/// </summary>
/// <param name="runspace">Runspace to invoke the command on</param>
/// <param name="command">Command to invoke</param>
/// <returns>Collection of command output result objects</returns>
public static Collection<PSObject> InvokeOnRunspace(PSCommand command, Runspace runspace)
{
if (command == null)
{
throw new PSArgumentNullException("command");
}
if (runspace == null)
{
throw new PSArgumentNullException("runspace");
}
if ((runspace.Debugger != null) && runspace.Debugger.InBreakpoint)
{
// Use the Debugger API to run the command when a runspace is stopped in the debugger.
PSDataCollection<PSObject> output = new PSDataCollection<PSObject>();
runspace.Debugger.ProcessCommand(
command,
output);
return new Collection<PSObject>(output);
}
// Otherwise run command directly in runspace.
PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;
ps.IsRunspaceOwner = false;
if (runspace.ConnectionInfo == null)
{
// Local runspace. Make a nested PowerShell object as needed.
ps.SetIsNested(runspace.GetCurrentlyRunningPipeline() != null);
}
using (ps)
{
ps.Commands = command;
return ps.Invoke<PSObject>();
}
}