Tuesday, February 15, 2011

Automatic adding SharePoint 2010 and your own cmdlets to PowerShell ISE

As SharePoint development team, we are starting to write and run more completed Powershell scripts for several major projects such as SharePoint ADFS conversion from window authentication. We are also developed a mass update cmdlets framework to update SharePoint sites. We will need to debug the scripts frequently and identify some intermediate result. PowerShell ISE is the best tool that allow us to set breaking point for debugging. However, there are two problems that is always confused by the team members here.

  1. SharePoint or your customized snap-in do not load automatically when you start PowerShell ISE. You have to add those manually every time when you have new instance.
  2. There are several places you could add the PowerShell profile to add SharePoint 2010 and your own cmdlets automatically. However the scope and location of those scripts are different and difficult to remember when there is an issue.
This blog is to summarize the ways to automatic adding SharePoint 2010 and your own cmdlets to PowerShell ISE for my team.

Before you setup this on SharePoint side, you should understand the scope of the profile for Powershell. Here is the location you could setup the profile that will automatic adding SharePoint 2010 and your own cmdlets to PowerShell ISE.  The profiles ate listed in precedence order and the first profile has the highest precedence.
Description                Path
   -----------                ----
   Current User, Current Host $Home\[My ]Documents\WindowsPowerShell\Profile.ps1
   Current User, All Hosts    $Home\[My ]Documents\Profile.ps1
   All Users, Current Host    $PsHome\Microsoft.PowerShell_profile.ps1
   All Users, All Hosts       $PsHome\Profile.ps1

 Here are the profile locations for my SharePoint server.
  • C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
  • C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\profile.ps1
At this time, you understand the different scope of the profile that could be used to automatic add SharePoint 2010 and your own cmdlets to PowerShell ISE. You should use the following command to setup the profile. I would use one example to setup for all users and all host that listed in one blog with some modification.

Open Powershell ISE from Programs->Accessories->PowerShell folder
a. Run the following code from the immediate window in ISE to create a new profile for all users on all hosts. You could change it easily based on the different syntax to create the profile. You should change the other variables in the other commands accordingly.

if (!(test-path $profile.AllUsersAllHosts))
{new-item -type file -path $profile.AllUsersAllHosts-force}

b. Run the following code to edit the new profile

psEdit $profile.AllUsersAllHosts

C. When profile1.ps1 opens, add the following code to attach the SharePoint snap-in every time PowerShell is run.

 If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{ Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell }
{ Add-PSSnapIn QualcommCMDLETs }  /* this is customized cmdlets */

d.  Save profile.ps1 and close PowerShell ISE.
e. You should see the following file craeted C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
f. Start PowerShell ISE again and type the following command to verify that the SharePoint snap-in loaded. You should see SharePoint-specific command listed.

Get-Command Get-SP*

You may noticed one warning if you launch the SharePoint 2010 Management Shell from Programs->Microsoft SharePoint 2010 Products->SharePoint 2010 Management Shell indicates some snap-in already added. This is because SharePoint 2010 Management Shell already added the SharePoint snap-in already. You could ignore this warning message and enjoy the PowerShell ISE with all the SharePoint commends and your own cmdlits from now.

No comments:

Post a Comment