none
Error executing remote Power Shell Script (Exchange 2010): Conversion of value "SMTP:myEmailAddress@domain.com" to type "Microsoft.Exchange.Data.SmtpAddress" ? RRS feed

  • Question

  • Hi,

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

     

    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:myEmailAddress@domain.com" to type "Microsoft.Exchange.Data.SmtpAddress" Error: "SMTP:myEmailAddress@domain.com" is not a valid SMTP address"


    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!

    Tuesday, February 22, 2011 9:21 PM

Answers

  • I would suggest you use Pipeline object instead which should get around this type of issue eg

         System.Net.ServicePointManager.ServerCertificateValidationCallback +=
      delegate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                  System.Security.Cryptography.X509Certificates.X509Chain chain,
                  System.Net.Security.SslPolicyErrors sslPolicyErrors)
      {
        return true; // **** Always accept
      };
          System.Security.SecureString secureString = new System.Security.SecureString();
          string myPassword = "password";
          foreach (char c in myPassword)
            secureString.AppendChar(c);
          PSCredential credential = new PSCredential("administrator", secureString);
          WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://ex2010dev.exdev.msgdevelop.com/powershell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
          connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
          connectionInfo.SkipCACheck = true;
          connectionInfo.SkipCNCheck = true;
          Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
          runspace.Open();
          System.Security.SecureString newPaswordSecureString = new System.Security.SecureString();
          Command command = new Command("Set-MailContact");
          command.Parameters.Add("Identity", "blah");
          command.Parameters.Add("EmailAddressPolicyEnabled", false);
          command.Parameters.Add("PrimarySmtpAddress", "myEmailAddress1@domain.com");
          command.Parameters.Add("Confirm", false);
          command.Parameters.Add("Force", true);
    
          Pipeline cmd = runspace.CreatePipeline();
          cmd.Commands.Add(command);
          Collection<PSObject> results = cmd.Invoke();
          runspace.Close();
    

    Cheers
    Glen 

    • Marked as answer by Falak Mahmood Thursday, February 24, 2011 9:10 PM
    Thursday, February 24, 2011 2:52 AM

All replies

  • I would suggest you use Pipeline object instead which should get around this type of issue eg

         System.Net.ServicePointManager.ServerCertificateValidationCallback +=
      delegate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                  System.Security.Cryptography.X509Certificates.X509Chain chain,
                  System.Net.Security.SslPolicyErrors sslPolicyErrors)
      {
        return true; // **** Always accept
      };
          System.Security.SecureString secureString = new System.Security.SecureString();
          string myPassword = "password";
          foreach (char c in myPassword)
            secureString.AppendChar(c);
          PSCredential credential = new PSCredential("administrator", secureString);
          WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://ex2010dev.exdev.msgdevelop.com/powershell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
          connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
          connectionInfo.SkipCACheck = true;
          connectionInfo.SkipCNCheck = true;
          Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
          runspace.Open();
          System.Security.SecureString newPaswordSecureString = new System.Security.SecureString();
          Command command = new Command("Set-MailContact");
          command.Parameters.Add("Identity", "blah");
          command.Parameters.Add("EmailAddressPolicyEnabled", false);
          command.Parameters.Add("PrimarySmtpAddress", "myEmailAddress1@domain.com");
          command.Parameters.Add("Confirm", false);
          command.Parameters.Add("Force", true);
    
          Pipeline cmd = runspace.CreatePipeline();
          cmd.Commands.Add(command);
          Collection<PSObject> results = cmd.Invoke();
          runspace.Close();
    

    Cheers
    Glen 

    • Marked as answer by Falak Mahmood Thursday, February 24, 2011 9:10 PM
    Thursday, February 24, 2011 2:52 AM
  • Hi Glen,,

    Great! it worked like a charm!

    Thursday, February 24, 2011 9:11 PM