Monday, June 09, 2008 8:00:05 PM UTC #

When trying to convince others why I think PowerShell is hot hot hot, I find myself lacking examples. So, dear reader, I'm writing this list as a sort of "memo to self: don't choke next time and remember one or two of these things." Here goes.

Before the mega-list

If anyone is interested in a particular topic in more detail, let me know. To me, most of what I list below is either a) too broad a topic to cover properly, b) too narrow for anyone else to find it of use, and/or c) just plain obvious.

I can't tell what may be interesting to others; it's all a "solved problem" at this point to me.

Of all these things, since the following is a jumbled, unprioritized, stream-of-consciousness type of list, I'd like to point out that the things I find are most useful are:

a) Object model spelunking - during development, figuring out which function to call, testing out little 5-line scripts to see what happens—little things to help me gain confidence that what I'm writing will work. This is also the most difficult thing to express properly.

b) Visual Studio post-build task - it's just so easy to add and remove bits of script to help me do … whatever it is I need doing. Note this is not necessarily a replacement for NAnt or MSBuild—instead it's something of an informal post-build scratchpad.

c) Deep (focused) administration tasks - like getting the crawl logs programmatically, or getting a list of sites or features into an Excel-friendly format for further analysis. SharePoint allows deep access to its inner workings with APIs that it uses itself—this is unique among enterprisey-type systems.

Memo to self: Powershell + SharePoint

Ways I've used PowerShell + SharePoint:

  • Object model spelunking. This includes
    • The most commonly used SharePoint objects, e.g. SPSite, SPWeb, SPList, SPListItem. PowerShell's unique (to the .NET space) REPL environment is the best for exploring an object model. You get the full power of an object browser with all the benefits of an immediate window. I don't know how to explain this; you'll just have to try it sometime.
      • Neat trick to help you write CAML queries. 1) Make a view that does exactly what you want. 2) Use PowerShell to dig into the view's SchemaXml property. Others have written Windows Forms tools to do the types of things I do in PowerShell.
    • Digging into the SPLimitedWebPartManager class.
  • Object model manipulations. This usually means really simple things that I do in a dev/test environment, like
    • Changing the logo on a site to a different gif
    • Changing the default master page property
    • Changing lists to Hidden
    • Changing fields to Hidden - there was a case where (for whatever reason) the ModerationStatus field became visible; it shouldn't have been; quick PowerShell script confirmed the fix.
    • Deleting all items in a list. $site.OpenWeb("/awesomeness").Lists["awesomeness"].Items | % { $_.Delete() }
      • Who cares? you may say. What happens if you have 1000 items in the list, that you're programmatically creating every time you run your integration test? Yeah. Yeah, you may need something like this.
    • Comparing two fields in a list via PowerShell, looking for corruption in one of them. Amusingly, the problem was that one field belonged to a Content Type, thus was not visible on the list edit page, thus causing us MUCH confusion. Oops! Well, PowerShell was able to help us, just not in the way we thought. By the way, I used PowerShell's metaprogramming facilities to do a 50-property comparison using a one-liner.
  • Enterprise Search administration. Includes:
    • Getting a copy of the crawl log. Up until a few weeks ago, until the Enterprise Search team released a stsadm tool to do the same, this was quite novel.
    • Swapping the "file inclusion list" into a "file exclusion list" in order to aid web crawling.
  • Working with wikis - digging into a wiki library and extracting the page titles - it was actually somewhat lame, but hey, it worked.
  • Ad-hoc querying of our farm's various sites; who's the owner, when were they last modified, etc.
  • Listing the farm's current various peoplepicker settings, to help ease my confusion with their application. This was pre-SP1 before we had the really useful settings available to us.
  • Comparing document templates (actual documents) in every library in the farm, comparing pre-migration to post-migration farm. It turned out, during the migration we "lost" two document templates. I'd like to give a big shout-out to SharePoint's WebDAV support, that was definitely a big help :)
  • Copying a list using SharePoint's lists.asmx web service. Presumably I was doing some processing on the data, there's not really an excuse for just copying the list from one site to another.
  • A different solution: used PowerShell as a "touch-free" solution to generating "reports" on a SharePoint server. I'm not particularly proud of technology choice in this case, but the results were good. This script involved a) pulling data from TFS, b) updating a custom list, c) creating InfoPath form data and "uploading" the forms. It wasn't the best choice.
  • Listing all activated Features in the farm (well, actually, close: all sites in a site collection, the site collection itself, the web application, and the farm-level Features). Anyway, listing all activated Features in an easy-to-paste-into-Excel format.
  • PowerShell as a Visual Studio post-build action
    • Use it to deploy my project
    • Use it to do a "fast deploy" - before building, just comment out the sections of the post-build script that we don't need to run.
      • gacutil /i for quick code updates to the GAC.
      • gacutil /u to ensure that when I run the TestDriven.NET test runner, it is running off of the most-recently-built project build, not off of whatever is sitting in the GAC. With this, I can guarantee nothing is in the GAC.
      • Attaching to event receivers.
    • Do an app pool reset.
    • Warm up the app pool by programmatically visiting a page in the site.
    • WANTED BADLY, MANY TIMES: a way to programmatically update a workflow association InfoPath form, WITHOUT having to go through the whole deployment rigmarole. If it's possible at all, I can automate it.
  • PowerShell as a "manual setup" for integration tests. We're talking the "copy 100 files to the document library and see what happens" type of integration test, which is … let's just say difficult to achieve without some kind of artifacts. So, in other words, because I was too lazy to set up my integration tests the proper way, I used PowerShell.
  • Doing name lookups from the people.asmx web service; checked ~400 names against the people list (and got back a rich list including email address, full name, job title, department, etc) in 5 minutes.
  • $solutions = ([xml](stsadm -o enumsolutions)).Solutions
    • You know all that worthless unreadable XML returned by stsadm -o enumsolutions? Well, with this one-liner, we have turned the useless into something powerful.
  • Installing a Fab 40 demo site collection programmatically - it takes a sweet long time to finish, but the script is painless and perfect. Others have posted batch files to install the templates, but no one's attempted to build all the demo sites automatically. This is a good example of what PowerShell can do.
  • Enumerating over customized/unghosted pages—all of them in the entire farm—and getting a list of all pages that are listed as uncustomized. This is more useful than what the PRESCAN.EXE log gives you.
    • On a related note, attempted to run the "RevertAll()" or similarly named function to revert all pages in the farm to default. Again this was on a copy of production data. And no, it didn't work as well as I'd hoped.
  • Use CabLib.dll to create CAB files, bypassing the horrors of MAKECAB.EXE.
  • Tiny development-time administrivia, like getting the four-part assembly name of your project's DLL, and copying it to the clipboard—you need it to declare in your feature.xml file for your Feature event receiver. Here's a bunch of things I did.

Conclusions

  • You don't have to use PowerShell. I'm sure many of you have solved similar problems with PowerShell alternatives, including
    • SQL queries
    • WinForms utility apps (check out CodePlex, you'll see what I mean)
    • Console apps
    • custom stsadm extensions (I'm talking to you, Gary LaPointe)
    • NAnt tasks/MSBuild tasks
    • Telling people "that's not possible"
  • It's hard to nail down for what I use PowerShell specifically, as you may infer from above. Is "lots of stuff" an okay answer? No?
Monday, June 09, 2008 8:00:05 PM UTC  #     |  Comments [4]  |  Trackback
Tuesday, June 10, 2008 4:44:24 AM UTC
do you recommend a powershell ide that provides intellisense or would you say its better to do without?
is the tab completion which is provided more like bash where it will complete as much as it can...
or more like cmd where it will cycle thru all that matches so far..
or maybe (hopefully) a mix between the two?
dennis
Tuesday, June 10, 2008 12:32:25 PM UTC
1. I like IntelliSense but all the PowerShell implementations are SLOW. So I turn it off.
2. Tab complete/autocomplete is okay (good for files), and can be improved. I would like it to be a little better, but it's not killer. Remember you have the option to pipe an object into | Get-Member, which will eliminate most of your "need" for Intellisense.

The best IDE at this point is the one from Microsoft--download PowerShell V2 CTP and it comes with an IDE. If you're writing really small scripts, you can just find a notepad replacement with syntax highlighting (will have to search for PowerShell syntax files, but they exist).
Peter Seale
Monday, November 02, 2009 2:39:09 AM UTC
Hi All,

I am a newbie with MOSS but a old hat with most everything else. I am (desperately) seeking a way to programmatically import many documents from a server share into MOSS; Preferrable, directories with sub-folders.

I have read that this is not possible but we are working with a database here so I thought there might be some glimmer of hope in the prospect.

Any help or a point in the right direction would be a huge lift!
Thanks!!
SteveM
Monday, November 30, 2009 7:22:43 PM UTC
You mentioned...
Ways I've used PowerShell + SharePoint:
Installing a Fab 40 demo site collection programmatically - it takes a sweet long time to finish, but the script is painless and perfect. Others have posted batch files to install the templates, but no one's attempted to build all the demo sites automatically. This is a good example of what PowerShell can do.

Would you be willing to share this script?
I'd love to create this demo site with ease.
Thanks!!!
SteveP
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, strike, strong, sub, sup, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview
Syndication

Search
Posts on this page
Categories
Sites I visit regularly
About

Powered by: newtelligence dasBlog 2.2.8279.16125

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010, Peter Seale

Send mail to the author(s) E-mail



Sign In