Link to home
Start Free TrialLog in
Avatar of Garry Shape
Garry ShapeFlag for United States of America

asked on

Only import specific cmdlets from Exchange?

I have a script that starts a PSSession to exchange server

$exchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$exchangeserver/PowerShell/
Import-PSSession $exchangesession

Open in new window


The Import-PSSession will work just fine, however, it takes a while. I think it's because it's loading everything Exchange.
Since I'm only running a couple of cmdlets within powershell after the PSSession import, is there a way to only import specific cmdlets?

Such as Get-Mailbox, Get-User, etc.
Avatar of Member_2_4839798
Member_2_4839798

Hello,

This is very interesting. My understanding of PowerShell is that you cannot do this. Each cmdlet must be part of a module, modules are imported in turn importing each and every cmdlet.

I'm doing a lot of work with DSC Custom Resources at the moment and the same theory applies.

Kindest regards

Michael
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Garry Shape

ASKER

Amazing. I appear to have overlooked that part in the MS examples. This link is much more helpful.
Thank you
@Subsun AWESOME, I will take note :)
Well, interestingly, my theory was wrong:
Importing the specific commands does not make the script load any faster.
It actually takes about 10 seconds longer.
I timed it because I have a Windows Form script driven by powershell and I time based on how soon the form opens up.
Consistently, with the import-pssession importing the entire module, is 10 seconds faster.

I do wonder importing specific commands helps lessen the strain on the server, if the script is being re-run multiple times, however.
Are you sure Importing the session takes the major % of the script starting time? in my experience loading the Exchange Add-in locally on exchange server itself may take 6- 10 seconds depends on the capacity of server. If you think the load time for your script is abnormal then you may post another question including the script to check if anything wrong about the script or to check if anything can be done to make it better.
No the majority of the load time is due to the script in its entirety.
I'm just finding that for some reason, it takes about 10 seconds longer when I change the single line of code that imports the pssession to only import cmdlets (Get-Mailbox, Add-MailboxPermissions, Add-ADPermission)
I will test this the pssession import in my environment and let you know the result..