Aarebrot.net

Frodes website about Sharepoint and other stuff...

  • Increase font size
  • Default font size
  • Decrease font size

Loading Sharepoint scripts in PowerShell

E-mail Print
User Rating: / 0
PoorBest 

If you've downloaded some of my previous scripts and tried to run them from your powershell prompt, you may have gotten some "Can not find type" errors if you didn't load the Sharepoint libraries first. If you're fresh with Powershell and Sharepoint, it may not be inherently obvious how to do this so I decided to write a small guide on how I have my Powershell setup on my virtual machine.

First you need to install Powershell (obviously). I suggest you still stick with 1.0 for now, since 2.0 is still not finished. You can download Powershell from here: https://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx.

The next step is to create a shortcut to Powershell that loads a script at startup. I have mine on the desktop, but you can put it where ever you want (Quick Launch, Programs, etc). The target of my shortcut is set to: %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -noexit &"H:\Powershell\_loadSP.ps1". Obviously you're going to have to change this to make sure it points to the correct place. The first part (before the &) points to your Poweshell installation, so you can probably copy and paste this part into your shortcut. Make sure you include the -noexit command line parameter.

The second part must point to where you have your startup script located. Mine is located on my personal network drive (H:) in a folder called Powershell. The script itself is called _loadSP.ps1. You can change this to reflect where you have your script located.

 
The shortcut target and start in properties

For convencience, I also have the "Start in:" property set to the Powershell folder on my personal network drive. I store all my scripts in this folder, including the startup script, so it's nice to have Powershell default to this directory so I can just start typing in scripts to run them.

When you create your own shortcuts to Powershell, it doesn't set the default Powershell colors for you. So if you want the blueish background you'll have to go in and set these yourself. The colors for the background is Red =1, Green = 36, Blue = 86. For the text it is Red = 238, Green = 237, Blue = 240. Pop-up text is Red = 0, Green = 128, Blue = 128. And finally pop-up background is all white (all values 255).

 
The screen colors

Next we have to make that Powershell start-up script. To run any of my scripts there are just two libraries you need to load.

  1. [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
  2. [System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Publishing")

That should be all you need to get started.

I have a few extra things added in my startup script however, which lists all the scripts in the current directory, as well as their syntax and a short description. This is pretty shoddy code as I wrote it in about 5 minutes, but it works and that's all I need.

  1. # Description
  2. # Loads Sharepoint libraries and lists scripts of current folder, as well as their description and syntax
  3. #
  4. # Syntax
  5. # ./_loadSP
  6. #
  7. # Parameters
  8. # none
  9. #
  10. # Settings
  11. # Only change the -value parameter!
  12. #
  13. set-variable -option constant -name filelen -value 16
  14. set-variable -option constant -name syntlen -value 35
  15. set-variable -option constant -name desclen -value 66
  16. # End of settings
  17.  
  18. "Loading Sharepoint libraries..."
  19. [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
  20. [System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Publishing")
  21. sleep -Milliseconds 300 # pause for dramatic effect
  22. cls
  23. "Sharepoint libraries loaded!"
  24. ""
  25. "Available scripts:"
  26. ""
  27. #Table formatting
  28. $n = "Name"
  29. $s = "Syntax"
  30. $d = "Description"
  31. $fspcr = " "
  32. $sspcr = " "
  33. for($c = 0; $c -lt $filelen - $n.length; $c++) { $fspcr += " " }
  34. for($c = 0; $c -lt $syntlen - $s.length; $c++) { $sspcr += " " }
  35. for($c = 0; $c -lt $n.length; $c++) { $ndsh += "-" }
  36. for($c = 0; $c -lt $s.length; $c++) { $sdsh += "-" }
  37. for($c = 0; $c -lt $d.length; $c++) { $ddsh += "-" }
  38. $n + $fspcr + $s + $sspcr + $d
  39. $ndsh + $fspcr + $sdsh + $sspcr + $ddsh
  40.  
  41. # Find all .ps1 scripts
  42. $files = Get-ChildItem | where { $_ -like "*.ps1" }
  43.  
  44. # lets list contents of folder...
  45. foreach($file in $files)
  46. {
  47.  $filecontent = Get-Content $file.Name
  48.  
  49.  # Retrieve syntax
  50.  $syntax = $filecontent[4].substring(2)
  51.  if($syntax.length -gt $syntlen)
  52.  {
  53.   $syntax = $syntax.substring(0,$syntlen-3) + "..."
  54.  }
  55.  
  56.  # Retrieve description
  57.  $desc = $filecontent[1].substring(2)
  58.  if($desc.length -gt $desclen)
  59.  {
  60.   $desc = $desc.substring(0,$desclen-3) + "..."
  61.  }
  62.  
  63.  # Format column one
  64.  $fspcr = " "
  65.  for($counter = 0; $counter -lt $filelen - $file.Name.length; $counter++)
  66.  {
  67.   $fspcr += " "
  68.  }
  69.  
  70.  # Format column two
  71.  $sspcr = " "
  72.  for($counter = 0; $counter -lt $syntlen - $syntax.length; $counter++)
  73.  {
  74.   $sspcr += " "
  75.  }
  76.  
  77.  # Let's print the content
  78.  $file.Name + $fspcr + $syntax + $sspcr + $desc
  79. }
  80.  
  81. # Changelog
  82. #
  83. # v1.0 - November 27th, 2008
  84. #  First public release


Powershell start-up script loaded

Last Updated on Thursday, 27 November 2008 10:14  

Add your comment

Your name:
Subject:
Comment:
  The word for verification. Lowercase letters only with no spaces.
Word verification: