2010-01-13 98 views
2

我使用C#来调用Exchange 2010 PowerShell方法,并且我刚遇到一个问题。通过C#PowerShell 2.0,返回值为空?

Execute方法返回的集合没有BaseObject。我可以使用.Properties [“PropertyName”],但似乎所有这些值都是字符串值。不太适用于文件大小,Guids等。

阅读http://blogs.msdn.com/powershell/archive/2010/01/07/how-objects-are-sent-to-and-from-remote-sessions.aspx,看来这是正常的,我有的类型是Deserialized.Namespace.TypeName。

我正在寻找一种方法将它们序列化回到可以使用属性等的活动对象中。我有适当的DLL和一切。

回答

0

看来我可以得到我想要的任何属性,但它需要在我远程调用的PS脚本中。

我通过直接远程PowerShell查询PowerShell,而不是使用Exchange名称空间来计算它。

下面是一个例子

$spsite | Select Id, Url, 
      @{ Name = "Owner"; Expression = { $_.Owner.UserLogin } } 

这工作完全,然后我可以使用

psResult.Members["Owner"].Value as string 

得到业主

0

你可以得到原单对象是这样的:

PS位:

$results = $MyCustomCollection 

C#位:

System.Collections.ObjectModel.Collection<PSObject> 
results = pipeline.Invoke(); 

CustomCollection theCustumCollection 
    = (CustomCollection)runspace.SessionStateProxy.GetVariable("results"); 

你必须确保返回的唯一的事情就是对象,没有别的; 所以放任何东西都会返回null;