none
Reusing a Powershell process for frequently occurring tasks RRS feed

  • Question

  • Hello,

    I've provided my company with a few scripts for a provisioning and out-processing Exchange 2010 mailboxes. It appears that there are large batches of work to do that are triggering a new Pshell process for each request, which is in turn using a lot of resources. I would like to refine the scripts to be more efficient. My thoughts were to run the scripts as jobs and close the process immediately, somehow have each task make use of an existing process, or maybe reuse an implicit remote session if that would help.

    Any feedback is greatly appreciated!

    Thanks,
    Matt

    Wednesday, June 3, 2015 5:48 PM

Answers

  • Rather than firing up the Exchange cmdlets to determine whether litigation hold is enabled, you can get the user object's msExchELCMailboxFlags attribute. If the 4th bit is set (i.e., decimal 8), then litigation hold is enabled for the account's mailbox. Example:


    $LITIGATION_HOLD_ENABLED = 8
    
    $identity = "username"
    $exchFlags = get-aduser $identity -properties msExchELCMailboxFlags | select-object -expandproperty msExchELCMailboxFlags
    if ( ($exchFlags -band $LITIGATION_HOLD_ENABLED) -ne 0 ) {
      "Litigation hold is enabled for $identity"
    }
    

    See the following thread for more information:

    https://social.technet.microsoft.com/Forums/en-US/c1c1c5f3-b340-4fc1-b0ec-ec1414a87749/


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by jrv Wednesday, June 3, 2015 8:30 PM
    • Marked as answer by Bill_StewartModerator Monday, July 13, 2015 3:29 PM
    Wednesday, June 3, 2015 8:07 PM
    Moderator

All replies

  • Cannot be of help for such a vague question.

    \_(ツ)_/

    Wednesday, June 3, 2015 6:08 PM
  • Sorry, I'm quite new to  pshell, and I didn't think I  would need to provide too much detail   for this.  We have an application that triggers a Pshell script.  It's being triggered very frequently and the number of Pshell processes are building up and stressing the server.   I thought the answer may be along the lines of methods of using Pshell in general, rather than specific to a script.  Exchange is a factor because of how you remote and use it's cmdlets, so I included that.  Here is the script.  Let me know  what else I can tell you.

    ==================================       

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto

    If ((Get-Mailbox -identity $A).LitigationHoldEnabled){

        If ($R = New-MailboxExportRequest -Name "$($A)-My-LitHold" -BatchName My-LitHold -Mailbox $A -FilePath \\server1\d$\folder1\$A-My-LitHoldExport.PST -AcceptLargeDataLoss -BadItemLimit 2147483647 | fl Status,Suspend,Name,FilePath,Mailbox,SourceDatabase | Out-String){

            Send-MailMessage -From LitigationHoldExporter@domain.com -SmtpServer exchange.domain.com -To joe@domain.com -Subject "Litigation Hold Export Started for $($A)" -Body "Contact the Messaging Team to investigate any problems.

            $R"
            }

            Else {Send-MailMessage -From LitigationHoldExporter@domain.com -SmtpServer exchange.domain.com -To joe@domain.com -Subject "Litigation Hold Export Started for $($A)" -Body "The Mailbox export failed to start for $($A).  Forward this message to the Messaging Team to investigate.

            $R"
            }

        }
        Else{Write-Host "Mailbox $($a) is not on litigation hold"}

    ==================================

    Wednesday, June 3, 2015 7:54 PM
  • Rather than firing up the Exchange cmdlets to determine whether litigation hold is enabled, you can get the user object's msExchELCMailboxFlags attribute. If the 4th bit is set (i.e., decimal 8), then litigation hold is enabled for the account's mailbox. Example:


    $LITIGATION_HOLD_ENABLED = 8
    
    $identity = "username"
    $exchFlags = get-aduser $identity -properties msExchELCMailboxFlags | select-object -expandproperty msExchELCMailboxFlags
    if ( ($exchFlags -band $LITIGATION_HOLD_ENABLED) -ne 0 ) {
      "Litigation hold is enabled for $identity"
    }
    

    See the following thread for more information:

    https://social.technet.microsoft.com/Forums/en-US/c1c1c5f3-b340-4fc1-b0ec-ec1414a87749/


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by jrv Wednesday, June 3, 2015 8:30 PM
    • Marked as answer by Bill_StewartModerator Monday, July 13, 2015 3:29 PM
    Wednesday, June 3, 2015 8:07 PM
    Moderator
  • That will help with that script.  Thank you!  There's also AD  property msExchLitigationHoldDate that looks like it would work too.  i can then only have  it connect to Exchange when necessary.  Very good.  

    There are a couple other scripts.  Another creates mailboxes for new users.  It will have to connect to Exchange every time, and this may actually be the one that is causing the real trouble.

    Is there no way to reuse an existing pshell process that already  has a connection to Exhange?  Or  amke use of the same session.  Is that a rediculous question?

    This scripting dude has some clues in his blog: Scaling and Queuing PowerShell Background Jobs: http://blogs.msdn.com/b/powershell/archive/2011/04/04/scaling-and-queuing-powershell-background-jobs.aspx

    I'm thinking of keeping a pshell process with exchange session open all the time, having it check periodically a txt file for new content, and queueing jobs based on a max limit of  pshell processes.  The checks...i don't know how to refer to them, to make sure the process is running, and the file content is new, etc seems quite involved, but that could really reduce overhead once I finish banging my head against the wall to complete it.

    Another thought I had is to start a session to exchange, invoke a start-job script to do the MB creation, export, or whatever, and then closing the pshell process sounds promising.  

    Wednesday, June 3, 2015 8:31 PM
  • Why is it a problem to open/create/close a session to crate a mailbox.  It is not a heavyweight thing.

    You have a dei9sng issue here that you need to contact an experienced developer to help you with.

    If the system currently works I recommend just leaving it alone.  If you have a specific performance issue then post that.  We cannot assist with vague questions

    Without code examples and error messages there is really no way to assist you.


    \_(ツ)_/

    Wednesday, June 3, 2015 8:34 PM
  • JRV... give me a break man.  I've told you I'm relatively new, and I'm trying to  give more detail.  I just provided a code example, and there is no error message.  The issue is as described, a large number of pshell instances being started rapidly and using a lot of memory and processor, enough to bring the box to a crawl.  I understand there may be be ways to make  the script more efficient in general, and I want to hear those, but I was at first asking about methods of using pshell.  I'm good with you sitting this one out, and if anyone else has feedback great.  

    We're also adding memory to the server, but I'm still concerned that will be a bandaid.  I'm still gathering information on the entire process, so maybe that will shed  some light.


    Wednesday, June 3, 2015 9:05 PM
  • I am sorry but there is not enough information to help you.  If this is critical then you would do well to hire a consultant that can work with you to sort this out.

    Without specific scripts there is no way to guess at what is happening. 


    \_(ツ)_/

    Wednesday, June 3, 2015 9:15 PM
  • JRV, I understand what you're saying, and yet I was still hoping for some feedback on if all of these triggered pshell tasks can make use of an existing pshell process, rather than have to  spin a new one up.

    A new outside consultant is not called for yet.

     
    Wednesday, June 3, 2015 9:37 PM
  • We have n idea what you are talking about.  What is a "triggered pshelll process"?  A process is an isolated program.  A process is started in many ways.  We no nothing about "triggered".

    PowerShell is a process.  You can find it by running get-process.

    get-process powershell

    Now- try to think about what you are asking and ask a question that is not arbitrary or vague.  Do not use terms you have made up like "triggered" and "pshell".  Use plain English and describe in non-technical but real language what problem it is that you are trying to solve.


    \_(ツ)_/

    Wednesday, June 3, 2015 10:33 PM
  • If you don't understand what I mean by triggered and pshell then you have the options to ask me to clarify.  Why you are asking for that well into the discussion I don't know.  Why are you speaking on behalf of the entire community, and why are you still responding at this point?  Is it just to frustrate me, or both  of us.  You're online demeanor feels more antagonistic than actually wanting to help. Don't worry about it, and next time you decide to chime in, in particular with someone who is a novice in this area, and as long as you are going to be representing the community, how about being a little more  inviting and accommodating.  And I think it goes without saying that I would rather  not hear from you when I ask  other  questions.
    Wednesday, June 3, 2015 11:22 PM
  • I guess you have no answer.  Is this just a fun game for you or do you really have a question?

    What is a trigger?  I asked that implicitly many times.


    \_(ツ)_/

    Wednesday, June 3, 2015 11:29 PM