我的代码抛出System.Management.Automation.Remoting.PSRemotingTransportException [英] My code throws System.Management.Automation.Remoting.PSRemotingTransportException

查看:279
本文介绍了我的代码抛出System.Management.Automation.Remoting.PSRemotingTransportException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

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

Hello All,

I am 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 Unfortunately, I landed in the below exception.

<pre>Exception: System.Management.Automation.Remoting.PSRemotingTransportException: Connecting to remote server failed with the following error message : <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>500 - Internal server error.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;} 
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;} 
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} 
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
 <div class="content-container"><fieldset>
  <h2>500 - Internal server error.</h2>
  <h3>There is a problem with the resource you are looking for, and it cannot be displayed.</h3>
 </fieldset></div>
</div>
</body>
</html>
 For more information, see the about_Remote_Troubleshooting Help topic.
</pre>



谁能帮我解决这个问题?

我为此引用了以下链接.



Can anyone help me to solve this?

I have referred the below link for this.
powershell[^]

Thanks & Regards
Sebastian

推荐答案

ra"); powershell.Commands =命令; powershell.Runspace =运行空间; powershell.Invoke(); System.Collections.ObjectModel.Collection< PSObject>结果= System.Collections.ObjectModel.Collection< PSObject>(); powershell = PowerShell.Create(); powershell.Runspace =运行空间; System.IO.StreamReader sr = System.IO.StreamReader(" ); powershell.AddScript(sr.ReadToEnd()); powershell.Runspace.SessionStateProxy.SetVariable(" " ); powershell.Runspace.SessionStateProxy.SetVariable(" " ); 结果= powershell.Invoke(); 如果(powershell.Streams.Error.Count > 1 ) { foreach (在 powershell.Streams.Error中,> Console.WriteLine(er.ErrorDetails); } 其他 { foreach (PSObject ps in 结果) { Console.WriteLine(ps.Properties [" ].Value.ToString()); } } } 捕获(例外) { MessageBox.Show(ex.Message); } 最终 { runspace.Dispose(); runspace = ; powershell.Dispose(); powershell = ; }
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 Unfortunately, I landed in the below exception.

<pre>Exception: System.Management.Automation.Remoting.PSRemotingTransportException: Connecting to remote server failed with the following error message : <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>500 - Internal server error.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;} 
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;} 
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} 
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
 <div class="content-container"><fieldset>
  <h2>500 - Internal server error.</h2>
  <h3>There is a problem with the resource you are looking for, and it cannot be displayed.</h3>
 </fieldset></div>
</div>
</body>
</html>
 For more information, see the about_Remote_Troubleshooting Help topic.
</pre>



谁能帮我解决这个问题?

我为此引用了以下链接.



Can anyone help me to solve this?

I have referred the below link for this.
powershell[^]

Thanks & Regards
Sebastian


首先,检查您的URL是否正确.
根据错误消息,Exchange服务器配置可能存在问题.
您是否按照以下说明启用了远程Exchange命令行管理程序:
First, check if your URL is correct.
Based on the error message, there might be something wrong with Exchange server configuration.
Have you enabled Remote Exchange Management Shell as described here:
http://www.msexchange.org/articles_tutorials/exchange-server-2010/management-administration/management-administration/exchange-2010-management-architecture-single-machine-manage-multiple-exchange-2010-organizations.html[^]

Also, this loop makes no sence, if there are errors only first will be thrown as new exception and execution will jump to your catch/finally section:
foreach (ErrorRecord current in powershell.Streams.Error)
{
    throw new Exception("Exception: " + current.Exception.ToString());
}


这篇关于我的代码抛出System.Management.Automation.Remoting.PSRemotingTransportException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆