{"id":2015,"date":"2012-03-07T06:35:24","date_gmt":"2012-03-07T06:35:24","guid":{"rendered":"https:\/\/poiseddevelopers.com\/reality-tech\/?p=2015"},"modified":"2024-05-13T10:42:05","modified_gmt":"2024-05-13T10:42:05","slug":"scripts-to-create-scripts","status":"publish","type":"post","link":"https:\/\/poiseddevelopers.com\/reality-tech\/scripts-to-create-scripts\/","title":{"rendered":"Scripts To Create Scripts\u2026"},"content":{"rendered":"<p>Everyone writes PowerShell scripts, but writing scripts to create scripts?<\/p>\n<p>I had an interesting challenge.\u00a0 I needed to create a full site hierarchy in \u201cthe cloud\u201d that mirrored my production environments, to enable offsite consultants to design our navigation.<\/p>\n<p>We\u2019re talking hundreds of sites and libraries.<\/p>\n<p>The challenge was to automate it and not use any internal content.<\/p>\n<p>PowerShell to the rescue!\u00a0 First let\u2019s in one line write the script to output all the PowerShell statements that will create all the sites we need:<\/p>\n<pre data-uw-rm-sr=\"\">get-spwebapplication \"http :\/\/SharePoint\/\" | get-spsite -limit ALL |\u00a0 get-spweb\u00a0 -Limit ALL | ForEach-Object { \"new-spweb -url `\"$($_.url)`\" -name `\"$($_.title)`\"\u00a0 -Template `\"STS#0`\"\" } &gt; siteScript.ps1<\/pre>\n<p>This statement does the following:<\/p>\n<ul>\n<li>Iterates through the entire production web application<\/li>\n<li>Pushes all the Site Collections into the Pipeline<\/li>\n<li>For each site collection, pushes all the SPWebs (sites) into the Pipeline<\/li>\n<li>Generates the New-SPweb command for each site<\/li>\n<\/ul>\n<p data-uw-rm-sr=\"\">Note I used the \u201cbacktick\u201d (`) so I could force each \u2018$\u2019 and each quote as literals. For my purposes the standard team site (STS#0) was sufficient.<\/p>\n<p data-uw-rm-sr=\"\">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.<\/p>\n<p>Next I needed to create the hundreds of Document Libraries in the cloud to mirror production.<\/p>\n<p>That script required multiple script lines to be output, and I needed to filter out several kinds of document libraries that didn\u2019t need to be created.\u00a0 Here\u2019s the script:<\/p>\n<pre data-uw-rm-sr=\"\">$siteCol = Get-SPSite \"http :\/\/SharePoint\/\"\r\nwrite-host \"`$templateName=`\"Document Library`\"\"\r\nget-spwebapplication \"http :\/\/SharePoint\/\" | get-spsite -limit ALL |\u00a0 get-spweb\u00a0 -Limit ALL | ForEach-Object {\r\nforeach($JPLib in $_.lists)\r\n{\r\n\u00a0 if ( ($JPlib.BaseType -ne \"DocumentLibrary\") -or ($JPlib.hidden) ) {}\r\n\u00a0 elseif ($JPLib.Title -Match \"Photo|Image|CustomizedsReports|Templates|Pages|Picture|cache|style|Slide\") {}\r\n\u00a0 elseif ($JPLib.Title -Match \"Assets|Collection\") {}\r\n\u00a0 elseif ($JPLib.BaseTemplate -ne \"DocumentLibrary\")\u00a0 {}\r\n\u00a0 elseif ($JPLib.Title -eq \"Shared Documents\")\u00a0 {}\r\n\u00a0 elseif ($JPLib.Title -eq \"Documents\")\u00a0 {}\r\n\u00a0 else\r\n\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 write-host \"`#\u00a0\u00a0 $($_.url)\u00a0\u00a0 $($JPLib.Title)\"\r\n\u00a0\u00a0 write-host \"`$SPWeb=get-spweb $($_.url)\"\r\n\u00a0\u00a0 write-host \"`$listTemplate = `$SPWeb.ListTemplates[`$TemplateName]\"\r\n\u00a0\u00a0 write-host \"`$JPGuid=`$SPWeb.Lists.Add(`\"$($JPLib.title)`\",`\"$($JPLib.title)`\",`$listTemplate)\" \r\n\u00a0\u00a0 write-host \"`$ls=`$SPWeb.Lists[`$JPGuid]\"\r\n\u00a0\u00a0 write-host \"`$ls.onquicklaunch=`$true\"\r\n\u00a0\u00a0 write-host \"`$ls.update()\"\r\n\u00a0 }\r\n} #foreach list\r\n} #foreach object\r\n<\/pre>\n<p>It worked like a charm.\u00a0 Plus the scripts can be run again to recreate the cloud copy.\u00a0\u00a0 The \u201cCloud\u201d to which I refer is CloudShare.\u00a0 So far, I am very pleased with the service and functionality for the price.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Everyone writes PowerShell scripts, but writing scripts to create scripts? I had an interesting challenge.\u00a0 I needed to create a full site hierarchy in \u201cthe cloud\u201d that mirrored my production environments, to enable offsite consultants to design our navigation. We\u2019re talking hundreds of sites and libraries. The challenge was to automate it and not use [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":2017,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[26],"tags":[],"class_list":["post-2015","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"_links":{"self":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/2015","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/comments?post=2015"}],"version-history":[{"count":2,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/2015\/revisions"}],"predecessor-version":[{"id":3952,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/2015\/revisions\/3952"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media\/2017"}],"wp:attachment":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media?parent=2015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/categories?post=2015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/tags?post=2015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}