Today I spoke in front of a great crowd at SharePoint Saturday Austin, which was an all-around blast!
The presentation was about PowerShell (shocker, right?) – but this time had a different vibe as I modified one of my presentations to work in SharePoint 2013 – and I also utilized PowerShell Web Access in Google Chrome for 100% of the technical demonstrations.
Below is a description of the topic itself along with PowerPoint Slides and the PowerShell and XML code that was used in the demo:
The Power is in the Shell, use it wisely!
The topic: The Power is in the Shell, use it wisely!
The story: You may have heard of PowerShell, but do you know what it’s capable of? Gone are the days of long, painful STSADM batch files – we have Windows PowerShell, and it’s here to stay.Learn how you can use Windows PowerShell both to perform simple one-off tasks as well as complex, bulk operations. Leveraging the Object Model gives Administrators and Developers the ability to do in a few lines of code what would’ve taken a lot more work (and probably a Developer or two) in the WSS platform.
In this demo filled session, you’ll see how you can get started with PowerShell, and you will hopefully leave with not only a greater understanding of what PowerShell is – but what it is capable of and how you can start using it to automate tasks in your SharePoint 2010 or 2013 environment.
Here are my slides, and below are the code snippets:
PowerShell code:
Demo 1
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue $Template = Get-SPWebTemplate BLANKINTERNETCONTAINER`#0 $Url = "http://sp2013.spsatx.adventureworks.com" $Site = Get-SPSite $Url $Site = New-SPSite $Url -OwnerAlias adventureworks\rdennis -Template $Template $Web = $Site.RootWeb $web | Get-Member -MemberType property</em></em></em> $Web.Title = "SharePoint Saturday - Austin, TX" $Web.Update() $Web.Dispose() $Site.Dispose()
Demo 2
function Set-SPWebTitle { <# .Synopsis Use Set-SPWebTitle to update the Title of a SharePoint Web. .Description This function uses SharePoint Cmdlets and Object Model code to set the title property of a SharePoint Web. .Example C:\PS>Set-SPWebTitle -WebUrl http://<siteUrl> -Title "New Title" This example updates the title of a web at http://<siteUrl> to "New Title" .Notes Name: Set-SPWebTitle Author: Ryan Dennis Last Edit: 2/24/2013 .Link http://www.sharepointryan.com http://twitter.com/SharePointRyan .Inputs None .Outputs None #Requires -Version 2.0 #> [CmdletBinding()] Param( [Parameter(Mandatory=$true)][System.String]$WebUrl, [Parameter(Mandatory=$true)][System.String]$Title ) Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue $Web = Get-SPWeb $WebUrl $Web.Title = $Title $Web.Update() $Web.Dispose() }
Demo 3
function New-SPWebFromXml { <# .Synopsis Use this PowerShell Script to create lots of sites, very quickly! .Description This advanced function uses Get-Content, ForEach-Object and New-SPWeb cmdlets to bulk create SharePoint webs from an XML file. .Example C:\PS>New-SPWebFromXml -Url http://<siteUrl> -XmlInput c:\Sites.xml This example creates sites from an XML located at c:\Sites.xml under a site collection at http://<siteUrl>. .Notes Name: New-SPWebFromXml Author: Ryan Dennis Last Edit: 2/24/2013 Keywords: New-SPWeb, Get-Content, ForEach-Object, New-Timespan, XML .Link http://www.sharepointryan.com http://twitter.com/SharePointRyan .Inputs None .Outputs None #Requires -Version 2.0 #> [CmdletBinding()] Param( [Parameter(Mandatory=$true)][System.String]$Url, [Parameter(Mandatory=$true)][System.String]$XmlInput ) Clear-Host # If SharePoint Snapin isn't loaded - load it # if ((Get-PSSnapin Microsoft.SharePoint.PowerShell)-eq $null) { Write-Host "Adding SharePoint Snapin" Add-PSSnapin Microsoft.SharePoint.PowerShell } $i=0 do { # Read in list of sites from XML file # [xml]$SitesXml = Get-Content $($XmlInput) if ($SitesXml -eq $null) { return } $siteCount = $SitesXml.Sites.Site.Count Write-Progress -Activity "Provisioning SharePoint Webs from XML" -PercentComplete (($i/$siteCount) * 100) ` -Status "Retrieving XML Data" -ErrorAction SilentlyContinue Start-Sleep -Seconds 2 Start-SPAssignment -Global # Loop through each site node to extract data # $SitesXml.Sites.Site | ForEach-Object { $SiteTitle = [string]$_.SiteTitle $SiteUrl = [string]$_.SiteUrl Write-Progress -Activity "Provisioning SharePoint Webs from XML" -PercentComplete (($i/$siteCount) * 100) ` -Status "Creating an SPWeb at $($SiteUrl)" -ErrorAction SilentlyContinue $i++ # Site specifics # $WebTemplate = "BLANKINTERNET`#0" $LangId = "1033" $SiteUrl = $Url+$SiteUrl # Create the SPWeb # if ($_.create -ne "false") { $Web = New-SPWeb -Url $SiteUrl -Language $LangId -Template $WebTemplate ` -Name $SiteTitle } else { $Web = Get-SPWeb -Identity $SiteUrl } # Get publishing site and web objects $site = $web.Site $pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web) $pagesLib = $pWeb.PagesList # Enable moderation on Pages libraries # $pagesLib.EnableModeration = $true; $pagesLib.EnableMinorVersions = $true; $pagesLib.Update() # Create all Pages using ForEach-Object # if ($_.Page -ne $null){ $pages = $_.Page | ForEach-Object { # Create page content variables from XML file $PageTitle = [string]$_.PageTitle $PageUrl = [string]$_.PageUrl $PageLayout = [string]$_.PageLayout $PageContent = [string]$_.PageContent $PageImage = [string]$_.PageImage $layout = $pWeb.GetAvailablePageLayouts() | Where-Object {$_.Title -match $PageLayout} # Create blank page using Add method Write-Progress -Activity "Provisioning SharePoint Webs from XML" -PercentComplete (($i/$siteCount) * 100) ` -Status "Creating $($PageTitle).aspx page" -ErrorAction SilentlyContinue $pages = $pWeb.GetPublishingPages($pWeb) $page = $pages.Add($PageUrl, $layout) $page.Update() # Update the filename to the one specified in the XML, add HTML content to Page Content zone $item = $page.ListItem $item["Title"] = $PageTitle; $item.Update() # Check-in and publish page $item.File.CheckIn("") $item.File.Publish("") $item.File.Approve("") $file = $item.File # If the page is marked as the Home Page in XML, set it as welcome page # if($_.IsHomePage -eq "true"){ $pWeb.DefaultPage = $file } $pWeb.Update() } #end Page Foreach } # End if block # } # End ForEach-Object loop # } while ($i -lt $siteCount) } # End function
XML Syntax:
<?xml version="1.0" encoding="utf-8"?> <Sites> <SiteAdmins> <User>adventureworks\rdennis</User> <User>adventureworks\spfarm</User> </SiteAdmins> <!-- root site --> <Site Create="false"> <SiteTitle>Home</SiteTitle> <SiteUrl>/</SiteUrl> <Page IsHomePage="false"> <PageTitle>Intranet Governance</PageTitle> <PageUrl>Intranet-Governance.aspx</PageUrl> <PageLayout>Blank Web Part Page</PageLayout></Page> <Page IsHomePage="false"> <PageTitle>Terms of Use</PageTitle> <PageUrl>Terms-of-Use.aspx</PageUrl> <PageLayout>Blank Web Part Page</PageLayout></Page> <Page IsHomePage="false"> <PageTitle>Privacy Policy</PageTitle> <PageUrl>Privacy-Policy.aspx</PageUrl> <PageLayout>Blank Web Part Page</PageLayout></Page> <Page IsHomePage="false"> <PageTitle>Linking Policy</PageTitle> <PageUrl>Linking-Policy.aspx</PageUrl> <PageLayout>Blank Web Part Page</PageLayout></Page> <Page IsHomePage="false"> <PageTitle>Legal Disclaimer</PageTitle> <PageUrl>Legal-Disclaimer.aspx</PageUrl> <PageLayout>Blank Web Part Page</PageLayout></Page> <Page IsHomePage="false"> <PageTitle>Help Me</PageTitle> <PageUrl>Help-Me.aspx</PageUrl> <PageLayout>Blank Web Part Page</PageLayout></Page> </Site> <!-- 2nd level sites --> <Site> <SiteTitle>My Company</SiteTitle> <SiteUrl>/My-Company</SiteUrl> <Page IsHomePage="true"> <PageTitle>My Company</PageTitle> <PageUrl>My-Company.aspx</PageUrl> <PageLayout>Blank Web Part Page</PageLayout></Page> <Page IsHomePage="false"> <PageTitle>Our Culture</PageTitle> <PageUrl>Our-Culture.aspx</PageUrl> <PageLayout>Blank Web Part Page</PageLayout></Page> <Page IsHomePage="false"> <PageTitle>History and Timeline</PageTitle> <PageUrl>History-And-Timeline.aspx</PageUrl> <PageLayout>Blank Web Part Page</PageLayout></Page> <Page IsHomePage="false"> <PageTitle>Sustainability</PageTitle> <PageUrl>Sustainability.aspx</PageUrl> <PageLayout>Blank Web Part Page</PageLayout></Page> <Page IsHomePage="false"> <PageTitle>My Community</PageTitle> <PageUrl>My-Community.aspx</PageUrl> <PageLayout>Blank Web Part Page</PageLayout></Page> </Site> <Site> <SiteTitle>Our Customers</SiteTitle> <SiteUrl>/Our-Customers</SiteUrl> <Page IsHomePage="true"> <PageTitle>Our Customers</PageTitle> <PageUrl>Our-Customers.aspx</PageUrl> <PageLayout>Blank Web Part Page</PageLayout></Page> <Page IsHomePage="false"> <PageTitle>Customer Engagement</PageTitle> <PageUrl>Customer-Engagement.aspx</PageUrl> <PageLayout>Blank Web Part Page</PageLayout></Page> <Page IsHomePage="false"> <PageTitle>Employee Ambassadors</PageTitle> <PageUrl>Employee-Ambassadors.aspx</PageUrl> <PageLayout>Blank Web Part Page</PageLayout></Page> </Site> <Site> <SiteTitle>News and Info</SiteTitle> <SiteUrl>/News-And-Info</SiteUrl> <Page IsHomePage="false"> <PageTitle>Utility Performance</PageTitle> <PageUrl>Utility-Performance.aspx</PageUrl> <PageLayout>Blank Web Part Page</PageLayout></Page> <Page IsHomePage="false"> <PageTitle>News Archive</PageTitle> <PageUrl>News-Archive.aspx</PageUrl> <PageLayout>Blank Web Part Page</PageLayout></Page> <Page IsHomePage="false"> <PageTitle>Calendar</PageTitle> <PageUrl>Calendar.aspx</PageUrl> <PageLayout>Blank Web Part Page</PageLayout></Page> <Page IsHomePage="false"> <PageTitle>Social Media</PageTitle> <PageUrl>Social-Media.aspx</PageUrl> <PageLayout>Blank Web Part Page</PageLayout></Page> </Site> <Site> <SiteTitle>Task Center</SiteTitle> <SiteUrl>/Task-Center</SiteUrl> <Page IsHomePage="true"> <PageTitle>Task Center</PageTitle> <PageUrl>Task-Center.aspx</PageUrl> <PageLayout>Blank Web Part Page</PageLayout></Page> <Page IsHomePage="false"> <PageTitle>Travel</PageTitle> <PageUrl>Travel.aspx</PageUrl> <PageLayout>Blank Web Part Page</PageLayout></Page> <Page IsHomePage="false"> <PageTitle>Toolbox</PageTitle> <PageUrl>Toolbox.aspx</PageUrl> <PageLayout>Blank Web Part Page</PageLayout></Page> <Page IsHomePage="false"> <PageTitle>Managing Employees</PageTitle> <PageUrl>Managing-Employees.aspx</PageUrl> <PageLayout>Blank Web Part Page</PageLayout></Page> </Site> <Site> <SiteTitle>Collaboration</SiteTitle> <SiteUrl>/Collaboration</SiteUrl> <Page IsHomePage="true"> <PageTitle>Collaboration</PageTitle> <PageUrl>Collaboration.aspx</PageUrl> <PageLayout>Blank Web Part Page</PageLayout></Page> <Page IsHomePage="false"> <PageTitle>Communities</PageTitle> <PageUrl>Communities.aspx</PageUrl> <PageLayout>Blank Web Part Page</PageLayout></Page> <Page IsHomePage="false"> <PageTitle>Employee Surveys</PageTitle> <PageUrl>Employee-Surveys.aspx</PageUrl> <PageLayout>Blank Web Part Page</PageLayout></Page> </Site> </Sites>
2 thoughts on “SharePoint Saturday Austin 2013”