I’ve become a fan of using the PowerShell Integrated Scripting Environment (PowerShell ISE) for creating, testing and debugging scripts, using breakpoints and step-by-step execution; features found in many development environments. Depending on the script I’m working on and for what customer or environment, I may need to add snap-ins or switch contexts, like connecting to Exchange Online.
One of the powerful features of ISE is that it allows customizing through the ISE object model. For example, you can explore ISE through the $psise object:
To add custom menu options to ISE, we’re going to add items to the submenu of $psISE.CurrentPowerShellTab.AddOnsMenu, which is “Add-ons”. An item needs to consist of:
- Display Name, which is used for displaying the menu item;
- Action, which can be a PowerShell cmdlet, scriptblock or function;
- Optionally, you can assign a keyboard shortcut to the menu option.
To automatically load the custom entries after starting up ISE, we’re going to define the entries in our default ISE profile file, Microsoft.PowerShellISE_profile.ps1, which location is stored in $profile. The file doesn’t exist by default, so when required you can simply create the file in the proper location using notepad $profile.
In our example, we’ll add three entries:
- Implicit Remoting to connect to Exchange using a static FQDN;
- Loading the Exchange 2010 Snap-in and connecting to Exchange using Autodiscover (unsupported, will bypass RBAC);
- Connecting to Exchange Online.
Note that the example won’t be using stored credentials and will let ISE prompt the user for credentials when required, which is perfectly fine if you need to access different Office 365 tenants for example.
Now, in the Microsoft.PowerShellISE_profile.ps1 file, add the following contents:
$psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Add( "Connect to Exchange @ Contoso", { $ExSession= New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exserver.contoso.com/PowerShell/ -Authentication Kerberos Import-PSSession $ExSession }, "Control+Alt+1" ) $psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Add( "Connect to Exchange On-Premise", { Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 . $env:ExchangeInstallPath\bin\RemoteExchange.ps1 Connect-ExchangeServer –auto }, "Control+Alt+2" ) $psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Add( "Connect to Exchange Online", { $o365Cred= Get-Credential $o365Session= New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $o365Cred -Authentication Basic -AllowRedirection Import-PSSession $o365Session }, "Control+Alt+3" )
After starting ISE you’ll see the Add-ons menu now contains three extra items:
When selecting “Connect to Exchange Online” (or pressing the configured keyboard shortcut), ISE will execute the associated code block; the progress is displayed in the output window. You will be prompted for credentials after which ISE will connect to Exchange Online and import the remote session.
After the session has been imported, you have the additional commands at your disposal in ISE and you can work on your scripts since they’ll be running in the context of the environment you’ve connected to.
Of course, this is just an example of what you can customize in ISE (pink background anyone?). For more information on customizing PowerShell ISE check here. If you’re new to PowerShell ISE, check here.
Pingback: Adding Exchange Shell items to PowerShell ISE | EighTwOne (821) « JC’s Blog-O-Gibberish
Really great post. I’ve been looking for this solution for a while. You just aleviated months of headache. Thanks.
LikeLike
OK, that works for 2010, but how to do this for 2013 cmdlets?
LikeLike
Same – module is named identical or remote into local Exchange sessions using the local FQDN and Kerberos authentication
LikeLike
Thank you very much for your very fast response! I’m new in Exchnage and PS and need some time to understand. Formerly Lotus Domino…
LikeLike
Pingback: Connecting to Office 365/Exchange | EighTwOne (821)
Pingback: Powershell ISE Exchange Add-On
Pingback: Remove-PSSession – 运维实战侠
Pingback: Remove-PSSession – 实战宝典
Pingback: Remove-PSSession - 运维实战侠