获取有关Exchange服务器的信息?

时间:2014-09-17 06:25:21

标签: c# powershell exchange-server

我的办公室里有一台Exchange服务器2013。我想找到有关使用Exchange管理shell命令找到的有关Exchange服务器的所有信息。我想找到如下数据。

  

服务器名称,邮箱数量,邮件联系人数量,信息存储,存储组,最近创建和删除的邮箱,有关​​通过Exchange服务器的所有电子邮件流的信息,有关发件人,收件人的信息以及我从交换中找到的更多信息服务器。

编程。我想使用C#从地理距离以编程方式查找此信息。我的机器上有窗口7,我想这样做。我正在尝试使用C#的远程电源shell。 例如,我有一个交换管理shell命令,即

Get-mailbox -resultsize unlimited -filter {$_.forwardingaddress -ne $null} | select name, userprincipalname

使用Exchange管理shell执行上述cmdlet后,我得到了一些数据,我想用C#以编程方式获取类似的信息。

我的代码段

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using System.Management.Automation; 
using System.Management.Automation.Runspaces;
namespace WindowsFormsApplication1
{
 public partial class Form1 : Form
 {
   public Form1()
   {
        InitializeComponent();
   }
   private void button1_Click(object sender, EventArgs e)
   {
     string schemaURI = "http://schemas.microsoft.com/powershell/Microsoft.Exchange";
     Uri connectTo = new Uri("https://<serverIP>/powershell/");
     string strpassword = "password";   
     System.Security.SecureString securePassword = new System.Security.SecureString();
     foreach (char c in strpassword)
     {
         securePassword.AppendChar(c);
     }
     PSCredential credential = new PSCredential("Administrator", securePassword);
     WSManConnectionInfo connectionInfo = new WSManConnectionInfo(connectTo,schemaURI, credential);
     connectionInfo.MaximumConnectionRedirectionCount = 5;
     connectionInfo.SkipCACheck = true;
     connectionInfo.SkipCNCheck = true;
     try
     {
        Runspace remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo);
        remoteRunspace.Open();
        var command = new Command("Get-mailbox");
        command.Parameters.Add("resultsize", "unlimited");
        command.Parameters.Add("Filter", "{forwardingaddress -ne $null}");
        var pipeline = remoteRunspace.CreatePipeline();
        pipeline.Commands.Add(command);
        var results = pipeline.Invoke();    
        foreach (PSObject item in results)
        {                                                                                          
            PSPropertyInfo pinfo = (PSPropertyInfo)item.Properties["Name"];
            PSPropertyInfo prop = (PSPropertyInfo)item.Properties["userprincipalname"];
            //prop = item.Properties["Name"];
            if (pinfo != null)
            {
                  MessageBox.Show(pinfo.Value.ToString());
            }
            if (prop != null)
            {
                  MessageBox.Show(prop.Value.ToString());
            }
         }
         remoteRunspace.Dispose();
        }
      catch (Exception ex)
      {
          MessageBox.Show(ex.Message); 
      }            
    }
  }
}

该行:

   remoteRunspace.Open();

产生以下例外情况:

  

连接到远程服务器失败,并显示以下错误消息:WinRM客户端无法处理该请求。 WinRM客户端尝试过   使用Negotiate身份验证机制,但目标计算机   (IP:443)返回“拒绝访问”错误。更改配置   允许使用协商认证机制或指定一个   服务器支持的身份验证机制。使用   Kerberos,将本地计算机名称指定为远程目标。   还要验证客户端计算机和目标计算机是否正常   加入了一个域名。要使用Basic,请将本地计算机名称指定为   远程目标,指定基本身份验证并提供用户   名称和密码。报告的可能的认证机制   服务器:有关详细信息,请参阅about_Remote_Troubleshooting   帮助主题

如何修复此类异常?

2 个答案:

答案 0 :(得分:1)

转到您要获取上述信息的服务器上面给出的路径。

Start Menu -> Administrative tools -> iis manager -> Sites -> default web site -> powershell 

然后在/ powershell home中选择IIS身份验证,打开身份验证后,有六种身份验证,如下所示

Anonymous authentication "disabled"
Asp.net impersonation    "disabled"
Basic authentication     "disabled"
Digest authentication    "disabled"
Forms authentication     "disabled"
Windows authentication   "disabled"

现在启用了最后一次身份验证,即Windows身份验证。启用Windows身份验证后,它们如下所示

Anonymous authentication "disabled"
Asp.net impersonation    "disabled"
Basic authentication     "disabled"
Digest authentication    "disabled"
Forms authentication     "disabled"
Windows authentication   "enabled"

之后运行你的代码,你得到了理想的结果。

答案 1 :(得分:0)

您需要在您使用的WSManConnectionInfo对象上设置AuthenticationMechanism,以匹配您配置Exchange Server的方式。如果您使用https,则应将其设置为Basic,例如

connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;

否则,如果您仍然在Exchange Server上使用默认配置,则可以使用Kerberos和http,以便更改MSDN示例http://technet.microsoft.com/en-us/library/dd335083(v=exchg.150).aspx中使用的内容(确保使用FQDN而不是IP )

Uri connectTo = new Uri("http://<FQDN>/powershell/");

然后使用

connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;

干杯 格伦