Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Powershell errors from c#

Problem

I am invoking powershell commands from c# however, the PowerShell command object only seems to have the property bool HasErrors which doesn't help me know what error I received.

This is how I build my powershell command

Library

public static class PowerSheller
{
    public static Runspace MakeRunspace()
    {
        InitialSessionState session = InitialSessionState.CreateDefault();
        Runspace runspace = RunspaceFactory.CreateRunspace(session);
        runspace.Open();

        return runspace;
    }

    public static PowerShell MakePowershell(Runspace runspace)
    {
        PowerShell command = PowerShell.Create();
        command.Runspace = runspace;

        return command;
    }
}

Invoking Move-Vm cmdlet

using (Runspace runspace = PowerSheller.MakeRunspace())
{
    using (PowerShell command = PowerSheller.MakePowershell(runspace))
    {
        command.AddCommand("Move-VM");
        command.AddParameter("Name", arguments.VMName);
        command.AddParameter("ComputerName", arguments.HostName);
        command.AddParameter("DestinationHost", arguments.DestinationHostName);

        if (arguments.MigrateStorage)
        {
            command.AddParameter("IncludeStorage");
            command.AddParameter("DestinationStoragePath", arguments.DestinationStoragePath);
        }

        try
        {
            IEnumerable<PSObject> results = command.Invoke();
            success = command.HasErrors;
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}

I was expecting some sort of exception to be thrown on failure, but instead it returns 0 objects. While HasErrors will result in knowing if the command was successful or not; I'm still not sure how to get the specific error, as no exception is thrown.

Thanks

like image 883
ohmusama Avatar asked Oct 03 '14 18:10

ohmusama


People also ask

How do I get PowerShell errors?

You can use Get-Error to display a specified number of errors that have occurred in the current session using the Newest parameter. The Get-Error cmdlet also receives error objects from a collection, such as $Error , to display multiple errors from the current session.

What is $error in PowerShell?

The $Error automatic variable contains an array of error objects in the current session. The array of objects can be piped to Get-Error to receive detailed error messages. In this example, $Error is piped to the Get-Error cmdlet. the result is list of detailed error messages, similar to the result of Example 1.

How do I catch specific exceptions in PowerShell?

If you want to handle a specific type of exception then you can provide the exception name in the catch block. To know the name of the exception you need to get the property of the $Error variable and it is GetType().

What is ${} in PowerShell?

To create or display a variable name that includes spaces or special characters, enclose the variable name in braces. This directs Windows PowerShell to interpret the characters in the variable name literally.


1 Answers

To see the errors look at the collection PowerShell.Streams.Error or in your code command.Streams.Error.

like image 63
Keith Hill Avatar answered Sep 30 '22 12:09

Keith Hill