Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正在获取有关exchange server的信息?_C#_Powershell_Exchange Server - Fatal编程技术网

C# 正在获取有关exchange server的信息?

C# 正在获取有关exchange server的信息?,c#,powershell,exchange-server,C#,Powershell,Exchange Server,我办公室里有一台exchange server 2013。我想查找使用exchange命令行管理程序命令找到的有关exchange server的所有信息。我想找到如下数据 服务器名称、邮箱数、邮件联系人数、信息存储、存储组、最近创建和删除的邮箱、有关通过exchange Server的所有电子邮件流的信息、有关发件人、收件人的信息以及我从exchange Server中找到的更多信息 以编程方式。我想使用C从地理位置以编程方式查找这些信息。我的机器上有Windows7,我想通过它来完成这项工作

我办公室里有一台exchange server 2013。我想查找使用exchange命令行管理程序命令找到的有关exchange server的所有信息。我想找到如下数据

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

以编程方式。我想使用C从地理位置以编程方式查找这些信息。我的机器上有Windows7,我想通过它来完成这项工作。我正在尝试使用带有C的远程powershell。 例如,我有一个exchange命令行管理程序命令,即

Get-mailbox -resultsize unlimited -filter {$_.forwardingaddress -ne $null} | select name, userprincipalname
在使用ExchangeManagementShell执行上述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); 
      }            
    }
  }
}
生成以下给出的异常:

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


如何修复此类型的异常?

您需要在您使用的WSManConnectionInfo对象上设置身份验证机制,以匹配您配置Exchange服务器的方式。如果您使用的是https,那么您应该将其设置为Basic eg

connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
否则,如果Exchange服务器上仍有默认配置,则可以使用Kerberos和http,因此请更改MSDN示例中使用的配置,确保使用FQDN而不是IP

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

在您的服务器上转到下面给定的路径,以获取上述信息

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"

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

我在WSManConnectionInfo对象上设置了身份验证机制,并且我还用FQDN替换了服务器IP,但我仍然收到一个异常,即连接到远程服务器失败,并显示以下错误消息:WinRM客户端无法处理该请求,因为无法解析服务器名称。有关详细信息,请参阅关于远程疑难解答帮助主题。该错误表明您无法解析尝试使用的FQDN。您应该能够ping您使用的任何服务器名,如果您不能,这将永远不会起作用。您是否尝试过仅使用Powershell从控制台进行连接?如果您在控制台中计算出身份验证/服务器设置的工作方式,您应该能够轻松地将其移动到代码中。我已正确添加FQDN&我尝试Powershell连接到控制台,但由于出现相同的错误,我无法连接。听起来您有一个潜在的网络问题,您应该能够ping并解决FQDN?。如果您无法让控制台工作,那么您的代码将永远无法工作,您可能希望尝试中的调试步骤
Anonymous authentication "disabled"
Asp.net impersonation    "disabled"
Basic authentication     "disabled"
Digest authentication    "disabled"
Forms authentication     "disabled"
Windows authentication   "disabled"
Anonymous authentication "disabled"
Asp.net impersonation    "disabled"
Basic authentication     "disabled"
Digest authentication    "disabled"
Forms authentication     "disabled"
Windows authentication   "enabled"