Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimizing PowerShell module imports

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
like image 702
Eli Avatar asked Feb 07 '12 03:02

Eli


1 Answers

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
like image 58
Shay Levy Avatar answered Oct 07 '22 19:10

Shay Levy