I am using powershell commands to execute scripts and cmdlets. So while executing cmdlets I used powershell.invoke and while executing a script I used pipeline.invoke method. I wanted to know if there is any difference between the System.Management.Automation.pipeline.invoke()
method and System.Management.Automation.Runspaces.powershell.invoke()
method.
Description. The Invoke-Command cmdlet runs commands on a local or remote computer and returns all output from the commands, including errors. Using a single Invoke-Command command, you can run commands on multiple computers. To run a single command on a remote computer, use the ComputerName parameter.
Here is a simple example. The following command gets the Notepad process and then stops it. The first command uses the Get-Process cmdlet to get an object representing the Notepad process. It uses a pipeline operator ( | ) to send the process object to the Stop-Process cmdlet, which stops the Notepad process.
One of the PowerShell features is the use of compressed or abbreviated cmdlet names. Instead of using the full name, 'Invoke-Expression' is most of the time replaced by 'IEX'. This three-characters string is then replaced by something more unreadable.
The approach to create a pipeline in a runspace e.g.:
var pipeline = runspace.CreatePipeline();
is 1.0 thing. That is, the original PowerShell hosting API required you to create the pipeline through the runspace that you created. My guess is that the team got feedback that the hosting API needed to be simplified so they came up with the PowerShell
class for the 2.0 release.
If you're interested in the nitty gritty details of what is different, go grab dotPeek and crack open the System.Management.Automation.dll and peruse it. One difference is that PowerShell.Invoke() has to determine the type of runspace in use in order to determine which type of pipeline to create e.g. LocalPipeline or RemotePipeline. When you use a Runspace, you actually create a derived class (LocalRunspace or RemoteRunspace) and each one of those will create the appropriate type of pipeline.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With