Windows Dev Center

Collapse the table of content
Expand the table of content
Expand Minimize
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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)

'Usage
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.
            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();
        }
    }

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


System.Object
  System.Management.Automation.InvocationInfo

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


Show:
© 2015 Microsoft