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. Exchange Powershell with C#

Exchange Powershell with C#

Scheduled Pinned Locked Moved C#
windows-adminhelpcsharpdatabasesysadmin
1 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.
  • J Offline
    J Offline
    JD86
    wrote on last edited by
    #1

    I don't really have a big issue with the powershell working because it works just fine. The problem I'm having is the method exits before the command is actually completed on the Exchange server. I think it may have something to do with the way Exchange throttles powershell commands. Is there a way to use powershell with Exchange but have it completely wait until the command is finish before refreshing my page? Let me give you an example: A user logs into the web interface and decides to enable their user for Exchange. So the user clicks Enable Exchange. This fires a powershell to Exchange running the Enable-Mailbox command. After it completes the command it retrieves updated information from Active Directory / Exchange about the users mailbox. The problem I have is when it tries to retrieve the updated information it hasn't had time to populate in Exchange so it still thinks there isn't a mailbox. I know I could put a sleep in there but I rather do something cleaner. Here is my base class:

    public void RunPowershell(PSCommand pscmd = null, String script = null)
    {
    Runspace remoteRunspace = null;
    PSDataCollection errors = new PSDataCollection();

            OpenRunspace(\_uri, \_schema, \_username, \_password, ref remoteRunspace);
    
            remoteRunspace.Open();
            powershell.Runspace = remoteRunspace;
            powershell.Streams.ClearStreams();
            powershell.Commands.Clear();
    
            if (pscmd != null)
            {
                String debugLog = "\[RunPowershell\] Executing powershell: ";
                foreach (Command cmd in pscmd.Commands)
                {
                    debugLog += cmd.CommandText;
                    foreach (CommandParameter cp in cmd.Parameters)
                        debugLog += String.Format("-{0} {1}", cp.Name, cp.Value.ToString());
                }
    
                Log.Debug(debugLog);
                powershell.Commands = pscmd;
            }
            else if (script != null)
            {
                Log.Debug("\[RunPowershell\] Executing powershell: " + script);
                powershell.Commands.AddScript(script);
            }
    
            powershell.Invoke();
            ThrowIfError();
    
            Log.Debug("\[Exited\] RunPowershell");
        }
    
        private void OpenRunspace(string uri, string schema, string username, string password, ref Runspace remoteRunspace)
        {
            SecureString securePa
    
    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