PowerShell: Through the SharePoint Looking Glass
Brian Caauwe – Manager, Business Productivity
April 22nd, 2017
Session Agenda
• What you NEED to know
• Scripting for on-prem
• Scripting for online
Who am I?
• Brian Caauwe
• Manager, Consultant, Speaker and new dad
• Email: bcaauwe@avtex.com
• Twitter: @bcaauwe
• Technical Editor
• Professional SharePoint 2013 Administration
• Certifications
• MCM: SharePoint Server 2010
• MCSM: SharePoint
Thank you Sponsors!
SharePoint Environment
Poll
• SharePoint 2007
• SharePoint 2010
• SharePoint 2013
• SharePoint 2016
• SharePoint Online
Investigation
Automation
Learning
Get-Help
• ALWAYS run “As Administrator”
• Uses powershell.exe under Windowssystem32
• Microsoft.SharePoint.Powershell snap-in
– C:Program FilesCommon FilesMicrosoft SharedWeb Server
Extensions15[6]CONFIGPOWERSHELLRegistrationsharepoint.ps1
• Sets ExecutionPolicy to RemoteSigned
• 700+ Cmdlets – SharePoint 2013
• 800+ Cmdlets – SharePoint 2016
Management Shell
On-Premise
• Microsoft.SharePoint namespace
• Server Architecture
• SPFarm
• SPWebApplication
• SPContentDatabase
• Site Architecture
• SPSite
• SPWeb
• SPList
• SPListItem
• MSDN Resource
• https://msdn.microsoft.com/en-us/library/jj193058.aspx
Server Side
Object Model
• Uses powershell.exe same as on-prem
• Imports the module
• Microsoft.Online.SharePoint.PowerShell
• Sets ExecutionPolicy to RemoteSigned
• Only 42 Cmdlets
• Also includes 84 Cmdlets for Azure AD Module
• Supplement
• Client Side Object Model (CSOM)
Management Shell
Online
• Microsoft.SharePoint.Client namespace
• Starting Point
• Microsoft.SharePoint.Client.ClientContext
• Microsoft.SharePoint.Client.SharePointOnlineCredentials
• Microsoft.SharePoint.Client.Web
• Microsoft.SharePoint.Client.Site
• MSDN Resource
• https://msdn.microsoft.com/en-
us/library/microsoft.sharepoint.client.aspx
Client Side
Object Model
• Call PowerShell from Windowssystem32
• Register Microsoft.SharePoint.Powershell snap-in
-psconsolefile “C:Program FilesCommon FilesMicrosoft
SharedWeb Server Extensions15[6]CONFIGPOWERSHELL
Registrationpsconsole.psc1”
• Call Script
-command “E:PowerShellSet-ScriptName.ps1”
• Logging
Scheduled Tasks
Memory Leaks
• Disposable Objects
$web.Dispose()
$site.Dispose()
• SPAssignment – Garbage Collector
Global
Start-SPAssignment -Global
Get-SPSite –Limit All | Get-SPWeb | Select Url, WebTemplate, WebTemplateId
Stop-SPAssignment -Global
Scoped
$w = Get-SPWebApplication http://www.company.com
$siteScope = Start-SPAssignment
foreach ($site in ($siteScope | Get-SPSite -Limit All –WebApplication $))
{
$webScope = Start-SPAssignment
foreach ($web in ($webScope | Get-SPWeb -Limit All -Site $site))
{
## Do Something
}
Stop-SPAssignment $webScope
}
Stop-SPAssignment $siteScope
• Other Assemblies
• IIS (WebAdministration)
• SQL
• Exchange
• User Profile
• Microsoft.Office.Server.UserProfiles
• Managed Metadata
• Microsoft.SharePoint.Taxonomy
On-prem
• High Level Cmdlets
• Get-SPWebApplication
• Get-SPSite
• Get-SPWeb
Day-to-Day
$w = Get-SPWebApplication http://my.company.com
$site = Get-SPSite http://my.company.com
$site | Get-SPWeb -Limit All
• Will utilize Log Cmdlets
• New-SPLogFile
• Merge-SPLogFile
Troubleshooting
New-SPLogFile
Merge-SPLogFile –Path “C:Debugerror.log” –Correlation
470a4ec2-985d-43be-a14e-176eb1c39672
• Will utilize SPSolution Cmdlets
• Add-SPSolution
• Get-SPSolution
• Install-SPSolution
Customizations
Add-SPSolution –LiteralPath “C:Solutionsspcmis.wsp”
$sol = Get-SPSolution | ?{$_.Name –eq “spcmis.wsp”}
$sol | Install-SPSolution –GACDeployment –AllWebApplications –Compatibilitylevel All
• End users request a site into a list
• Automate site creation
• Inform user of progress
• Enforce Governance
Site Request
Site Request
Lay of the land…
• List Location: http://portal.lab.com
• List Title: Site Requests
• Columns:
• Title* (Single line of text)
• Abbreviation* (Single line of text)
• Site Description (Multiple lines of text)
• Site Type* (Choice)
• Size* (Choice)
• Site Owner* (Person)
• Secondary Owner* (Person)
• Site Contributors (Person-Multi)
• Site Visitors (Person-Multi)
• Status (Choice) [Hidden]
• Default Value [Submitted]
• Status Description (Multiple lines of text) [Hidden]
* Required Fields
Demo
Site Request Process
Online
• Utilize Cmdlet
• Connect-SPOService
Connecting
Connect-SPOService –url https://tenant-admin.sharepoint.com
• Limited Cmdlets
• Get/Set-SPOSite
• Get/Set/Remove-SPOUser
Day-to-Day
$site = Get-SPSite https://[tenant].sharepoint.com
Set-SPOUser -LoginName user@email.com -Site $site -IsSiteCollectionAdmin $true
Core
Objects
CSOM PnP
• Everything starts with the Client Context
Client Side
Object Model
$username = “user@email.com”
$password = ConvertTo-SecureString -String “pass@word1” -AsPlainText
-Force
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$ctx.Credentials = New-Object
Microsoft.SharePoint.Client.SharePointOnlineCredentials($username,
$password)
• Using Load and ExecuteQuery to use objects
Client Side
Object Model
$web = $ctx.Web
$site = $ctx.Site
$ctx.Load($web)
$ctx.Load($site)
$ctx.ExecuteQuery()
$creds = New-Object System.Management.Automation.PSCredential
“user@email.com”, (ConvertTo-SecureString “pass@word1” -AsPlainText -force)
Connect-PnPOnline –Url $url –Credentials $creds
$ctx = Get-PnPContext
$ctx.RequestTimeout = [System.Threading.Timeout]::Infinite
$root = Get-PnPWeb
PnP PowerShell
Starts with the Connect-PnPOnline
Revolves around PnPContext
$provisioningList = Get-PnPList -Identity $listName
$query = "<View><Query><Where><Eq><FieldRef Name='Status'></FieldRef><Value
Type='Text'>Submitted</Value></Eq></Where></Query></View>"
$items = Get-PnPListItem -List $provisioningList -Query $query
Set-PnPListItem -List "Provisioning List" -Identity $item.Id -Values @{"Status"="In
Progress";"StatusDescription"="Starting site creation process"}
New-PnPGroup -Title $ownerGroupName -Owner $siteOwner.Email
Get-PnPGroup -AssociatedOwnerGroup -Web $web
Set-PnPGroup -Identity $ownersGroup -SetAssociatedGroup Owners
Set-PnPGroupPermissions -Identity $ownersGroup -AddRole "Full Control“
Add-PnPUserToGroup -LoginName $siteOwner.Email -Identity $ownersGroup
PnP PowerShell
Wraps CSOM methods and objects
New-PnPTenantSite -Title $item.Title -Url $siteLocation -Owner $siteOwner.Email -
TimeZone 11 -Description $siteDescription -Template $siteTemplate –Wait
New-PnPWeb -Title $item.Title -Url $webLocation -Template $siteTemplate -Web
$parentWeb -Locale 1033 -BreakInheritance
Demo
PnP PowerShell
Questions
Feedback Url :
http://whova.com/event/program/168097/?email=bcaauwe@avtex.com&simpl
e_agenda=True&platform=webapp
Give Me Feedback
Whova
How to get ahold of me?
• Brian Caauwe
• Email: bcaauwe@avtex.com
• Twitter: @bcaauwe
http://webinars.avtex.com/
Resources
• Windows PowerShell for SharePoint 2013
• https://technet.microsoft.com/en-us/library/ee662539.aspx
• Office Dev Patterns and Practices GitHub
• https://github.com/SharePoint/PnP
• PnP PowerShell
• https://github.com/sharepoint/pnp-powershell/
• SharePoint Server 2013 Client Components SDK
• http://www.microsoft.com/en-us/download/details.aspx?id=35585
• SharePoint Online Management Shell
• http://www.microsoft.com/en-us/download/details.aspx?id=35588
• Display a list of OneDrive for Business Site Collections
• https://technet.microsoft.com/en-us/library/dn911464.aspx
• Using Lambda Expressions with CSOM in PowerShell
• http://www.itunity.com/article/loading-specific-values-lambda-expressions-sharepoint-csom-api-
windows-powershell-1249
PowerShell: Through the SharePoint Looking Glass

PowerShell: Through the SharePoint Looking Glass

  • 1.
    PowerShell: Through theSharePoint Looking Glass Brian Caauwe – Manager, Business Productivity April 22nd, 2017
  • 2.
    Session Agenda • Whatyou NEED to know • Scripting for on-prem • Scripting for online
  • 3.
    Who am I? •Brian Caauwe • Manager, Consultant, Speaker and new dad • Email: bcaauwe@avtex.com • Twitter: @bcaauwe • Technical Editor • Professional SharePoint 2013 Administration • Certifications • MCM: SharePoint Server 2010 • MCSM: SharePoint
  • 4.
  • 5.
    SharePoint Environment Poll • SharePoint2007 • SharePoint 2010 • SharePoint 2013 • SharePoint 2016 • SharePoint Online
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
    • ALWAYS run“As Administrator” • Uses powershell.exe under Windowssystem32 • Microsoft.SharePoint.Powershell snap-in – C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15[6]CONFIGPOWERSHELLRegistrationsharepoint.ps1 • Sets ExecutionPolicy to RemoteSigned • 700+ Cmdlets – SharePoint 2013 • 800+ Cmdlets – SharePoint 2016 Management Shell On-Premise
  • 12.
    • Microsoft.SharePoint namespace •Server Architecture • SPFarm • SPWebApplication • SPContentDatabase • Site Architecture • SPSite • SPWeb • SPList • SPListItem • MSDN Resource • https://msdn.microsoft.com/en-us/library/jj193058.aspx Server Side Object Model
  • 13.
    • Uses powershell.exesame as on-prem • Imports the module • Microsoft.Online.SharePoint.PowerShell • Sets ExecutionPolicy to RemoteSigned • Only 42 Cmdlets • Also includes 84 Cmdlets for Azure AD Module • Supplement • Client Side Object Model (CSOM) Management Shell Online
  • 14.
    • Microsoft.SharePoint.Client namespace •Starting Point • Microsoft.SharePoint.Client.ClientContext • Microsoft.SharePoint.Client.SharePointOnlineCredentials • Microsoft.SharePoint.Client.Web • Microsoft.SharePoint.Client.Site • MSDN Resource • https://msdn.microsoft.com/en- us/library/microsoft.sharepoint.client.aspx Client Side Object Model
  • 15.
    • Call PowerShellfrom Windowssystem32 • Register Microsoft.SharePoint.Powershell snap-in -psconsolefile “C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15[6]CONFIGPOWERSHELL Registrationpsconsole.psc1” • Call Script -command “E:PowerShellSet-ScriptName.ps1” • Logging Scheduled Tasks
  • 16.
    Memory Leaks • DisposableObjects $web.Dispose() $site.Dispose() • SPAssignment – Garbage Collector Global Start-SPAssignment -Global Get-SPSite –Limit All | Get-SPWeb | Select Url, WebTemplate, WebTemplateId Stop-SPAssignment -Global Scoped $w = Get-SPWebApplication http://www.company.com $siteScope = Start-SPAssignment foreach ($site in ($siteScope | Get-SPSite -Limit All –WebApplication $)) { $webScope = Start-SPAssignment foreach ($web in ($webScope | Get-SPWeb -Limit All -Site $site)) { ## Do Something } Stop-SPAssignment $webScope } Stop-SPAssignment $siteScope
  • 17.
    • Other Assemblies •IIS (WebAdministration) • SQL • Exchange • User Profile • Microsoft.Office.Server.UserProfiles • Managed Metadata • Microsoft.SharePoint.Taxonomy
  • 18.
  • 19.
    • High LevelCmdlets • Get-SPWebApplication • Get-SPSite • Get-SPWeb Day-to-Day $w = Get-SPWebApplication http://my.company.com $site = Get-SPSite http://my.company.com $site | Get-SPWeb -Limit All
  • 20.
    • Will utilizeLog Cmdlets • New-SPLogFile • Merge-SPLogFile Troubleshooting New-SPLogFile Merge-SPLogFile –Path “C:Debugerror.log” –Correlation 470a4ec2-985d-43be-a14e-176eb1c39672
  • 21.
    • Will utilizeSPSolution Cmdlets • Add-SPSolution • Get-SPSolution • Install-SPSolution Customizations Add-SPSolution –LiteralPath “C:Solutionsspcmis.wsp” $sol = Get-SPSolution | ?{$_.Name –eq “spcmis.wsp”} $sol | Install-SPSolution –GACDeployment –AllWebApplications –Compatibilitylevel All
  • 22.
    • End usersrequest a site into a list • Automate site creation • Inform user of progress • Enforce Governance Site Request
  • 23.
    Site Request Lay ofthe land… • List Location: http://portal.lab.com • List Title: Site Requests • Columns: • Title* (Single line of text) • Abbreviation* (Single line of text) • Site Description (Multiple lines of text) • Site Type* (Choice) • Size* (Choice) • Site Owner* (Person) • Secondary Owner* (Person) • Site Contributors (Person-Multi) • Site Visitors (Person-Multi) • Status (Choice) [Hidden] • Default Value [Submitted] • Status Description (Multiple lines of text) [Hidden] * Required Fields
  • 24.
  • 25.
  • 26.
    • Utilize Cmdlet •Connect-SPOService Connecting Connect-SPOService –url https://tenant-admin.sharepoint.com
  • 27.
    • Limited Cmdlets •Get/Set-SPOSite • Get/Set/Remove-SPOUser Day-to-Day $site = Get-SPSite https://[tenant].sharepoint.com Set-SPOUser -LoginName user@email.com -Site $site -IsSiteCollectionAdmin $true
  • 28.
  • 29.
    • Everything startswith the Client Context Client Side Object Model $username = “user@email.com” $password = ConvertTo-SecureString -String “pass@word1” -AsPlainText -Force $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url) $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
  • 30.
    • Using Loadand ExecuteQuery to use objects Client Side Object Model $web = $ctx.Web $site = $ctx.Site $ctx.Load($web) $ctx.Load($site) $ctx.ExecuteQuery()
  • 31.
    $creds = New-ObjectSystem.Management.Automation.PSCredential “user@email.com”, (ConvertTo-SecureString “pass@word1” -AsPlainText -force) Connect-PnPOnline –Url $url –Credentials $creds $ctx = Get-PnPContext $ctx.RequestTimeout = [System.Threading.Timeout]::Infinite $root = Get-PnPWeb PnP PowerShell Starts with the Connect-PnPOnline Revolves around PnPContext
  • 32.
    $provisioningList = Get-PnPList-Identity $listName $query = "<View><Query><Where><Eq><FieldRef Name='Status'></FieldRef><Value Type='Text'>Submitted</Value></Eq></Where></Query></View>" $items = Get-PnPListItem -List $provisioningList -Query $query Set-PnPListItem -List "Provisioning List" -Identity $item.Id -Values @{"Status"="In Progress";"StatusDescription"="Starting site creation process"} New-PnPGroup -Title $ownerGroupName -Owner $siteOwner.Email Get-PnPGroup -AssociatedOwnerGroup -Web $web Set-PnPGroup -Identity $ownersGroup -SetAssociatedGroup Owners Set-PnPGroupPermissions -Identity $ownersGroup -AddRole "Full Control“ Add-PnPUserToGroup -LoginName $siteOwner.Email -Identity $ownersGroup PnP PowerShell Wraps CSOM methods and objects New-PnPTenantSite -Title $item.Title -Url $siteLocation -Owner $siteOwner.Email - TimeZone 11 -Description $siteDescription -Template $siteTemplate –Wait New-PnPWeb -Title $item.Title -Url $webLocation -Template $siteTemplate -Web $parentWeb -Locale 1033 -BreakInheritance
  • 33.
  • 34.
  • 35.
  • 36.
    How to getahold of me? • Brian Caauwe • Email: bcaauwe@avtex.com • Twitter: @bcaauwe
  • 37.
  • 38.
    Resources • Windows PowerShellfor SharePoint 2013 • https://technet.microsoft.com/en-us/library/ee662539.aspx • Office Dev Patterns and Practices GitHub • https://github.com/SharePoint/PnP • PnP PowerShell • https://github.com/sharepoint/pnp-powershell/ • SharePoint Server 2013 Client Components SDK • http://www.microsoft.com/en-us/download/details.aspx?id=35585 • SharePoint Online Management Shell • http://www.microsoft.com/en-us/download/details.aspx?id=35588 • Display a list of OneDrive for Business Site Collections • https://technet.microsoft.com/en-us/library/dn911464.aspx • Using Lambda Expressions with CSOM in PowerShell • http://www.itunity.com/article/loading-specific-values-lambda-expressions-sharepoint-csom-api- windows-powershell-1249

Editor's Notes

  • #10 You won’t learn by books or sessions Find YOUR practical applications
  • #11 Use Get-Help for information on Cmdlets Full property for verbose information Examples for… Examples