Programming4us
         
 
 
Sharepoint

Working with the SharePoint 2010 Management Shell (part 1)

12/13/2010 2:40:45 PM
To open the SharePoint 2010 Management Shell, click Start, and then select Programs, Microsoft SharePoint 2010 Products, and finally, SharePoint 2010 Management Shell, as shown in Figure 1.
Figure 1. Accessing the SharePoint 2010 Management Shell


The SharePoint 2010 Management Shell is a custom console; it is not the same console that opens if you open the default Windows PowerShell console, a shortcut to which is usually placed on the taskbar. A review of the properties of the SharePoint 2010 Management Shell shortcut exposes the command that is executed.

C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe  -NoExit " &
' C:\Program Files\Common Files\Microsoft Shared\Web Server
Extensions\14\CONFIG\POWERSHELL\Registration\\sharepoint.ps1 ' "

The shortcut points to the file Sharepoint.ps1. This is an example of a Windows PowerShell profile, which is identical to any other Windows PowerShell script file and is used to store frequently used elements that need to be automatically loaded at the start of a Windows PowerShell session. Hence, just like normal scripting files, profiles can contain functions, aliases, and variables, and they can load any Windows PowerShell extensions, snap-ins, or modules you might need. A Windows PowerShell snap-in (PSSnapin) is a .NET program that is compiled into DLL files and contains new cmdlets, functions, and/or providers. Windows PowerShell comes with a number of snap-ins, including Microsoft.PowerShell.Core, Microsoft.PowerShell.Host, and Microsoft.PowerShell.WSMan.Management, as well as modules such as ActiveDirectory, FailoverClusters, and WebAdministration. As other products are installed on your computer, additional extensions will become available.

The main aim of the SharePoint profile file is to load the Windows PowerShell for SharePoint snap-in so you can then use the new cmdlets specific to SharePoint. The SharePoint profile file contains the following code, plus a signature.

$ver = $host | select version
if ($ver.Version.Major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"}
Add-PsSnapin Microsoft.SharePoint.PowerShell
Set-location $home

This code obtains the version of Windows PowerShell, checks that it is greater than 1, and then if that is true, it sets the threading model so that the first thread will be reused, loads the SharePoint PowerShell snap-in, and then changes directory to the home folder, such as C:\users\<userid>, where userid is the person who is currently logged in. 

The SharePoint PowerShell snap-in is not the only snap-in loaded into the SharePoint 2010 Management Shell. Therefore, not only can you use the SharePoint cmdlets, you also can use other cmdlets from the snap-ins that are loaded. To see all the snap-ins and the order in which they were loaded, type Get-PSSnapin. Your output should look similar to the following example.

Name        : Microsoft.PowerShell.Diagnostics
PSVersion : 2.0
Description : This Windows PowerShell snap-in contains Windows Eventing and
Performance Counter cmdlets.
Name : Microsoft.WSMan.Management
PSVersion : 2.0
Description : This Windows PowerShell snap-in contains cmdlets (such as
Get-WSManInstance and Set-WSManInstance) that are used by the
Windows PowerShell host to manage WSMan operations.
Name : Microsoft.PowerShell.Core
PSVersion : 2.0
Description : This Windows PowerShell snap-in contains cmdlets used to manage
components of WindowsPowerShell.
Name : Microsoft.PowerShell.Utility
PSVersion : 2.0
Description : This Windows PowerShell snap-in contains utility Cmdlets used to
manipulate data.
Name : Microsoft.PowerShell.Host
PSVersion : 2.0
Description : This Windows PowerShell snap-in contains cmdlets (such as Start-
Transcript and Stop-Transcript) that are provided for use with the
Windows PowerShell console host.
Name : Microsoft.PowerShell.Management
PSVersion : 2.0
Description : This Windows PowerShell snap-in contains management cmdlets used to
manage Windows components.
Name : Microsoft.PowerShell.Security
PSVersion : 2.0
Description : This Windows PowerShell snap-in contains cmdlets to manage Windows
PowerShell security.
Name : Microsoft.SharePoint.PowerShell
PSVersion : 1.0
Description : Register all administration Cmdlets for Microsoft SharePoint Server.



Note:

If you develop your own scripts that can be called from the command prompt or by double-clicking the file in Windows Explorer, and those scripts contain Windows PowerShell for SharePoint commands, make sure that you include the command to set the threading model and load the Windows PowerShell for SharePoint snap-in in your scripts. When calling a script from within a Windows PowerShell console, you can use the #requires tag at the top of your script.


Real World: Customizing Your Console

It is common practice to modify the profile file on the designated administrator machine. The profile could be altered so that it

  • Contains all the snap-ins or modules used by all IT professionals in an organization or department.

  • Tracks all Windows PowerShell commands executed.

  • Changes the prompt to indicate the server name. In an organization with a multitude of servers, it is all too easy to start an administrative task on the wrong server, so anything that can help an administrator identify the server he is working on will be helpful, such as configuring the window title to list the name of the server.

An example of a custom profile could contain the following code.

<# ***********************************************************
Copyright (c)2010, Contoso, All Rights Reserved
*************************************************************
Contoso PowerShell Profile - ContosoProfile.ps1
Author: Peter Connelly
Purpose: This profile sets the PowerShell window size, font and title
and loads the SharePoint snap-in
History: Version 1.0 01/02/2010 First version
#>
# Track all Windows PowerShell commands
$profilename = $MyInvocation.MyCommand.Name
$profilepath = $MyInvocation.MyCommand.Path
$transcriptFile = "C:\Contoso\Logs\Powershell_$profilename.log"
Start-Transcript $transcriptFile -append -force
Write-Output "Starting profile: $profilepath"
# Command from SharePoint Server 2010 profile file - SharePoint.ps1
$ver = $host | select version
if ($ver.Version.Major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"}
Add-PsSnapin Microsoft.SharePoint.PowerShell
Set-location $home
# Check that this is a command-line interface and not the ISE
if ($host.name -eq "ConsoleHost")
{
$width = 80
$sizeWindow = new-object System.Management.Automation.Host.Size $width,40
$sizeBuffer = new-object System.Management.Automation.Host.Size $width,9999
<# Check to adhere to the following rules:
The buffer width can't be resized to be narrower than the window's current
width plus the window's width can't be resized to be wider than the
buffer's current width. #>
$S = $Host.UI.RawUI
if ($s.WindowSize.width -gt $width)
{
$s.WindowSize = $sizeWindow
$s.BufferSize = $sizeBuffer
} else {
$s.BufferSize = $sizeBuffer
$s.WindowSize = $sizeWindow
}
}
# Set foreground, background color and window title
$s.ForegroundColor = "Yellow";$s.BackgroundColor = "DarkBlue";
$s.WindowTitle = "$env:computername"
# #############################################################
# End of ContosoProfile.ps1
# #############################################################

Because the profile script is automatically executed every time you run the SharePoint 2010 Management Shell, it should be signed. More information about customizing your console can be found at http://technet.microsoft.com/en-us/library/ee156814.aspx.
Other -----------------
- SharePoint 2010 : Edit the Contents of a Page
- SharePoint 2010 : Change the Page Layout of a Publishing Page
- SharePoint 2010 : Authoring Pages - Edit the Properties of a Page
- SharePoint 2010 : Authoring Pages - Create a New Page (part 2)
- SharePoint 2010 : Authoring Pages - Create a New Page (part 1)
- SharePoint 2010 : Managing Systems Remotely with WinRM
- SharePoint 2010 : Installing Windows PowerShell
- SharePoint 2010 : Using Windows PowerShell: The Basics
- SharePoint 2010 : Modify a View
- SharePoint 2010 : Create Mobile Views
- Uninstalling SharePoint 2010
- Configuring a SharePoint 2010 Installation (part 1) - Renaming the Central Administration Database
- Configuring a SharePoint 2010 Installation (part 1) - Running the Farm Configuration Wizard
- SharePoint 2010 : Enable or Disable Inline Editing in a View
- Performing SharePoint 2010 Installations (part 5)
- Performing SharePoint 2010 Installations (part 4)
- Performing SharePoint 2010 Installations (part 3)
- Performing SharePoint 2010 Installations (part 2)
- Performing SharePoint 2010 Installations (part 1) - SharePoint 2010 Standalone Installation
- SharePoint 2010 : Specify the Item Limit for a View
 
 
Most View
- SQL Server 2008 Scheduling and Notification : Scripting Jobs and Alerts, Multiserver Job Management & Event Forwarding
- The Art of SEO : Content Optimization (part 2)
- Developing Applications for Windows Phone 7 : Data Binding (part 1)
- Developing for Windows Phone and Xbox Live : Sprites and 2D Graphics - Spritebatch (part 2)
- SharePoint 2007 : Use Alerts - Manage My Alerts in a Site
- Windows 7 : Creating a Windows Network - Installing Network Wiring
- Microsoft Dynamics AX 2009 : The Application Integration Framework (part 5) - Working with Document Services - Publishing Dynamics AX Services, Configuring Dynamics AX Services
- Exchange Server 2007: Monitor Your Exchange Environment (part 1)
- CSS for Mobile Browsers : Common Patterns (part 1) - Absolute and floating positions
- Destination: SQL Server 2008 or SQL Server 2008 R2 (part 1) - Side-by-Side Migration
Top 10
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 3) - Configuring Recipient Filtering
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 2)
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 1)
- Implementing Edge Services for an Exchange Server 2007 Environment : Installing and Configuring the Edge Transport Server Components
- What's New in SharePoint 2013 (part 7) - BCS
- What's New in SharePoint 2013 (part 6) - SEARCH
- What's New in SharePoint 2013 (part 6) - WEB CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 5) - ENTERPRISE CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 4) - WORKFLOWS
- What's New in SharePoint 2013 (part 3) - REMOTE EVENTS