I currently have a PowerShell script that is used for complex automated Exchange mailbox management. At times, the script will be running 20-40 instances simultaneously. The body of the script itself doesn't take much time to run; however, the script requires the ActiveDirectory module, the ServerManager module, and the Exchange 2010 PS-snapin. Loading these modules and snapins takes a few seconds when only 1 instance of the script is running. This is of course compounded once there are multiple instances of the script running. This ends up heading the box towards extremely high resource usage as it tries to load these modules 20-40 times simultaneously.
The question is this:
Is there someway to either keep the 3 required modules loaded into memory, so to speak, or is there some other way to optimize this that anyone can think of that will not require the loading of these 3 modules each time a new instance of the PowerShell script is called up?
Here is the basics of the script simplified (without: function bodies, if statements, error handling) - Let me know if you need any section added
CODE SNIPPIT:
param($ARG1,$ARG2,$ARG3) # Grab arguments from command line
# Load Modules
Import-Module ActiveDirectory
Import-Module ServerManager
Add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010
# Pass Arguments to Variables
$username = $ARG1
# Create Functions
function checkValidADUser {}
function checkOldMailbox {}
function chooseMailstore {}
function createMailbox {}
# Run Functions
checkValidADUser $username
checkOldMailbox $username
chooseMailstore $username
createMailbox $username $mailstore
You can try and load just the cmdlets you need from each module. With regards to the AD module you can speed up its loading by disabling the loading of the default AD drive. Some AD operations are also available via the Exchange commands, maybe you can skip the AD module. By the way, loading the E2010 snap-in is not supported.
$env:ADPS_LoadDefaultDrive = 0
Import-Module ActiveDirectory -Cmdlet Get-ADUser,Set-ADUser
Import-Module ServerManager ...
Add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With