执行远程Powershell脚本时出错(Exchange 2010)

时间:2011-02-22 21:47:37

标签: c# powershell exchange-server

我写了一个非常小的应用程序,它访问Exchanger Server 2010 SP1的远程电源外壳并执行一些脚本。这是示例代码。一切都在尝试和阻止。

  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();

我正在尝试设置MailContact的PrimarySmtpAddress,但由于某些原因,我收到以下异常:

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

我认为它必须归因于序列化/反序列化。有人知道如何正确传递电子邮件地址的价值吗?

任何提示帮助都将受到高度赞赏!

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

尝试不使用SMTP:限定符。这已经隐含在-primarySMTPAddress参数中。