Sharing the experience search

Search sharing-the-experience.blogspot.com

Friday, April 1, 2011

PowerShell and SharePoint: What, Why and How

!UPDATE: A really easy tool for Powershelling for SP2013 , extremely helpful for totally newbies Windows PowerShell for SharePoint Command Builderhtml


I know it's frustrating to start something new....That's why I made this post - to show you that's no so hard when you trust someone and when you do one baby step at the time.

  I will explain:

1. [What is PowerShell]

2. [Why it is cool]

3. [How to start]

4. [How to use it]

   Ready?

1. [What is PowerShell]


PowerShell is not, first and foremost, a scripting language. It’s a shell, not unlike Cmd.exe.
It’s written in .NET instead of C++ or something, but in the end it’s a text-based commandline
window. You type commands, hit Enter, and they run—and you see results.
  It looks like this:

2. [Why it is cool]

The beauty of it - you can load whatever dll you like - and then manipulate with  a object of the dll.
It's quite handy for working with Sharepoint. No need to write and compile the code anymore to do some tedious tasks, you can do it on the fly! (but make sure that you have backed the SharePoint up first)
   An easy example how to make use out from PowerShell in the SharePoint:

  [System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")

   $siteUrl=Read-Host "Please enter url for site to open"

   $site=[Microsoft.SharePoint.SPSite]($siteUrl)

   Write-Host "======The site " $siteUrl " is opening======"

   $web=$site.OpenWeb() 


3. [How to start]

PowerShell v2 comes preinstalled on Windows 7 and Windows Server 2008 R2.

How to install Powershell ISE on Server 2008 R2   
 

Windows PowerShell ISE is an optional feature and requires .Net Framework 3.51 to be installed.

To install Windows PowerShell ISE on Windows Server 2008 R2, use the Add Features Wizard
or run the PowerShell Window

and type the command:
Import-Module ServerManager; Add-WindowsFeature PowerShell-ISE

After  that you will be able to use more reached development environment to work with PowerShell:

4. [How to use it]

To start PowerShell ISE - go to {System Drive}\Windows\System32\WindowsPowerShell\v1.0
and run powershell_ise.exe
OR
run PowerShell through Accessories ->PowerShell and run the command: powershell_ise.exe 
It looks like this:
 
  I like it more than the default PowerShell window - much easier to write functions and follow the result.
 
Since I am focusing on SharePoint, I want to show how to ease your way up to start manipulate with SharePoint API 
 
To create preloaded dlls for your profile type in the PowerShell command window: 
  new-item -path $profile -itemtype file -force  
  notepad.exe $profile. 
 
 Paste this code into the profile and save it:
 
[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")

$siteUrl=Read-Host "Please enter url for site to open"

$site=[Microsoft.SharePoint.SPSite]($siteUrl)

Write-Host "======The site " $siteUrl " is opening======"

$web=$site.OpenWeb()

Write-Host "=====web and site are ready======" 
 

 
Re-start the PowerShell ISE
 


 
That's it! You are ready to go!
 
Wait...Here are several tips:
- Got an error "cannot be loaded because the execution of scripts is disabled  on this system"?
Run Set-ExecutionPolicy "RemoteSigned" (http://technet.microsoft.com/en-us/library/dd347628.aspx)
- an example how to use in SharePoint
- when you manipulate with object you can get tips what properties and method it has: 
write your object , set the dot and then Press "Tab"
  - there are tremendous amount of the PowerShell scripts have been already written in the SharePoint field.
Most used for me is This script retrieves a binary solution file (a cabinet file, normally with .wsp or .cab extension) from the SharePoint configuration database. 

Happy life with PowerShell!

 
 


 
  






No comments:

Post a Comment