Archive for March 2010

 
 

More Help with SharePoint Management Shell

Our new best friend, Dan, has posted this that helps with anyone that trying to get to their SharePoint farm from the SharePoint Management Shell (SMS.)

We have to remember that when we’re running PowerShell, we’re given permissions according to the account that we logged into Windows with. This is a little different than in the SharePoint GUI where what we see depends on who we are but the actions we take are often executed using the application pool account. What that means is that when we’re running PowerShell and .ps1 scripts, we have to be sure we’ve got the necessary permissions to complete our tasks. Dan’s post covers most of that.

hth

-robot

Still Working Through the Codeplex Autointaller Script

We’re still trying to decode the SharePoint 2010 Autoinstaller Script from CodePlex.

We got the Throw thing figured out thanks to TechNet which offers this little demo:

function Get-XMLFiles
   {
   param ($path = $(throw "The Path parameter is required."))
   dir -path $path\* -include *.xml -recurse | sort lastwritetime
   |  ft lastwritetime, attributes, name  -auto
   }

Which is all good except trying to figure out what to do with it. I found this from our new best friend Dr. Tobias. Of course, we know saving this into a .ps1 script will make it run but, in the console, you have to learn to do multi-line entry and Dr. Tobias covers that straight away.

So looking that the function we’ll create, we see we enclose the code in {curley braces} and we have a param section. Here we drop our variable $path and we set it’s initial value to $(Throw “blah blah blah”) What this does is poke the Throw to the processor unless it’s replaced by an alternative at runtime.

Hence, get-XMLFiles (‘C:\’) will return all the .xml files on your C: drive.

You’ll also see that the -include option filters the return to .xml files, the -recurse option makes it dig down into folders and sub-folders, the  sort sorts the output and the ft formats the output into a nice table with the designated headers. Note also that the parameter is passed inside parenthesis and single quotes. I presume you’d separate multiple paramerters with a comma.

Essentially, in the Get-XMLFiles function, it’s give me a path or I’ll barf. I’ve been there.

In the AutoInstaller script, the script itself has a parameter block, so when the script is called, it better get passed a configuration file or it’s barf city. I’ve been there too.

The one thing I don’t know is how to list the function code after it’s built. I presume I can just rebuild it with new code but I don’t know how to get what I’ve got in there to make minor edits. So I turned to our new best friend Lee who talks about environment variables here. From what he says, I figured out that a get-content function:<function name> will display the code supporting a given function.

Progress. That’s good.

-robot

How's it Work?: Codeplex AutoInstaller Script

So since we’re expert at beginner PowerShell Scripts, maybe we should take a look at the AutoInstaller on CodePlex. Download it and you’ll find it’s got an AutoSPInstaller.ps1 script inside.

It takes about two seconds to see we got work to do. First, there’s this issue of the:

param
(
    [string]$InputFile = $(throw 'need parameter input file
    (e.g. "c:\SP2010\Scripted\SetInputs.xml")')
)

So I can’t quite crack this but when you look at get-help throw, you see it looks like they’re making the path a required parameter or bailing on the script.

The second issue is that of iterating through an XML file. Now I imagine we’re going to put all our configurations in an XML file and the script will read it and call the SharePoint Management Shell commands that use them to build your whole farm with one click. Groovy.

So how do we iterate through an XML file? Well, our new best friend, Dan, covers it pretty good here. He’s got the best line of the day:

With some practice you actually can drive nails with a screwdriver and then you only need to learn how to use one tool to build a house.

I gotta tell you guys and gals, I see alot of myself in that one-tool group. So maybe Dan will help. The problem is that his example is a grocery list and it’s almost supper time so, I’m heading out to:

  1. Buy Groceries
  2. Make Dinner
  3. Iterate…

Sound like an exciting evening for a robot.

-robot

Got PowerShell Figured Out Yet?

Here’s the answer from our new best friend Gary.

His post covers:

  • -loops
  • -variables
  • -arrays
  • -math
  • -string concatenation
  • -output

In this post, he shows us how adding:

> .\filename.txt

to a command writes the output to a file in the current directory and also how adding:

 | format-list 

to a command converts our output from a nice table with headers and everything to a list with space to be more verbose. For example, try these two:

get-process winlogon

and this:

get-process winlogon | format-list

This is the most I’ve learned in the shortest amount of time in a long time. Especially if you count the fact that I now know there’s only four perfect numbers less than 200,000.

-robot

Continuing with SharePoint Management Shell

First of all, to echo a point we made a few days ago, our new best friends at Acme Solutions talk about SMS here. They agree that it will be the tool of choice for the SharePoint 2010 administrator.

Then, since we’re still trying to get our feet under us in this environment, we found this from our new best friend, Gary. He confirms we we had said earlier that SMS is just MS PowerShell with SharePoint 2010 library added in and he proves it by looking at the shortcut properties when you right click on Start | Programs |SharePoint 2010 | SharePoint Management Shell.

Here we see the target is just:

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 ' "

Here we see that the shortcut is just a PowerShell Script of it’s own:

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

We looked at the Add-PSSnapIn command previously and turns our PowerShell environment into a SharePoint Management Shell environment by adding the microsoft.sharepoint.powershell library. But this also shows us that we can set variables using the $VariableName = Value format.

Prove that to yourself by opening PowerShell and enter:

$robot = "I Love Robots"

and then do:

$robot 

So we’re checked out on setting PowerShell variables.

Finally, the SharePoint.ps1 script includes a largish #sig block of apparently random characters. I presume this has something to do with signing scripts. Recall in our create and delete script that we did last week, we included a   line that said:

Set-ExecutionPolicy RemoteSigned

Now, obviously, I plagerized this from somewhere becasue you all know as much about this or more than I do. And I apologize to my source, whoever you are, because I also try to credit you guys when we benefit from your genius. But, since it’s escaped me, I can only say that I recall reading that this command will allow local scripts to run unsigned but still require remote scripts to have a signature. I’m guessing this big block of characters has something to do with that kind of signature.

hth

-robot

Create and Delete Web App, Site, and Web using PowerShell

SharePoint 2010 retains the ambiguous naming of the various containers so that anyone new to the technology will end up getting confused. As a result, I feel compelled to explain, as best I can, everytime the subject comes up, the relationships using the appropriate terms: Web Apps, Sites and Webs. At the same time, we can hook them in our head to the SMS commands that we use to work with them.

Web Apps are an IIS entity. Consequently, while web apps can share ApplicationPools, everything in a single web app shares a single Application Pool. Also, while a single web app can support any number of site collections, it can only have one host header. This means that one site collection can own the host header url : http://<host header> while all the other will have a url: http://<host header>/SiteCollectionTopLevelSiteName.

In SMS we use new-SPWebApplication and remove-SPWebApplication to create and destroy web apps.

From the SharePoint administrators’ point of view, a “Site” is really a “Site Collection” which is a single top-level site and zero or more sub-sites. Interactively, Site Collections are created in Central Admin and are assigned to a specfic web app and have their own content database. If there’s already a Site Collection using the web app’s host header, Central Admin will make you give it a URL under that host header.

In SMS we use new-SPSite and remove-SPSite to create and delete a site collection.

Finally, a Web, aka Team Site, is a single SharePoint site. It uses a site template and can support any number of sites below it. Webs are either a top level site or a sub-site.

In SMS, we use new-SPWeb and remove-SPweb to create and delete webs.  

So, after you install SharePoint, if you can open Central Admin and if you can open the SharePoint Management Shell, you can create and delete Web Apps, Sites and Web at will. While it’s easy enough using Central Admin, I was thinking the script that does all of this might be interesting.

I came up with this:

function Pause ($Message="Press any key to continue...")
{
Write-Host -NoNewLine $Message
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Write-Host ""
}

Set-ExecutionPolicy RemoteSigned

New-SPWebApplication -Name MyHostHeader -Port 80
   -HostHeader MyHostHeader -URL http://MyHostHeader
   -AllowAnonymousAccess -ApplicationPool MyHostHeaderAppPool
   -ApplicationPoolAccount (Get-SPManagedAccount MyServer\SVC_SPAppPool03)
ECHO "Web Application Created"
ECHO ""
PAUSE

New-SPSite -Url http://MyHostHeader -OwnerAlias MyServer\Administrator
   -Name MyHostHeader -Template "STS#0"
ECHO "Site Collection Created"
ECHO ""
PAUSE

New-SPWeb -Url http://MyHostHeader/TeamSite -Template "STS#0"
ECHO "Team Sub-Site Created"
ECHO ""
PAUSE

Remove-SPWeb http://MyHostHeader/TeamSite
ECHO "Team Sub-Site Deleted"
ECHO ""
PAUSE

Remove-SPSite -Identity http://MyHostHeader
Echo "Site Collection Deleted"
ECHO ""
PAUSE

Remove-SPWebApplication -Identity http://MyHostHeader
   -DeleteIISSite -RemoveContentDatabase
Echo "Web Application Deleted"
ECHO ""
PAUSE

To see it work, copy it to Notepad and delete the line breaks I added to help with formatting here on the page.  Save it as MyScript.ps1. Open your SharePoint Management Shell and run it by using the full path and filename. If you navigate to the folder where you saved it, you have to call it with:

 .\MyScript.ps1

This will create a web app, site and web and then delete the web, the site and the web app, each time, pausing so you can browse out an see it. I got the PAUSE function from the PowerShell Team blog here.

hth

-robot

Windows PowerShell 2.0

So, we’re looking at this SharePoint Management Shell and learning a bit about it. One thing for sure, if you’re a SharePoint admin you’re going to want to run Windows PowerShell locally.

Here’s the download page from TechNet

This is what Microsoft calls its “Core Management Framework” which includes the Remote Manager and the Background Intelligent Transfer Service (BITS.) The download I chose for my XP laptop did not include the BITS component. When it’s installed, you get the command line interface and the “Integrated Scripting Environment.” They appear under Start | Programs | Accessories | Windows PowerShell.  

Also, there’s a decent intro to PowerShell scripting from our new best friend Todd on TechNet here.

-robot

Windows PowerShell and the SharePoint Management Shell

If you’re an expert at this then you’re ahead of me here. My new install of Server 2008 R2 includes a Windows PowerShell (WPS) and my SharePoint install includes a SharePoint Management Shell (SMS).

I found this on MSDN where they explain that SMS is just WPS with the SharePoint PowerShell snap-in added. Also, this is part of the move away from the Shared Service Provider to a more generic Service Application Framework that will support all of Office 14. Here, we can see that Microsoft claims this approach will improve development, integration, load balancing, authentication and back-up\restore.

There’s also a discussion of the PowerShell and SharePoint Foundation here where you’ll find links to the beginner guides for WPS.

I also found the help file for the SMS Cmdlets here. This page is labeled for the Search Server 2010 Express but the SharePoint 2010 Beta commands are included in the third of the three help files listed.

Finally, our new best friend David explains here that cmdlets can be written into a .psl file and run from the PS> prompt as well as where to find your $profile so you can add the SharePoint cmdlets into your generic PowerShell environment.

-robot

Okay, we know not to try to browse to web applications with non-standard host headers from the server because the loop back restrictions Windows places on us. Here, they explain that we can hack the registry to eliminate the problem but that’s not really necessary for our purposes now.

So let’s look at the SMS command for our new web application:

New-SPWebApplication
-Name "Contoso Internet Site"
-Port 80
-HostHeader sharepoint.contoso.com
-URL https://www.contoso.com
-ApplicationPool ContosoAppPool
-ApplicationPoolAccount (Get-SPManagedAccount MyServer\Administrator)

Now, I’ve added line breaks for readability so we’ll have to delete them to make it work. I’ve also created the managed account on Central Admin’s Security | Configure Managed Accounts.

It runs for a minute and I get a return of:

DisplayName               Url
-----------               ---
sharepoint.contoso.com    http://www.contoso.com

Now, I can’t browse to it locally, but I can from another computer. Of course, there will be an issue with DNS, but I added www.contoso.com to my hosts file and pointed it to the SharePoint server.

So I browse to the site and I get prompted to login, but then I get The webpage cannot be found. This makes sense because there’s no site collection installed there yet. When we did this interactively in CA, it gave us a popup window that told us to go create a site collection.

Let’s go back to CA and look at Application Management | Manage Web Applications and you’ll see the new web application listed. What we need to do is go back to the Application Management pages and click Create Site Collections. Here, our new web application will appear in the web app pull down; if not, pull it down and select the new web app. We give it a name, select a template, specify an owner and click OK. After it processes, we can get a link to our new site collection’s home page. Click it and it will prompt us to login and there it is.

We can back out by moving to the Application Management page and, in the Delete a Site Collection. Pull down the Site Collection pick list and click on Change Site Collection. Here, we get another pull down to select the site collection we just created. Now, we also get an option to select any additional site collection we may have created in the selected web application. When we can click OK, we go back to the delete page and we click Delete. We get a dialog box to confirm and click OK.

We go back to the Application Management page and we can click on Manage Web Applications, select our web application and click Delete. This time we get a DHTML popup where we dot the options to delete the content database and the IIS web site and click Delete and then OK on the confirmation popup.

So, next, what we want to do is add to our new web app command to allow anonymous access and then we’ll add the command to create the site collection creating our first SMS script to do both at once. Then we’ll do another script to work the delete.

 hth!

-robot