none
c# programatically exporting transport rule RRS feed

  • Question

  • Exporting transport rule programmatically

    Hi ,

    I am trying to export transport rule programmatically.

    My code works for simple commands like

    get-exchangeserver, get-TransportRule etc but if I try to give command importing rules to some file it gives error that the given command is not recognized as cmdlet, script….

    My code is as below ( I have tried giving both windows authentication or user ‘s authentication who is exchange admin)

    WSManConnectionInfo connectionInfo = new WSManConnectionInfo(   serverUri,

                                                                                SHELL_URI,

                                                                                creds);

                try

                {

                    using (Runspace rs = RunspaceFactory.CreateRunspace(connectionInfo))

                    {

                        PowerShell psh = PowerShell.Create();

                        psh.Runspace = rs;

                        rs.Open();

    psh.AddCommand("$file = Export-TransportRuleCollection -ExportLegacyRules" +"Set-Content -Path " + @"C:\LegacyRules3.xml" + "-Value file.FileData -Encoding Byte");

                    

    psh.Invoke();

    I am using c# and exchange server 2010 PowerShell version is 1.0 the remoite os is winodws 7.

    Any idea what correction I should do?

    Regards,

    Abhagwat

    Monday, April 15, 2013 8:17 AM

Answers

  • Hi Laeeq Qazi,

    I tried code from below link:

    http://social.msdn.microsoft.com/Forums/en-US/sbappdev/thread/3c143111-eb14-4235-8084-5edec8202f8b/

    and now my code works for remote connection, but now I am trying for running the code directly on exchnag server and facing issue that is getting same error that the given command is not recognized as cmdlet, script….

    I have set ExecutionPolicy unrestricted still facing issue, below is not sure what is the problem,

    Let me know if you have sample code  to call script through c# exchange server(i.e not by remote method)

    Regards,

    Abhagwat

    • Marked as answer by ABBhagwat Thursday, April 18, 2013 8:18 AM
    Thursday, April 18, 2013 6:50 AM

All replies