【问题标题】:Error while executing Exchange powershell commands from C# program从 C# 程序执行 Exchange powershell 命令时出错
【发布时间】:2013-12-09 08:52:34
【问题描述】:

我尝试使用以下代码从 Exchange 服务器获取邮箱。我在运行程序时遇到问题

static void Main(string[] args)
{
   string user = "Domain\\username";
   SecureString passwd = new SecureString();
   foreach (char c in "Password")
   {
        passwd.AppendChar(c);
   }
   PSCredential cred = new PSCredential(user, passwd);
   WSManConnectionInfo ConnInfo = new WSManConnectionInfo(new Uri(liveIdconnectionUri), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", cred);
   ConnInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
   ConnInfo.MaximumConnectionRedirectionCount = 2;
   //ConnInfo.ProxyAccessType = System.Management.Automation.Remoting.ProxyAccessType.AutoDetect;
   Runspace ExchangeRunspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(ConnInfo);
   PowerShell powershell = PowerShell.Create();
   PSCommand command = new PSCommand();
   command.AddCommand("Get-Mailbox");
   command.AddParameter("Identity", user);
   powershell.Commands = command;
   // open the remote runspace
   ExchangeRunspace.Open();
   // associate the runspace with powershell
   powershell.Runspace = ExchangeRunspace;
   // invoke the powershell to obtain the results
   powershell.Invoke();
   Collection<PSObject> results = powershell.Invoke();

   foreach (PSObject obj in results)
   {
        PSMemberInfoCollection<PSPropertyInfo> propInfos = obj.Properties;
        Console.WriteLine("********************");
        foreach (PSPropertyInfo propInfo in propInfos)
        {
            string propInfoValue = (propInfo.Value == null) ? "" : propInfo.Value.ToString();
            Console.WriteLine("{0} --> {1}", propInfo.Name, propInfoValue);
        }


    }
    Console.ReadLine();
}

运行上述代码时出现以下错误

Connecting to remote server failed with the following error message : WinRM cannot process the request. The following error occured while using Kerberos authentication: There are currently no logon servers available to service the logon request.  
 Possible causes are:
  -The user name or password specified are invalid.
  -Kerberos is used when no authentication method and no user name are specified.
  -Kerberos accepts domain user names, but not local user names.
  -The Service Principal Name (SPN) for the remote computer name and port does not exist.
  -The client and remote computers are in different domains and there is no trust between the two domains.
 After checking for the above issues, try the following:
  -Check the Event Viewer for events related to authentication.
  -Change the authentication method; add the destination computer to the WinRM TrustedHosts configuration setting or use HTTPS transport.
 Note that computers in the TrustedHosts list might not be authenticated.
   -For more information about WinRM configuration, run the following command: winrm help config. For more information, see the about_Remote_Troubleshooting Help topic."

即使我运行命令 'New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $liveIdconnectionUri -credential "user@example.com" -authentication kerberos',我也会收到同样的错误。

任何解决此问题的指针都会非常有帮助。在此先感谢。

【问题讨论】:

    标签: c# powershell exchange-server-2010


    【解决方案1】:

    如果无法访问系统,就很难为您提供确切的原因。我会尝试一些事情:

    * 确保您已正确设置远程处理并且防火墙中存在 WSMAN 异常。尝试运行“winrm quickconfig”。

    * 确保您已为服务器设置正确的远程脚本执行策略。请参阅(获取/设置)-ExecutionPolicy

    * 确保传递了正确的域凭据,并且可以从目标计算机向域 ctrlr 进行身份验证。

    【讨论】:

      【解决方案2】:
                  string username = "Hitesh";
                  string password = "Shrimali";
                  string sSAMAccount = "SAMAccountName"; //here Also you can use Your emailAddress
      
                  var lyncPW = GetSecurePassword(password);
      
                  PSCredential creds = new PSCredential(username, lyncPW);
                  Runspace m_RunSpace = RunspaceFactory.CreateRunspace();
                  PowerShell powershell = PowerShell.Create();
                  m_RunSpace.Open();
      
      
                  PSCommand CmdCredential = new PSCommand();
                  CmdCredential.AddCommand("Get-Credential");
                  CmdCredential.AddParameter("Credential", creds);
                  powershell.Commands = CmdCredential;
                  powershell.Runspace = m_RunSpace;
                  powershell.Invoke();
      
      
                  PSCommand command = new PSCommand();
                  command.AddCommand("New-PSSession");
                  command.AddParameter("ConfigurationName", "Microsoft.Exchange");
                  command.AddParameter("ConnectionUri", "YourURL/PowerShell/"); //Your Exchange URL
                  command.AddParameter("Credential", creds);
                  command.AddParameter("Authentication", "Default");
                  powershell.Commands = command;
                  powershell.Runspace = m_RunSpace;
                  powershell.Invoke();
      
      
                  PSSessionOption sessionOption = new PSSessionOption();
                  sessionOption.SkipCACheck = true;
                  sessionOption.SkipCNCheck = true;
                  sessionOption.SkipRevocationCheck = true;
                  command.AddParameter("SessionOption", sessionOption);
                  powershell.Commands = command;
                  powershell.Runspace = m_RunSpace;
      
                  Collection<PSSession> result = powershell.Invoke<PSSession>();
                  PSSession ps = result.FirstOrDefault();
      
                  RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(m_RunSpace);
                  runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted -Scope CurrentUser");
      
      
                  PSCommand cmdImportSession = new PSCommand();
                  cmdImportSession.AddCommand("Import-PSSession");
                  cmdImportSession.AddParameter("Session", ps);
                  cmdImportSession.AddParameter("AllowClobber", true);
                  powershell.Commands = cmdImportSession;
                  powershell.Runspace = m_RunSpace;
                  powershell.Invoke();
      
      
                  PSCommand cmdNewMoveRequest = new PSCommand();
                  cmdNewMoveRequest.AddCommand("New-MoveRequest");
                  cmdNewMoveRequest.AddParameter("Identity", sSAMAccount);
                  cmdNewMoveRequest.AddParameter("TargetDatabase", "BoxerProperty Disabled Users Mailbox Database");
                  powershell.Commands = cmdNewMoveRequest;
                  powershell.Runspace = m_RunSpace;
                  powershell.Invoke();
      
      
                  foreach (ErrorRecord current in powershell.Streams.Error)
                  {
                      // log errors
                  }
      
                  m_RunSpace.Close();
      

      【讨论】:

      • 我使用了“New-MoveRequest”,就像您可以在此处使用任何命令一样。 - 谢谢 -Hitesh shrimali
      猜你喜欢
      • 1970-01-01
      • 2011-07-02
      • 1970-01-01
      • 2015-07-16
      • 2018-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多