【问题标题】:How to get the properties of Cmdlet results which contain list of deserialized objects (multi-valued properties)(executing powershell cmdlets from c#)如何获取包含反序列化对象列表(多值属性)的 Cmdlet 结果的属性(从 c# 执行 powershell cmdlet)
【发布时间】:2013-02-03 06:20:13
【问题描述】:

我正在使用 c# 执行 PowerShell cmdlet 并解析 cmdlet 结果 (Collection[].Properties["PropertyName"].value) 以获取所需的值。

但有些结果包含反序列化对象的列表。但是 PowerShell 只是将这些转换为对象的 ArrayList,我无法获取它们的所有属性。

例如:Exchange 命令行管理程序 cmdlet 的 Get-MailboxDatabase 返回 Exchange 服务器上的邮箱数据库列表(集合)。我可以从 PSObject.Properties["PropertyName"].Value 中获取名称、服务器等属性。

它还包含一个数据库副本列表(如下所示)

class MailboxDatabase { DatabaseCopy[] DatabaseCopies; }

PSObject.Properties["DatabaseCopies"].Value 只不过是一个对象数组列表。

但是所有 DatabaseCopy 属性都没有反序列化(或者我不知道如何获取它们)。

我只能看到名称(从 arraylists 列表中的 DatabaseCopy 字符串)。

谁能帮助我如何获得多值结果的所有反序列化属性?

更多详情可以关注:How to get DatabaseCopies from MailboxDatabase programmatically in C# (wrapped ExchangeMangementShell cmdlets in c#)?

问候, 梦想家

【问题讨论】:

    标签: c# serialization powershell powershell-2.0 powershell-remoting


    【解决方案1】:

    cmdlet 应返回 PSObject 或 PSObject 列表。

    我认为 DatabaseCopies 没有序列化,只是设置为纯字符串。所以它们不能被反序列化。

    【讨论】:

    • 但是 DatabaseCopy 是一个可序列化的类。例如:其定义为 [Serializable] public class DatabaseCopy : ADConfigurationObject, IActivationPreferenceSettable, IComparable { // 属性 public int ActivationPreference { get;内部集; } 内部 int ActivationPreferenceInternal { 获取;放; } 公共字符串数据库名称 { 获取; } 公共字符串 HostServerName { 获取; } 公共 EnhancedTimeSpan ReplayLagTime { 获取;放; } [Parameter(Mandatory=false)] public EnhancedTimeSpan TruncationLagTime { get;放; } }
    【解决方案2】:

    即使来自 PowerShell,它们也以纯字符串形式出现。

    PS C:\> $md.type
    PS C:\> $md.DatabaseCopies.gettype()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     ArrayList                                System.Object
    
    
    PS C:\> $md.DatabaseCopies[0].gettype()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     String                                   System.Object
    
    PS C:\> $md.gettype()
    Method invocation failed because [Deserialized.Microsoft.Exchange.Data.Directory.SystemConfiguration.MailboxDatabase] doesn't contain a method named 'gettype'.
    

    所以,看来我别无选择。我将为我的用例寻找解决方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-23
      • 1970-01-01
      • 2012-06-02
      相关资源
      最近更新 更多