Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Looking for an example of remote Exchange Shell in c sharp

Looking for an example of remote Exchange Shell in c sharp

Scheduled Pinned Locked Moved C#
csharpsysadminlinuxtoolstutorial
4 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    turbosupramk3
    wrote on last edited by
    #1

    Hi, I have executed Exchange 2007 commands inside of c sharp code before, but have not attempted to do so with 2013 until now. There are no management tools installed on the machines this app will run on, so I believe I have to open up a remote PSsession to an Exchange 2013 server which is something else I have not done. I was wondering if any one had an example of doing this with an Exchange 2013 specific cmdlet? Thank you

    T 1 Reply Last reply
    0
    • T turbosupramk3

      Hi, I have executed Exchange 2007 commands inside of c sharp code before, but have not attempted to do so with 2013 until now. There are no management tools installed on the machines this app will run on, so I believe I have to open up a remote PSsession to an Exchange 2013 server which is something else I have not done. I was wondering if any one had an example of doing this with an Exchange 2013 specific cmdlet? Thank you

      T Offline
      T Offline
      turbosupramk3
      wrote on last edited by
      #2

      I found this

      $session = new-pssession -configurationname microsoft.exchange -connectionuri http://exchangeserver.domain.com/powershell -auth kerberos -credential (get-credential)

      import-psession $session

      And I can at least see the commands in the powershell session now, so this is a start

      T 1 Reply Last reply
      0
      • T turbosupramk3

        I found this

        $session = new-pssession -configurationname microsoft.exchange -connectionuri http://exchangeserver.domain.com/powershell -auth kerberos -credential (get-credential)

        import-psession $session

        And I can at least see the commands in the powershell session now, so this is a start

        T Offline
        T Offline
        turbosupramk3
        wrote on last edited by
        #3

        This is telling me the "State of runspace is not valid for this operation" at line 1082, which is the var results2 = powerShell.Invoke(); line. Any ideas?

        using (PowerShell powerShell = PowerShell.Create())
        {
        powerShell.Runspace = runspace;
        string un = @"domain\username";
        System.Security.SecureString pw = new System.Security.SecureString();
        string password = "password";
        foreach (char ch in password)
        {
        pw.AppendChar(ch);
        }
        PSCredential cred = new PSCredential(un, pw);

                        string CONNECTION\_URI = @"http://exchangeserver.com/powershell";
                        PSCommand psSession = new PSCommand();
                        psSession.AddCommand("$session = New-PSSession");
                        psSession.AddParameter("ConfigurationName", "Microsoft.Exchange");
                        psSession.AddParameter("ConnectionUri", new Uri(CONNECTION\_URI));
                        psSession.AddParameter("Kerberos");
                        psSession.AddParameter("Credential", cred);
                                            psSession.AddParameter("AllowRedirection");
        
                        psSession.AddCommand("Import-PSSession");
                        psSession.AddParameter("$session");
        
                        psSession.AddCommand("Get-MailboxDatabaseCopyStatus");
                        psSession.AddParameter("databasename");
                        powerShell.Commands = psSession;
                        var results2 = powerShell.Invoke();
                        foreach (var item in results2)
                        {
                            MessageBox.Show(item.Members.ToString());
                        }
                    }
        
        T 1 Reply Last reply
        0
        • T turbosupramk3

          This is telling me the "State of runspace is not valid for this operation" at line 1082, which is the var results2 = powerShell.Invoke(); line. Any ideas?

          using (PowerShell powerShell = PowerShell.Create())
          {
          powerShell.Runspace = runspace;
          string un = @"domain\username";
          System.Security.SecureString pw = new System.Security.SecureString();
          string password = "password";
          foreach (char ch in password)
          {
          pw.AppendChar(ch);
          }
          PSCredential cred = new PSCredential(un, pw);

                          string CONNECTION\_URI = @"http://exchangeserver.com/powershell";
                          PSCommand psSession = new PSCommand();
                          psSession.AddCommand("$session = New-PSSession");
                          psSession.AddParameter("ConfigurationName", "Microsoft.Exchange");
                          psSession.AddParameter("ConnectionUri", new Uri(CONNECTION\_URI));
                          psSession.AddParameter("Kerberos");
                          psSession.AddParameter("Credential", cred);
                                              psSession.AddParameter("AllowRedirection");
          
                          psSession.AddCommand("Import-PSSession");
                          psSession.AddParameter("$session");
          
                          psSession.AddCommand("Get-MailboxDatabaseCopyStatus");
                          psSession.AddParameter("databasename");
                          powerShell.Commands = psSession;
                          var results2 = powerShell.Invoke();
                          foreach (var item in results2)
                          {
                              MessageBox.Show(item.Members.ToString());
                          }
                      }
          
          T Offline
          T Offline
          turbosupramk3
          wrote on last edited by
          #4

          In case someone comes across this in a search

          string un = @"domain\username";
          System.Security.SecureString pw = new System.Security.SecureString();
          string password = "password";
          string databaseName = "databasename";
          string exchangeServerName = "http://exchangeserver.com/powershell";
          string microsoftSchemaName = "http://schemas.microsoft.com/powershell/Microsoft.Exchange";
          foreach (char ch in password)
          {
          pw.AppendChar(ch);
          }
          PSCredential cred = new PSCredential(un, pw);

                  WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri(exchangeServerName), microsoftSchemaName, cred);
                  connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
          
                  using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
                  {
                      using (PowerShell powershell = PowerShell.Create())
                      {
                          powershell.AddCommand("Get-Mailboxdatabasecopystatus");
                          powershell.AddParameter("identity", databaseName);
                          powershell.AddCommand("select-object");
                          var props = new string\[\] { "name", "status" };
                          powershell.AddParameter("property", props);
                          runspace.Open();
                          powershell.Runspace = runspace;
          
                          Collection<PSObject> results = powershell.Invoke();
          
                          foreach (PSObject result in results)
                          {
                              MessageBox.Show(result.ToString());
                          }
                      }
                  }
          
          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups