7

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

1
  • I highly suggest such a strategy rather than doing a ps.AddCommand(script).Invoke(); on the PowerShell handle; I had a leak or something by which with two consecutive invocations the latter would never get executed. With the runspace it works fine instead. Commented Dec 15, 2020 at 19:34

2 Answers 2

25

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

Sign up to request clarification or add additional context in comments.

1 Comment

Took me a while to get back to this, had to do some other things first. For those of you who find this later. The exception thrown by the command is available in the elements in command.Streams.Error.ElementAt(0).Exception (assuming there is at least 1 element)
0

Try iterating through the results collection:

foreach (PSObject psObject in results)
{
   ....do stuff with psObject (output to console, etc... you can use ToString() if you want)
}

That should get you the actual output from the console.

2 Comments

I get 0 objects back from the call... Am I invoking the PS wrong?
@ohmusama Possibly. Take a look at the guidance I've provided here on how to call PowerShell from C# and see if it works for you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.