8

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

ohmusama
  • 4,159
  • 5
  • 24
  • 44
  • 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. – reim Dec 15 '20 at 19:34

2 Answers2

22

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

Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • 3
    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) – ohmusama Nov 17 '14 at 03:18
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.

tnw
  • 13,521
  • 15
  • 70
  • 111
  • I get 0 objects back from the call... Am I invoking the PS wrong? – ohmusama Oct 03 '14 at 22:04
  • @ohmusama Possibly. Take a look at the guidance I've provided [here](http://stackoverflow.com/questions/21264112/how-to-call-poweshell-cmdlets-from-c-sharp-in-visual-studio/21264331#21264331) on how to call PowerShell from C# and see if it works for you. – tnw Oct 06 '14 at 13:34