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

InvocationInfo Class

 

Updated: April 27, 2016

Applies To: Windows PowerShell

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)

System.Object
  System.Management.Automation.InvocationInfo

public class InvocationInfo

NameDescription
System_CAPS_pubpropertyBoundParameters

Gets a dictionary of the arguments that were bound to parameters of the command. This property is introduced in Windows PowerShell 2.0.

System_CAPS_pubpropertyCommandOrigin

Gets the dispatch origin of the command. For example, was the command dispatched internally by Windows PowerShell or by a runspace. This property is introduced in Windows PowerShell 2.0.

System_CAPS_pubpropertyDisplayScriptPosition

Gets or sets the position for the invocation or error. Introduced in Windows PowerShell 3.0.

System_CAPS_pubpropertyExpectingInput

Gets a value that indicates whether the command is expecting input from the pipeline. This property is introduced in Windows PowerShell 2.0.

System_CAPS_pubpropertyHistoryId

Gets the history identifier for the command that was invoked. This property is introduced in Windows PowerShell 2.0.

System_CAPS_pubpropertyInvocationName

Gets the name of the command that was invoked.

System_CAPS_pubpropertyLine

Gets the text of the line used to invoke the command.

System_CAPS_pubpropertyMyCommand

Gets information about the command.

System_CAPS_pubpropertyOffsetInLine

Gets the offset (in characters) of the command location in the line.

System_CAPS_pubpropertyPipelineLength

Gets the number of elements that are in the pipeline that contains the command.

System_CAPS_pubpropertyPipelinePosition

Gets the position of this command in the pipeline that contains the command.

System_CAPS_pubpropertyPositionMessage

Gets a formatted message that describes where the command appeared in the pipeline.

System_CAPS_pubpropertyPSCommandPath

Gets the path to the command, relative to the directory where the user invoked the command. Introduced in Windows PowerShell 3.0.

System_CAPS_pubpropertyPSScriptRoot

Gets the directory where the user invoked the command. Introduced in Windows PowerShell 3.0.

System_CAPS_pubpropertyScriptLineNumber

Gets the line number of the script that contains the command.

System_CAPS_pubpropertyScriptName

Gets the name of the script containing the command.

System_CAPS_pubpropertyUnboundArguments

Gets a list of the arguments that were not bound to parameters of the command. This property is introduced in Windows PowerShell 2.0.

NameDescription
System_CAPS_pubmethodSystem_CAPS_staticCreate(CommandInfo, IScriptExtent)

Creates a new InvocationInfo class, based on the specified command info and script position. Introduced in Windows PowerShell 4.0.

System_CAPS_pubmethodEquals(Object)

(Inherited from Object.)

System_CAPS_protmethodFinalize()

(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

(Inherited from Object.)

System_CAPS_pubmethodGetType()

(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone()

(Inherited from Object.)

System_CAPS_pubmethodToString()

(Inherited from Object.)

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

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.
            try
            {
                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}",
                    runtimeException.ErrorRecord.InvocationInfo.InvocationName,
                    runtimeException.Message,
                    runtimeException.ErrorRecord.InvocationInfo.PositionMessage
                    );
            }

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

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

Return to top
Show:
© 2016 Microsoft