Solved

Only import specific cmdlets from Exchange?

Posted on 2014-11-24
8
224 Views
Last Modified: 2014-11-29
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.
0
Comment
Question by:garryshape
  • 3
  • 3
  • 2
8 Comments
 
LVL 10

Expert Comment

by:Michael Ian Claridge
ID: 40463754
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
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 40465112
Import-pssession has some parameter which will allow you to import specific commands.. I didn't get a chance to test it but you can try and see if it works for you.

import-pssession $exchangesession -type cmdlet -name Get-Mailbox,Get-User -FormatTypeName *

Open in new window

or
import-pssession $exchangesession -commandname Get-Mailbox -formattypename *

Open in new window


Ref : http://ss64.com/ps/import-pssession.html
0
 

Author Closing Comment

by:garryshape
ID: 40465164
Amazing. I appear to have overlooked that part in the MS examples. This link is much more helpful.
Thank you
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 10

Expert Comment

by:Michael Ian Claridge
ID: 40465244
@Subsun AWESOME, I will take note :)
0
 

Author Comment

by:garryshape
ID: 40465738
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.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 40466276
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.
0
 

Author Comment

by:garryshape
ID: 40466755
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)
0
 
LVL 40

Expert Comment

by:Subsun
ID: 40471977
I will test this the pssession import in my environment and let you know the result..
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question