I have this simple program using powershell. This is just a proof of concept, I'm using the same code in a larger app. The problem is that the values of some of the properties in the code below can't be read. Reading the Value property throws a GetValueInvocationException.
This actually even happens with one of Microsoft sample projects that comes with the PowerShell SDK. Why is this and is there a solution?
static void Main(string[] args)
{
var powerShell = System.Management.Automation.PowerShell.Create();
var runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Runspace.DefaultRunspace = runspace;
powerShell.Runspace = runspace;
powerShell.AddScript("Get-Process");
var results = powerShell.Invoke();
foreach (var prop in results.First().Properties)
{
try
{
Console.WriteLine(prop.Name + " : " + prop.Value);
}
catch (Exception e)
{
Console.WriteLine(string.Format("Exception {0} on {1}", e.GetType(), prop.Name));
}
}
Console.ReadKey();
}