【问题标题】:Error executing remote Power Shell Script (Exchange 2010)执行远程 Powershell 脚本时出错 (Exchange 2010)
【发布时间】:2011-07-02 08:18:56
【问题描述】:

我编写了一个非常小的应用程序,它访问 Exchanger Server 2010 SP1 的 Remote Power Shell 并执行一些脚本。这是示例代码。一切都在 try and catch 块中。

  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”

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

任何提示帮助将不胜感激!

【问题讨论】:

    标签: c# powershell exchange-server


    【解决方案1】:

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

    【讨论】:

    • 我正在尝试设置 MailContact 的 PrimarySmtpAddress。例如,myEmailAddress@domain.com 是这样的地址之一?如果我必须设置除 ExternalEmailddress 之外的其他电子邮件地址,您认为正确的格式应该是什么?
    【解决方案2】:

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

    【讨论】:

    • 我没有通过任何 SMTP 限定符,您在我的代码中看不到吗?我关心的是为 MailContact 设置两个电子邮件地址。一个是 ExternalEmailAddress,其他可以是任何。但是,据我所知,我们只能有一个 ExternalEmailAddress,但我应该使用哪个属性来设置额外的电子邮件地址
    • 您可以看到这段代码清楚地表明我没有添加合格的 SMTP:~~command.AddParameter("PrimarySmtpAddress", "myEmailAddress@domain.com");~~
    • 刚刚意识到我正在做的错误。感谢您指出它;)
    猜你喜欢
    • 2017-04-27
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 1970-01-01
    • 2022-10-12
    • 2013-12-04
    相关资源
    最近更新 更多