PowerShell 2.0 via C#, return values are empty?
-
20-09-2019 - |
Question
I'm using C# to call Exchange 2010 PowerShell methods and I've just encountered a snag.
The Collection returned by the Execute method don't have a BaseObject. I can use .Properties["PropertyName"], but it seems that all those values are string values. Not very practical for file size, Guids and so on.
Reading http://blogs.msdn.com/powershell/archive/2010/01/07/how-objects-are-sent-to-and-from-remote-sessions.aspx, it seems that this is normal, and the types I have are Deserialized.Namespace.TypeName.
I'm looking for a way to serialize them back into a live object that I can use properties and so on. I have the appropriate DLL and everything.
Solution
It seems I can get any property I want, but it needs to be in the PS script I invoke remotely.
I figured it out while querying PowerShell via direct Remote powershell, not with the Exchange namespace.
Here is an example
$spsite | Select Id, Url,
@{ Name = "Owner"; Expression = { $_.Owner.UserLogin } }
This works perfectly, I can then use
psResult.Members["Owner"].Value as string
to get the owner
OTHER TIPS
You can get the orginal object like this:
PS bit:
$results = $MyCustomCollection
C# bit:
System.Collections.ObjectModel.Collection<PSObject>
results = pipeline.Invoke();
CustomCollection theCustumCollection
= (CustomCollection )runspace.SessionStateProxy.GetVariable("results");
you do have to make sure that the only thing returned is that object and nothing else; so put anything that would return anything to null;