Everyone writes PowerShell scripts, but writing scripts to create scripts?
I had an interesting challenge. I needed to create a full site hierarchy in “the cloud” that mirrored my production environments, to enable offsite consultants to design our navigation.
We’re talking hundreds of sites and libraries.
The challenge was to automate it and not use any internal content.
PowerShell to the rescue! First let’s in one line write the script to output all the PowerShell statements that will create all the sites we need:
get-spwebapplication "http ://SharePoint/" | get-spsite -limit ALL | get-spweb -Limit ALL | ForEach-Object { "new-spweb -url `"$($_.url)`" -name `"$($_.title)`" -Template `"STS#0`"" } > siteScript.ps1
This statement does the following:
- Iterates through the entire production web application
- Pushes all the Site Collections into the Pipeline
- For each site collection, pushes all the SPWebs (sites) into the Pipeline
- Generates the New-SPweb command for each site
Note I used the “backtick” (`) so I could force each ‘$’ and each quote as literals. For my purposes the standard team site (STS#0) was sufficient.
All I then did was to run the sitescript.ps1 output in the cloud. Of course the web application, managed paths and site collection scripting was run in advance.
Next I needed to create the hundreds of Document Libraries in the cloud to mirror production.
That script required multiple script lines to be output, and I needed to filter out several kinds of document libraries that didn’t need to be created. Here’s the script:
$siteCol = Get-SPSite "http ://SharePoint/" write-host "`$templateName=`"Document Library`"" get-spwebapplication "http ://SharePoint/" | get-spsite -limit ALL | get-spweb -Limit ALL | ForEach-Object { foreach($JPLib in $_.lists) { if ( ($JPlib.BaseType -ne "DocumentLibrary") -or ($JPlib.hidden) ) {} elseif ($JPLib.Title -Match "Photo|Image|CustomizedsReports|Templates|Pages|Picture|cache|style|Slide") {} elseif ($JPLib.Title -Match "Assets|Collection") {} elseif ($JPLib.BaseTemplate -ne "DocumentLibrary") {} elseif ($JPLib.Title -eq "Shared Documents") {} elseif ($JPLib.Title -eq "Documents") {} else { write-host "`# $($_.url) $($JPLib.Title)" write-host "`$SPWeb=get-spweb $($_.url)" write-host "`$listTemplate = `$SPWeb.ListTemplates[`$TemplateName]" write-host "`$JPGuid=`$SPWeb.Lists.Add(`"$($JPLib.title)`",`"$($JPLib.title)`",`$listTemplate)" write-host "`$ls=`$SPWeb.Lists[`$JPGuid]" write-host "`$ls.onquicklaunch=`$true" write-host "`$ls.update()" } } #foreach list } #foreach object
It worked like a charm. Plus the scripts can be run again to recreate the cloud copy. The “Cloud” to which I refer is CloudShare. So far, I am very pleased with the service and functionality for the price.
3 thoughts on “Scripts To Create Scripts…”
Leave a Reply
Want to talk?
Drop us a line. We are here to answer your questions 24*7.
Joel, Great post.. I’m just curious if there is a missing closing } at the end…
–Thanks Gerald
thanks, good catch! I corrected it, and added comments around the close brace, which helps map open to close.
Cheers,
joel
Wow.. this is wonderful stuff! Makes it real easy to copy an entire site hierarchy, along with libraries, to any other farm.