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

问题描述:

我编写了一个非常小的应用程序,它访问Exchanger Server 2010 SP1的远程电源外壳并执行一些脚本。这里是示例代码。一切都在尝试和赶上块。执行远程Power Shell脚本时出错(Exchange 2010)

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", "[email protected]"); 
    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:[email protected]”键入“Microsoft.Exchange.Data.SmtpAddress”

我认为它必须是由于序列化/反序列化。有人对如何正确传递电子邮件地址的价值有任何想法吗?

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

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

+0

我没有通过任何SMTP限定符,你不能在我的代码中看到?我担心的是为MailContact设置两个电子邮件地址。一个是ExternalEmailAddress,另一个可以是任何。然而,据我所知,我们只能有一个ExternalEmailAddress,但我应该使用哪个属性来设置额外的电子邮件地址 – 2011-02-23 08:31:29

+0

您可以看到这段代码清楚地显示了我没有添加SMTP限定:~~ command.AddParameter(“PrimarySmtpAddress “,”[email protected]“); ~~ – 2011-02-23 08:40:30

+0

刚刚意识到我正在做的错误。感谢您的支持;) – 2011-02-23 10:02:00

我认为你是令人困惑的smtp服务器地址与电子邮件地址,尝试通过类似smtp.yourcomapnydomain.com而不是这样的电子邮件地址,然后再次测试。

+0

我想设置MailContact的PrimarySmtpAddress。例如,[email protected]就是这样的地址之一吗?如果我必须设置除ExternalEmailddress外的其他电子邮件地址,您认为应该采用正确的格式吗? – 2011-02-23 08:26:06