code master
code master

Reputation: 2026

Error executing remote Power Shell Script (Exchange 2010)

I wrote a very small application, which access the Remote Power Shell of Exchanger Server 2010 SP1 and execute some scripts. Here is the sample code. Everything is in try and catch block.

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

I am trying to set PrimarySmtpAddress of a MailContact, but for some reasons I am getting the following exception:

System.Management.Automation.RemoteException: Cannot process argument transformation on parameter 'PrimarySmtpAddress'. Cannot convert value "SMTP:[email protected]" to type "Microsoft.Exchange.Data.SmtpAddress"

I think its must be due to serialization/de-serialization. Does someone have any idea on how to correctly pass the email address's value?

Any hint help will be highly appreciated!

Upvotes: 0

Views: 2096

Answers (2)

mjolinor
mjolinor

Reputation: 68243

Try leaving off the SMTP: qualifier. That's already implicit in the -primarySMTPAddress parameter.

Upvotes: 1

Davide Piras
Davide Piras

Reputation: 44595

I think you are confusing smtp server address with email address, try to pass something like smtp.yourcomapnydomain.com instead of such email address and test again.

Upvotes: 2

Related Questions