Solved

Only import specific cmdlets from Exchange?

Posted on 2014-11-24
8
209 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
 
LVL 10

Expert Comment

by:Michael Ian Claridge
ID: 40465244
@Subsun AWESOME, I will take note :)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function onceā€¦
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now