如何在C#中以编程方式从MailboxDatabase获取DatabaseCopies(在c#中包装ExchangeMangementShell cmdlet)?

时间:2013-02-01 09:46:05

标签: c# powershell powershell-v2.0 exchange-server-2010 powershell-remoting

我在C#中包装ExchangeManagementShell Cmdlet,以编程方式执行cmdlet(请参阅__http://social.msdn.microsoft.com/Forums/en-US/exchangesvrdevelopment/thread/155504b3-ffe3-4bdf-887a-1e61842a8697 )

我知道mailboxdatabase的“databasecopies”属性包含副本。但我不知道如何解析deserilzied databasecopies数据以获得适当的。

例如:请参阅以下代码段。我基本上解析Get-MailboxDatabase cmdlet结果以获取我们感兴趣的属性。不确定,如何从中获取DatabaseCopies。

非常感谢任何帮助或参考,因为我有点震惊?

foreach (PSObject cmdletResults in this.Execute("Get-MailboxDatabase"))
{
   MailboxDatabase mdb = new MailboxDatabase();
   mdb.ExchangeApplicationSystemGlobalId = this.ExchangeApplicationSystem.GlobalId;
   mdb.Name = cmdletResults.Properties["Name"].Value.ToString();
   mdb.MountedOnServer = cmdletResults.Properties["Server"].Value.ConvertToString();
   mdb.EdbFilePath = cmdletResults.Properties["EdbFilePath"].Value.ConvertToString();
   mdb.LogFolderPath = cmdletResults.Properties["LogFolderPath"].Value.ConvertToString();
   mdb.LogFilePrefix = cmdletResults.Properties["LogFilePrefix"].Value.ConvertToString();
   mdb.Guid = cmdletResults.Properties["Guid"].Value.ToString();
   string mt = cmdletResults.Properties["MasterType"].Value.ConvertToString();
   if (!string.IsNullOrEmpty(mt))
   {
      mdb.MasterType = mt.ToEnum(MasterType.Unknown);
   }
   mdb.MasterServerOrAvailabilityGroup = cmdletResults.Properties["MasterServerOrAvailabilityGroup"].Value.ConvertToString();
   PSObject pso = cmdletResults.Properties["Servers"].Value as PSObject;
   if (null != pso
          && null != pso.BaseObject)
   {
       ArrayList servers = pso.BaseObject as ArrayList;
       if (null != servers)
       {
           mdb.Servers = servers.ToArray().Where(server => null != server)
                                .Select(server => server.ToString())
                                .ToArray();
       }
    }
}                    

此致

梦想家

1 个答案:

答案 0 :(得分:0)

即使从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() 方法调用失败,因为[Deserialized.Microsoft.Exchange.Data.Directory.SystemConfiguration.MailboxDatabase] d oes不包含名为'gettype'的方法。 在行:1个字符:12 + $ md.gettype<<<< ()     + CategoryInfo:InvalidOperation:(gettype:String)[],RuntimeException     + FullyQualifiedErrorId:MethodNotFound

所以,看起来我没有任何选择。我会为我的用例寻找解决方法。

的问候, 梦想家