【问题标题】:Getting information about exchange server?获取有关交换服务器的信息?
【发布时间】:2014-10-01 18:25:59
【问题描述】:

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

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

以编程方式。我想使用 C# 从地理距离以编程方式查找此信息。我的机器上有窗口 7,我想通过它执行此操作。我正在尝试使用带有 C# 的远程电源外壳。 例如,我有一个交换管理 shell 命令,即

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

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

我的代码sn-p

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 客户端试图 使用协商身份验证机制,但目标计算机 (IP:443) 返回“拒绝访问”错误。更改配置 允许使用协商身份验证机制或指定一个 服务器支持的身份验证机制。使用 Kerberos,将本地计算机名称指定为远程目标。 还要验证客户端计算机和目标计算机是 加入一个域。要使用 Basic,请将本地计算机名称指定为 远程目标,指定基本身份验证并提供用户 名称和密码。报告的可能的身份验证机制 服务器:有关详细信息,请参阅 about_Remote_Troubleshooting 帮助主题

如何解决此类异常?

【问题讨论】:

    标签: c# powershell exchange-server


    【解决方案1】:

    转到您想要获取上述信息的服务器上的以下给定路径。

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

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

    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"
    

    然后运行你的代码,你得到了想要的结果。

    【讨论】:

      【解决方案2】:

      您需要在您使用的 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;
      

      干杯 格伦

      【讨论】:

      • 我在 WSManConnectionInfo 对象上设置了 AuthenticationMechanism,我还用 FQDN 替换了服务器 IP,但我仍然遇到异常,即 连接到远程服务器失败并显示以下错误消息: WinRM 客户端无法处理该请求,因为无法解析服务器名称。有关详细信息,请参阅 about_Remote_Troubleshooting 帮助主题。
      • 该错误表明您无法解析您尝试使用的 FQDN。如果不能,您应该能够 ping 您使用的任何服务器名称,这将永远无法正常工作。您是否尝试过仅使用 Powershell 从控制台进行连接?如果您在控制台中确定了哪些身份验证/服务器设置起作用,您应该能够轻松地将它们移动到您的代码中。
      • 我已经正确添加了 FQDN,我尝试使用 Powershell 连接到控制台,但我无法连接,因为出现了同样的错误。
      • 听起来您有一个底层网络问题,您应该能够 ping 并解决 FQDN ?如果您无法让控制台工作,您的代码将永远无法工作,您可能想尝试technet.microsoft.com/en-us/library/dd351136(v=exchg.141).aspx 中的调试步骤
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      • 1970-01-01
      • 1970-01-01
      • 2020-06-16
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      相关资源
      最近更新 更多