Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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# 执行远程powershell脚本时出错(Exchange 2010)_C#_Powershell_Exchange Server - Fatal编程技术网

C# 执行远程powershell脚本时出错(Exchange 2010)

C# 执行远程powershell脚本时出错(Exchange 2010),c#,powershell,exchange-server,C#,Powershell,Exchange Server,我编写了一个非常小的应用程序,它访问ExchangeServer2010SP1的远程powershell并执行一些脚本。下面是示例代码。一切都在试抓阶段 string insecurePassword = "mypassword"; SecureString securePassword = new SecureString(); foreach (char passChar in insecurePassword.ToCharArray()) { securePasswo

我编写了一个非常小的应用程序,它访问ExchangeServer2010SP1的远程powershell并执行一些脚本。下面是示例代码。一切都在试抓阶段

  string insecurePassword = "mypassword";
  SecureString securePassword = new SecureString();
  foreach (char passChar in insecurePassword.ToCharArray())
  {
   securePassword.AppendChar(passChar);
  }
  PSCredential credential = new PSCredential("mydomain\\administrator", securePassword);

  WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://exchange2010.domain.com/powershell?serializationLevel=Full"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
  connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
  Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
  PowerShell powershell = PowerShell.Create();
  PSCommand command = new PSCommand();
  ICollection<System.Management.Automation.PSObject> results;


//The command I want to execute is Set-MailContact with some parameters.



    command.AddCommand("Set-MailContact");
    command.AddParameter("Identity", "SomeIdentityOfContact");
    command.AddParameter("EmailAddressPolicyEnabled", false);
    command.AddParameter("PrimarySmtpAddress", "myEmailAddress@domain.com");
    command.AddParameter("Confirm", false);
    command.AddParameter("Force", true);
    powershell.Commands = command;

    // open the remote runspace
    runspace.Open();
    // associate the runspace with powershell
    powershell.Runspace = runspace;
    // invoke the powershell to obtain the results
    results = powershell.Invoke();
string unsecurepassword=“mypassword”;
SecureString securePassword=新SecureString();
foreach(unsecurepassword.ToCharArray()中的char passChar)
{
AppendChar(passChar);
}
PSCredential=新的PSCredential(“mydomain\\administrator”,securePassword);
WSManConnectionInfo connectionInfo=新的WSManConnectionInfo(新的Uri(“http://exchange2010.domain.com/powershell?serializationLevel=Full"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange“,凭证);
connectionInfo.AuthenticationMechanism=AuthenticationMechanism.Kerberos;
Runspace Runspace=System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
PowerShell PowerShell=PowerShell.Create();
PSCommand=新PSCommand();
i收集结果;
//我要执行的命令是使用一些参数设置为MailContact。
AddCommand(“设置邮件联系人”);
AddParameter(“Identity”、“SomeIdentityOfContact”);
AddParameter(“EmailAddressPolicyEnabled”,false);
AddParameter(“PrimarySmtpAddress”myEmailAddress@domain.com");
command.AddParameter(“确认”,false);
command.AddParameter(“Force”,true);
powershell.Commands=command;
//打开远程运行空间
Open();
//将运行空间与powershell关联
powershell.Runspace=运行空间;
//调用powershell以获取结果
结果=powershell.Invoke();
我正在尝试设置MailContact的PrimarySmtpAddress,但由于某些原因,我遇到以下异常:

System.Management.Automation.RemoteException:无法处理参数“PrimarySmtpAddress”的参数转换。无法转换值“SMTP:myEmailAddress@domain.com“输入”Microsoft.Exchange.Data.SmtpAddress“

我想这一定是因为序列化/反序列化。有人知道如何正确传递电子邮件地址的值吗


任何提示和帮助都将不胜感激

我认为您混淆了smtp服务器地址和电子邮件地址,请尝试传递类似smtp.yourcomapnydomain.com的内容,而不是此类电子邮件地址,然后再次测试。

我认为您混淆了smtp服务器地址和电子邮件地址,尝试传递类似smtp.yourcomapnydomain.com的邮件,而不是这样的电子邮件地址,然后再次测试。

尝试取消smtp:qualifier。这已经隐含在-primarySMTPAddress参数中。

尝试取消SMTP:限定符。这已经隐含在-primarySMTPAddress参数中。

我正在尝试设置MailContact的primarySMTPAddress。例如myEmailAddress@domain.com有这样的地址吗?如果我必须在ExternalEmailAddress之外设置其他电子邮件地址,您认为正确的格式应该是什么?我正在尝试设置MailContact的PrimarySmtpAddress。例如myEmailAddress@domain.com有这样的地址吗?如果我必须在ExternalEmailAddress之外设置其他电子邮件地址,您认为正确的格式应该是什么?我没有传递任何SMTP限定符,您在我的代码中看不到吗?我关心的是为邮件联系人设置两个电子邮件地址。一个是ExternalEmailAddress,另一个可以是任意地址。然而,据我所知,我们只能有一个ExternalEmailAddress,但我应该使用哪个属性来设置额外的电子邮件地址您可以看到这段代码,它清楚地表明我没有添加SMTP限定:~~ command.AddParameter(“PrimarySmtpAddress”myEmailAddress@domain.com");~~刚刚意识到我犯了个错误。谢谢你指出这一点;)我没有传递任何SMTP限定符,您在我的代码中看不到吗?我关心的是为邮件联系人设置两个电子邮件地址。一个是ExternalEmailAddress,另一个可以是任意地址。然而,据我所知,我们只能有一个ExternalEmailAddress,但我应该使用哪个属性来设置额外的电子邮件地址您可以看到这段代码,它清楚地表明我没有添加SMTP限定:~~ command.AddParameter(“PrimarySmtpAddress”myEmailAddress@domain.com");~~刚刚意识到我犯了个错误。谢谢你指出这一点;)