本文介绍了PSRemoting传输异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

大家好,

我试图使用C#运行Exchange Server 2010监视命令.这是我的代码...

Hello All,

I was trying to run exchange server 2010 monitoring commands using C#. Here is my code...

string password = "abc123";
string userName = @"me.com\Administrator";
System.Uri uri = new Uri("http://192.168.1.194/powershell?serializationLevel=Full");
System.Security.SecureString securePassword = String2SecureString(password);
System.Management.Automation.PSCredential creds = new System.Management.Automation.PSCredential(userName, securePassword);
Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace();
PowerShell powershell = PowerShell.Create();
PSCommand command = new PSCommand();
command.AddCommand("New-PSSession");
command.AddParameter("ConfigurationName", "Microsoft.Exchange");
command.AddParameter("ConnectionUri", uri);
command.AddParameter("Credential", creds);
command.AddParameter("Authentication", "Default");
PSSessionOption sessionOption = new PSSessionOption();
sessionOption.SkipCACheck = true;
sessionOption.SkipCNCheck = true;
sessionOption.SkipRevocationCheck = true;
command.AddParameter("SessionOption", sessionOption);
powershell.Commands = command;
try
   {
       runspace.Open();
       powershell.Runspace = runspace;
       Collection<PSSession> result = powershell.Invoke<PSSession>();
       foreach (ErrorRecord current in powershell.Streams.Error)
       {
             throw new Exception("Exception: " + current.Exception.ToString());
       }
       if (result.Count != 1)
          throw new Exception("Unexpected number of Remote Runspace connections
          returned.");

       powershell = PowerShell.Create();
       command = new PSCommand();
       command.AddCommand("Set-Variable");
       command.AddParameter("Name", "ra");
       command.AddParameter("Value", result[0]);
       powershell.Commands = command;
       powershell.Runspace = runspace;
       powershell.Invoke();

       powershell = PowerShell.Create();
       command = new PSCommand();
       command.AddScript("Import-PSSession -Session $ra");
       powershell.Commands = command;
       powershell.Runspace = runspace;
       powershell.Invoke();

       System.Collections.ObjectModel.Collection<PSObject> results = new
       System.Collections.ObjectModel.Collection<PSObject>();

       powershell = PowerShell.Create();
       powershell.Runspace = runspace;

       System.IO.StreamReader sr = new System.IO.StreamReader("..\\..\\Script.ps1");
       powershell.AddScript(sr.ReadToEnd());
       powershell.Runspace.SessionStateProxy.SetVariable("proc", "C*");
       powershell.Runspace.SessionStateProxy.SetVariable("mbx", "*MBX");

       results = powershell.Invoke();

       if (powershell.Streams.Error.Count > 1)
       {
           foreach (ErrorRecord er in powershell.Streams.Error)
                  Console.WriteLine(er.ErrorDetails);
       }
       else
       {
           foreach (PSObject ps in results)
           {
                   Console.WriteLine(ps.Properties["Name"].Value.ToString());
           }
       }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        runspace.Dispose();
        runspace = null;
        powershell.Dispose();
        powershell = null;
     }



但是我遇到了以下错误...



But I have got the following error...

Exception: System.Management.Automation.Remoting.PSRemotingTransportException: Connecting to remote server failed with the following error message : The WinRM client cannot process the request. The WinRM client tried to use Negotiate authentication mechanism, but the destination computer (192.168.1.194:80) returned an 'access denied' error. Change the configuration to allow Negotiate authentication mechanism to be used or specify one of the authentication mechanisms supported by the server. To use Kerberos, specify the local computer name as the remote destination. Also verify that the client computer and the destination computer are joined to a domain. To use Basic, specify the local computer name as the remote destination, specify Basic authentication and provide user name and password. Possible authentication mechanisms reported by server: For more information, see the about_Remote_Troubleshooting Help topic.



解决这个问题的任何帮助都会很棒...

谢谢&问候

Sebastian



Any help to solve this would be great...

Thanks & Regards

Sebastian

推荐答案



但是我遇到了以下错误...



But I have got the following error...

Exception: System.Management.Automation.Remoting.PSRemotingTransportException: Connecting to remote server failed with the following error message : The WinRM client cannot process the request. The WinRM client tried to use Negotiate authentication mechanism, but the destination computer (192.168.1.194:80) returned an 'access denied' error. Change the configuration to allow Negotiate authentication mechanism to be used or specify one of the authentication mechanisms supported by the server. To use Kerberos, specify the local computer name as the remote destination. Also verify that the client computer and the destination computer are joined to a domain. To use Basic, specify the local computer name as the remote destination, specify Basic authentication and provide user name and password. Possible authentication mechanisms reported by server: For more information, see the about_Remote_Troubleshooting Help topic.



解决这个问题的任何帮助都会很棒...

谢谢&问候

Sebastian



Any help to solve this would be great...

Thanks & Regards

Sebastian



这篇关于PSRemoting传输异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 22:19