Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 413
  • Last Modified:

Exchange modules in Power Shell ISE

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
georgedschneider
Asked:
georgedschneider
  • 6
  • 5
4 Solutions
 
Vaseem MohammedCommented:
Run this 1liner in ISE with administrator rights.
Add-pssnapin microsoft.exchange.management.powershell.e2010
0
 
georgedschneiderAuthor Commented:
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
 
Roberto OviedoCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Vaseem MohammedCommented:
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
 
georgedschneiderAuthor Commented:
Is this for any user or just the currently logged on user?
0
 
Vaseem MohammedCommented:
For currently logged on user
0
 
Vaseem MohammedCommented:
0
 
georgedschneiderAuthor Commented:
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
 
Vaseem MohammedCommented:
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
 
georgedschneiderAuthor Commented:
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
 
Vaseem MohammedCommented:
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
 
georgedschneiderAuthor Commented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now