Solved

Exchange modules in Power Shell ISE

Posted on 2014-09-24
14
141 Views
Last Modified: 2015-02-04
Is there a way to load the exchange modules into Power Shell ISE so I can run Exchange 2010 commands from within Power shell for scripting and scheduling jobs through task manager?  The exchange 2010 Management tools are already installe don my scripting server.
0
Comment
Question by:georgedschneider
  • 6
  • 5
14 Comments
 
LVL 12

Accepted Solution

by:
Vaseem Mohammed earned 375 total points
ID: 40342488
Run this 1liner in ISE with administrator rights.
Add-pssnapin microsoft.exchange.management.powershell.e2010
0
 

Author Comment

by:georgedschneider
ID: 40342586
Silly me I thought I had run this but apparently not.  Is there any way to have this automatically so if I schedule a script to run through task scheduler it will?
0
 

Assisted Solution

by:Roberto Oviedo
Roberto Oviedo earned 125 total points
ID: 40342600
In each script you want to schedule I recommend adding these lines:

#Check if the tools are installed Exchange (Based on code by Martin Pugh, www.thesurlyadmin.com)
$ExchangeServer = 'RemoteServerNameFQDN'
If (Get-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -Registered -ErrorAction SilentlyContinue)
{   
    $Snapin = "Microsoft.Exchange.Management.PowerShell.E2010"
}
ElseIf (Get-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin -Registered -ErrorAction SilentlyContinue)
{   
    $Snapin = "Microsoft.Exchange.Management.PowerShell.Admin"
}
Else
{   
    Write-Verbose "Unable to locate Exchange tools on localhost, attempting to remote to Exchange Server 2010/2013..."
    Try {
        $RemoteCredential = $host.ui.PromptForCredential("Credentials Exchange Server", "Please enter your user name and password for Exchange Server.", "", "NetBiosUserName")
        $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$ExchangeServer/PowerShell/ -Authentication Kerberos -Credential $RemoteCredential -ErrorAction Stop
        Import-PSSession $Session -ErrorAction Stop 
    }
    Catch { 
        Write-Host "Unable to import Exchange tools from $ExchangeServer" -ForegroundColor Red
        Write-Host "Error:  $($Error[0])" -ForegroundColor Magenta
        Exit
    }
}

#Connect to Exchange Management Shell
If ($Snapin)
{   #We know it exists, but is it already loaded?
    If (-not (Get-PSSnapin $Snapin -ErrorAction SilentlyContinue))
    {   Try {
            Add-PSSnapin $Snapin -ErrorAction Stop
        }
        Catch {
            Write-Host "Unable to load $Snapin because $($Error[0])" -ForegroundColor Red
            Exit
        }
    }
}

Open in new window

0
 
LVL 12

Assisted Solution

by:Vaseem Mohammed
Vaseem Mohammed earned 375 total points
ID: 40342625
1st you need to create a profile to store scripts which must load by default.
Run
if (!(test-path $profile )) 
{new-item -type file -path $profile -force}

Open in new window

This will create a profile, you can check by entering $profile which gives you the location of profile.ps1
To edit use
PSEdit $Profile

Open in new window

This will open the Microsoft.PowershellISE_profile.ps1
Put
Add-PSSnapin Microsoft.Exchange.Management.Powershell.E2010

Open in new window

and save the file.
When you launch ISE the Snap-In will be loaded upon startup.
0
 

Author Comment

by:georgedschneider
ID: 40342818
Is this for any user or just the currently logged on user?
0
 
LVL 12

Expert Comment

by:Vaseem Mohammed
ID: 40342853
For currently logged on user
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 12

Expert Comment

by:Vaseem Mohammed
ID: 40342855
0
 

Author Comment

by:georgedschneider
ID: 40345791
Is this what would be needed in order for me to create scripts to run against exchange and schedule them on my scripting machine?
0
 
LVL 12

Assisted Solution

by:Vaseem Mohammed
Vaseem Mohammed earned 375 total points
ID: 40347007
If you are scheduling / distributing scripts then better option is to use If-Else to check and load snapin.
$2010snapin = Get-PSSnapin -Name Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction SilentlyContinue
if($2010snapin)
{
    Write-Host "Snap-In already loaded"
    
}
else
{
    Write-Host "Not Present"
    Write-Host "Adding Exchange 2010 Snap-In"
    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
}

Open in new window

0
 

Author Comment

by:georgedschneider
ID: 40361287
So basically all i need to do on my scripting machine that has Power shell, Exchange tools include this as part of the exchange scripts that I intend to run as a scheduled task.
0
 
LVL 12

Expert Comment

by:Vaseem Mohammed
ID: 40367677
Exchange tools will make the snap-in available on machine, If you open "Exchange Management Shell" the Snap-in will be loaded automatically, whereas if you open "Windows Powershell" the Snap-in will not be loaded in the session and hence you need to run the above mentioned code, so that when the script runs very 1st thing it does is to check the availability of Snap-in otherwise the script will fail.
0
 

Author Comment

by:georgedschneider
ID: 40395728
What is the difference between the various Powershell ISE profile types.  What are the hosts profile types used for?

Current user, PowerShell ISE
All users, PowerShell ISE
Current user, All hosts
All users, All hosts
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

This article will help you understand what HashTables are and how to use them in PowerShell.
Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
To show how to create a transport rule in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Mail Flow >> Rules tab.:  To cr…
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager

744 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

16 Experts available now in Live!

Get 1:1 Help Now