Run the Exchange command without importing Exchange Snap-in every single time RRS feed

  • Question

  • Hello Team,

    We have the following requirement:

    We would like to have a front-end webpage where we have the below fields:

    Enter the Secondary SMTP adddress:


    When the user clicks on SUBMIT, it could trigger a Powershell script on the server which in-turn imports O365 Exchange Online Shell every time and adds the alias if available. If not - it shows the error to user immediately.

    Here is the problem - when 10 people from the team submits those requests, it connects to Exchange Online shell 10 times.

    Is there a way for me to send the requests to the same runspace (which already has connected PS session to Exchange Online) instead of creating a new PS session every time for requests ?

    I thought about queuing the requests in a DB but this won't help in showing instant results to user unless I query DB every single second which doesn't seem like a good idea... (we might not have hits to the webpage during night hours but our code still queries DB every single second)

    • Edited by Raghu.Ram Thursday, December 12, 2019 7:03 AM
    Thursday, December 12, 2019 6:59 AM

All replies

  • This is not a web forum. Please post web questions in the correct forum for web development.


    We cannot design web sites for you as this is an administrative scripting forum and not a web development forum or a free code writing forum.  Your questions have nothing to do with PowerShell scripting.

    Also your question shows that you have little understanding or training in computer technology beyond the desktop. I recommend contacting a consulting company to help you identify and solve your needs. As asked, your requirement cannot be accomplished by PowerShell or any other technology. Webs and clients do not work as you suggest and your real requirement will make this clear. A trained consultant can help you find a solution but it will not work as you outline.

    In addition Exchange can be managed through Exchange remoting which is the normal method of doing this hwever, irt takes considerable technical skill and training to set up the system to meet your requirements.

    See: https://docs.microsoft.com/en-us/powershell/exchange/exchange-server/connect-to-exchange-servers-using-remote-powershell?view=exchange-ps

    Post in Exchange forum for assistance with Exchange and PowerShell.


    • Edited by jrv Thursday, December 12, 2019 8:24 AM
    Thursday, December 12, 2019 8:18 AM
  • Sounds like, you did not understand my requirement. I actually did the whole thing (using Apache/PHP/PS) but the only problem is that it launches PS and then connects to Exchange Remote Shell every single time.. I wanted to avoid these multiple connection attempts to Exchange Shell. FYI - I have been working with Exchange Management Shell since 6 years.. I will wait, if some one else has idea about passing the commands like Get-Mailbox into existing instance of EMS rather than launching a new one everytime
    And Let’s not worry about web page stuff, could we just focus on the powershell part of the query and any thoughts on how to achieve that ? I just mentioned about web page stuff as well just to give the complete picture Thank you for responding 😀 anyway.
    • Edited by Raghu.Ram Thursday, December 12, 2019 1:05 PM
    Thursday, December 12, 2019 1:01 PM
  • How can you use EMS without loading it? If you don't want to load the Exchange support then don't load it. It is very hard to understand what you are asking.

    I know the shell  it loads when you ask it to load.  If you just run PowerShell then it does not load except when you tell it to load.

    Without your code this is impossible to answer or understand.

    If you want to run PowerShell without a shell then you have to use the SDK to host PowerShell.  This would be how we do it if running a script from a web server.  The Exchange snapin is never a part of this.


    Thursday, December 12, 2019 1:11 PM
  • Yes.. I am wondering around that... Something like Invoke-command and use the session switch to run it against EMS.. I am not sure of a way to acheive this.. Do you guys think it’s not possible at all ? I launch EMS instance.. I would like to launch another Powershell instance.. I wanted to send the commands from this instance to EMS.. No way to do this ?
    Thursday, December 12, 2019 1:43 PM
  • Hello Raghu.Ram,

    Does the webpage have the option to invoke the Powershell command by itself? This way you can have a serviceaccount run a single Powershell session.

    This is not so good for your change management though. The serviceaccount will make all the changes in the environment.

    Could please let me know?



    Thursday, December 12, 2019 2:15 PM
  • Yes Niels, it can launch powershell with on click Submit button. But it’s launching different powershell instances and thus imports Exchange shell multiple times. I am wondering if there is any way to just import shell once and leverage the same (existing) session for all requests.. I wasn’t able to figure out this part.
    • Edited by Raghu.Ram Thursday, December 12, 2019 2:46 PM
    Thursday, December 12, 2019 2:45 PM
  • Could you please paint the picture with a High Level Design?

    If this is not possible you coudl take a look at this tool from a brilliant guy:



    Thursday, December 12, 2019 2:50 PM
  • I can add one thing that you are ignoring. PowerShell can attach to an existing session which can have anything pre-loaded. Instead of invoking PowerShell just use the "attach to the session by name.

    Start6 the session with the web application and have the session detached. Use a global for the name then use Connect-PsSession to acquire the existing session. Run your commands and detach.

    Disconnect-PsSession <session name>
    $session = Connect-PsSession <session name>
    Invoke-Command -Session $session -ScriptBlock $sb

    This can be made to work with a simple PS call in a web server.

    Remember that running PS in a web presents some tricky security issues so the session should be of limited scope.


    Thursday, December 12, 2019 3:07 PM