none
exchange2010sp3 i want to operating exchange management shell by python,how can i do ? RRS feed

  • Question

  • i use C# using System.Management.Automation; to Remote operation exchange management shell

    such as move DB、new mailbox...

    here is my code :

    using System.Management.Automation;
    using System.Management.Automation.Runspaces;
    private static string adminUser = @"contoso\administrator";
    private static string adminPwd = "password";
    public WSManConnectionInfo mailexloginin()
    {
    	SecureString ssRunasPassword = new SecureString();
            foreach (char x in adminPwd)
            {
            	ssRunasPassword.AppendChar(x);
             }
    	PSCredential credentials = new PSCredential(adminUser, ssRunasPassword);
            var connInfo = new WSManConnectionInfo(new Uri("http://192.168.0.1/PowerShell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credentials);
    	connInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
             return connInfo;
    }


    but now i want to use python to  Achieve this demand

    I can't find a library for this.

    I don't know how the above code is implemented.

    I noticed a url:

    var connInfo = new WSManConnectionInfo(new Uri("http://192.168.0.1/PowerShell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credentials);

    how can i use this url to do something?

    Monday, November 5, 2018 9:30 AM

All replies

  • Hi 绅士aa,

    You could refer to the following code to connection Exchange Shell by using C#:

    PSCredential creds = new PSCredential(userName, securePassword);
    System.Uri uri = new Uri("http://Exchange-Server/powershell?serializationLevel=Full");
    
    Runspace runspace = RunspaceFactory.CreateRunspace();
    
    PowerShell powershell = PowerShell.Create();
    PSCommand command = new PSCommand();
    command.AddCommand("New-PSSession");
    command.AddParameter("ConfigurationName", "Microsoft.Exchange");
    command.AddParameter("ConnectionUri", uri);
    command.AddParameter("Credential", creds);
    command.AddParameter("Authentication", "Default");
    powershell.Commands = command;
    runspace.Open(); powershell.Runspace = runspace;
    Collection<PSSession> result = powershell.Invoke<PSSession>();
    
    powershell = PowerShell.Create();
    command = new PSCommand();
    command.AddCommand("Set-Variable");
    command.AddParameter("Name", "ra");
    command.AddParameter("Value", result[0]);
    powershell.Commands = command;
    powershell.Runspace = runspace;
    powershell.Invoke();
    
    powershell = PowerShell.Create();
    command = new PSCommand();
    command.AddScript("Import-PSSession -Session $ra");
    powershell.Commands = command;
    powershell.Runspace = runspace;
    powershell.Invoke();

    For more information, please see the link:

    Connect to Microsoft Exchange PowerShell within C#

    Also, for the python, unfortunately, I didn't find some available links to achieve it. Have you checked this link:

    How can I run command in Microsoft Exchange Server Powershell through Python script?

    Regards,

    Simon


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Tuesday, November 6, 2018 9:27 AM
  • Hi 绅士aa,

    I can see that this thread is still open and you did not posted on this thread since last 20 days.

    Has your issue been solved yet?

    if your issue is still exist then let us know about that, we will try to suggest you further to solve the issue.

    Please remember to mark the replies as answers if they helped. Please help us close the thread. Thanks for your understanding.

    Regards,

    Simon


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Monday, December 3, 2018 12:37 AM