InvocationInfo Class
Collapse the table of content
Expand the table of content

InvocationInfo Class

Provides information about invoked commands, such as the name of the command, the string used to invoke the command, and where the command is in the pipeline.

Namespace: System.Management.Automation
Assembly: System.Management.Automation (in System.Management.Automation.dll)

Dim instance As InvocationInfo

[DebuggerDisplayAttribute("Command = {_commandInfo}")] 
public class InvocationInfo
/** @attribute DebuggerDisplayAttribute("Command = {_commandInfo}") */ 
public class InvocationInfo
DebuggerDisplayAttribute("Command = {_commandInfo}") 
public class InvocationInfo

The following code example, taken from the Runspace04 example in the Windows SDK, uses the InvocationInfo class to trap and display error information.

class Runspace04

        static void Main(string[] args)
            // Create an instance of the PowerShell class.
            PowerShell powershell = PowerShell.Create().AddCommand("Get-ChildItem").AddCommand("Select-String").AddArgument("*");

            // Invoke the runspace. Because of the bad regular expression,
            // no objects will be returned. Instead, an exception will be
            // thrown.
                foreach (PSObject result in powershell.Invoke())
                    Console.WriteLine("'{0}'", result.ToString());

                // Now process any error records that were generated while running the script.
                Console.WriteLine("\nThe following non-terminating errors occurred:\n");
                PSDataCollection<ErrorRecord> errors = powershell.Streams.Error;
                if (errors != null && errors.Count > 0)
                    foreach (ErrorRecord err in errors)
                        System.Console.WriteLine("    error: {0}", err.ToString());
            catch (RuntimeException runtimeException)
                // Trap any exception generated by the script. These exceptions
                // will all be derived from RuntimeException.
                System.Console.WriteLine("Runtime exception: {0}: {1}\n{2}",

            System.Console.WriteLine("\nHit any key to exit...");

This class can describe any type of command defined by the CommandTypes enumeration. These types include cmdlets, functions, or scripts.


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


Target Platforms

© 2016 Microsoft