T63zengyu
取名字真难null
2015-04-21 09:27

c# 访问Exchange初始化时报错

  • 代码
  • string
  • 信息
  • line

RunspaceConfiguration runspaceConfig = RunspaceConfiguration.Create();

            PSSnapInException snapEX = null;
            PSSnapInInfo snampInfo = runspaceConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out snapEX);

            Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfig);

            runspace.Open();
                            这段代码可执行,不报错
                            但是接下来我需要获取一个信息,代码如下:
                             foreach (PSObject obj in results)
                {
                    foreach (PSMemberInfo mi in obj.Members)
                    {
                        File.AppendAllText("D:\\log.txt", "id:" + mi.Name.Trim() + "\r\n");
                        if (mi.Name == "ActiveSyncAllowedDeviceIDs")
                        {
                            Microsoft.Exchange.Data.MultiValuedProperty<string> mvp = (Microsoft.Exchange.Data.MultiValuedProperty<string>)mi.Value;
                            foreach (string item in mvp)
                            {
                                File.AppendAllText("D:\\log.txt", "id:" + o + "\r\n");
                           }
                            //MultiValuedProperty

                        }
                    }

                }
                                    要有Microsoft.Exchange.Data.MultiValuedProperty这个类型,必须引用Microsoft.Exchange.Data,如果引用了这个类型,最上面那段代码就会报错,错误信息如下:
                                    The type initializer for 'Microsoft.Exchange.Management.PowerShell.CmdletConfigurationEntries' threw an exception.

at Microsoft.Exchange.Management.PowerShell.CmdletConfigurationEntries.get_ExchangeCmdletConfigurationEntries()
at Microsoft.Exchange.Management.PowerShell.AdminPSSnapIn.get_Cmdlets()
at System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.MergeCustomPSSnapIn(PSSnapInInfo mshsnapinInfo, CustomPSSnapIn customPSSnapIn)
at System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.LoadCustomPSSnapIn(PSSnapInInfo mshsnapinInfo)
at System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.LoadPSSnapIn(PSSnapInInfo mshsnapinInfo)
at System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.LoadPSSnapIn(PSSnapInInfo mshsnapinInfo, PSSnapInException& warning)
at System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.DoAddPSSnapIn(String name, PSSnapInException& warning)
at PowerShell.PowerShellManage.LiftBan(String user) in E:\AnyOffice\PowerShell\PowerShellManage.cs:line 282

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答