How to do everything with
PowerShell
Juan Carlos González
September 26th, 2015
Barcelona
Thanks to our Sponsors
Get stamps from all the sponsors
Deposit the passport to enter
the prize raffle
Good luck!
The SPSBCN Passport
FREE BEER!
Get your ticket at the registration
desk
Network and have fun with your
colleagues!
SharePint sponsored by
Michael Collins Pub
Plaça de Sagrada
Família
Starts at 19:30h
They help us improve
for SharePoint Saturday
2016!
Remember to fill your evaluation forms
Juan Carlos González
 Office 365 MVP
 Cloud & Productivity Advisor at MVP CLUSTER
 SUGES, Comunidad de O365 & Nuberos.Net coordinator
 CompartiMOSS Co-Director (www.compartimoss.com)
 Some ways to contact me:
 Twitter: jcgm1978
 LinkedIn: https://nl.linkedin.com/in/juagon
 Contact E-Mails:
 jcgonzalezmartin1978@Hotmail.com
 juancarlos.gonzalez@fiveshareit.es
 Blog: https://jcgonzalezmartin.wordpress.com/
 MVP CLUSTER Web Site: www.mvpcluster.com
Agenda
Default PowerShell
cmdlets
PowerShell for SharePoint possibilities
 What can I do with PowerShell for SharePoint?
Backup &
Restore
Operations
PowerShell provides several ways to interact and
work with SharePoint (OnPrem & Online)
Platform
Administration
Use Server
Side / Client
Side APIs
Troubleshooting
Everything !!!
PowerShell Development Environments
 SharePoint OnPremises – SharePoint Management
Shell:
 Available by default on every WFE / Application Server in a SharePoint Farm
 It provides access in the box to all the SharePoint cmdlets
PowerShell Development Environments
 SharePoint OnPremises – PowerShell Web Access:
PowerShell Development Environments
 SharePoint OnPremises – PowerShell Web Access:
 It provides a way to execute PowerShell cmdlets in the Browser
 In order to use it you have to:
 Enable PowerShell Web Access feature at the WFE(s) level
 Install/Enable PowerShell Web Access components using PowerShell
 Configure Default IIS Web Site on every SharePoint WFE(s) you want to use
PowerShell Web Access
Install-PswaWebApplication –UseTestCertificate
Add-PswaAuthorizationRule -UserName [Dominio][Usuario] -ComputerName
[NombreComputador] -ConfigurationName Microsoft.Powershell
PowerShell Development Environments
 SharePoint OnPremises y SPO – Windows
PowerShell:
 For SP OnPremises, the SharePoint Snap-In has to be loaded at first
PowerShell Development Environments
 SharePoint OnPremises y SPO – ISE:
PowerShell Development Environments
 PowerShell ISE:
 It’s “almost” a development environment for PowerShell providing features
such as Debugging Intellisense Code Coloring …
 Each new ISE version release adds new features and improvements
 It is a feature provided by default in Windows (Client and Server Editions),
although there are some cases you need to enable it (Windows Server 2008 R2)
 SharePoint Snap-In must be previously loaded in the ISE in order to be able
to use SharePoint OnPremises PowerShell cmdelts
 This step is not required for SPO
PowerShell Development Environments
 SharePoint OnPremises y SPO – Visual Studio:
PowerShell Development Environments
 SharePoint OnPremises y SPO – Visual Studio:
 PowerShell support in Visual Studio is provided by means of the PowerShell
Tools for Visual Studio:
 https://visualstudiogallery.msdn.microsoft.com/c9eb3ba8-0c59-4944-9a62-
6eee37294597
 Some features provided by PowerShell Tools for Visual Studio are:
 Create projects for PowerShell scripts and modules
 Execute PowerShell Scripts & Commands right from Visual Studio
 Edit, run and debug PowerShell scripts locally and remotely using the
Visual Studio debugger
 Leverage Visual Studio’s locals, watch, call stack, … for your scripts and
modules

PowerShell Development Environments
 SPO – SharePoint Online Management Shell:
 It provides a shortcut to SharePoint Online default cmdlets
 Updated quite often by Microsoft (last update available: August 2015)
PowerShell Development Environments
for SharePoint OnPremises and SPO
Default PowerShell Cmdlets
 Components of a PowerShell Cmdlet:
We can create
custom cmdlets
Default PowerShell Cmdlets
 SharePoint OnPremises:
 More tan 800 cmdlets in SharePoint 2013 SP 1 (861 cmdlets in SP 2016 IT
Preview)
Get-Command –PSSnapin "Microsoft.SharePoint.PowerShell"
Default PowerShell Cmdlets
 Example # 1 - Get-SPSite
 It allows to get all the Site Collections in the Farm that match the specified
conditions
 http://technet.microsoft.com/es-es/library/ff607950(v=office.15).aspx
Get-SPSite | select url, @{Expression={$_.Usage.Storage/1MB}}
Default PowerShell Cmdlets
 SharePoint Online:
 More than 40 cmdlets available for SPO (August 2015 update)
$spoCmdlets=Get-Command | where {$_.ModuleName -eq “Microsoft.Online.SharePoint.PowerShell"}
$spoCmdlets.Count
$spoCmdlets.Name
Default PowerShell Cmdlets
 Example # 1 - Get-SPOSite
 It allows to get all the Site Collections in a SharePoint Online tenant that match
the specified conditions
 https://technet.microsoft.com/es-es/library/FP161380.aspx
#Ejecución en la Consola de Administración de SharePoint Online
$sUserName="jcgonzalez@nuberosnet.onmicrosoft.com"
$sMessage="Introduce your SPO Credentials"
$sSPOAdminCenterUrl="https://nuberosnet-admin.sharepoint.com/"
$msolcred = Get-Credential -UserName $sUserName -Message $sMessage
Connect-SPOService -Url $sSPOAdminCenterUrl -Credential $msolcred
$spoSiteCollections=Get-SPOSite
Default PowerShell cmdlets for
SharePoint OnPremises and SPO
Using SharePoint APIs in PowerShell
 SharePoint Server Side API:
 SharePoint PowerShell Snap-In provides also access to the full server side API:
 You can use SharePoint objects in the same way you do in Visual Studio
 Example # 1 – How to create a SharePoint list and a add a column to the list:
$spSite=Get-SPSite -Identity $sSiteUrl
$spWeb=$spSite.OpenWeb()
$spWeb.Lists.Add("Lista Grande","Lista Grande",100)
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Text
$spList = $spWeb.Lists["Lista Grande"]
$spList.Fields.Add(“Datos”,$spFieldType,$false)
$spList.Fields["Datos"].Update()
$spList.Update()
Using SharePoint APIs in PowerShell
 SharePoint Server Side API:
 Example # 2 – How to do a CAML Query:
$spSite=Get-SPSite -Identity $sSiteCollection
$spwWeb=$spSite.OpenWeb()
$splList = $spwWeb.Lists.TryGetList($sListName)
$spqQuery = New-Object Microsoft.SharePoint.SPQuery
$spqQuery.Query =
" <Where>
<Contains>
<FieldRef Name='FileLeafRef' />
<Value Type='File'>Farm</Value>
</Contains>
</Where>"
$spqQuery.ViewFields = "<FieldRef Name='FileLeafRef' /><FieldRef Name='Title' />"
$spqQuery.ViewFieldsOnly = $true
$splListItems = $splList.GetItems($spqQuery)
Using SharePoint APIs in PowerShell
 SharePoint Client Side API:
 Client Side API (better known as CSOM) can be used in PowerShell for both
SharePoint OnPremises and SPO
 First step before using the API is to load the required assemblies:
 Once the assemblies are loaded, simply follow CSOM rules
Add-Type -Path "<CSOM_Path>Microsoft.SharePoint.Client.dll"
Add-Type -Path "<CSOM_Path>Microsoft.SharePoint.Client.Runtime.dll"
Using SharePoint APIs in PowerShell
 SharePoint Client Side API:
 Example 1 – Using CSOM for SharePoint OnPremises in PowerShell:
#SharePoint Client Object Model Context
$spCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteColUrl)
$spCredentials = New-Object
System.Net.NetworkCredential($sUserName,$sPassword,$sDomain)
$spCtx.Credentials = $spCredentials
#Root Web Site
$spRootWebSite = $spCtx.Web
#Collecction of Sites under the Root Web Site
$spSites = $spRootWebSite.Webs
#Loading operations
$spCtx.Load($spRootWebSite)
$spCtx.Load($spSites)
$spCtx.ExecuteQuery()
Using SharePoint APIs in PowerShell
 SharePoint Client Side API:
 Example 2 – Using CSOM for SPO in PowerShell:
$spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteColUrl)
$spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUsername, $sPassword)
$spoCtx.Credentials = $spoCredentials
#Root Web Site
$spoRootWebSite = $spoCtx.Web
#Collecction of Sites under the Root Web Site
$spoSites = $spoRootWebSite.Webs
#Loading operations
$spoCtx.Load($spoRootWebSite)
$spoCtx.Load($spoSites)
$spoCtx.ExecuteQuery()
#We need to iterate through the $spoSites Object in order to get individual sites information
foreach($spoSite in $spoSites){
$spoCtx.Load($spoSite)
$spoCtx.ExecuteQuery()
Write-Host $spoSite.Title " - " $spoSite.Url -ForegroundColor Blue
}
Do you see the difference?
Using SharePoint APIs from PowerShell
PowerShell Usage Scenarios
Platform
Administration
There are several PowerShell usage scenarios in
SharePoint (OnPremises and Online)
SharePoint
Migrations
Auditing Tasks
(Farm / Tenant
Level)
Troubleshooting
Solutions
Deployment
PowerShell Usage Scenarios
 Installation & Configuration:
 PowerShell provides more control over the SharePoint installation &
configuration process in regards to:
 Installation Accounts Database Names Service Applications
Configuration …
 It takes some more time than the SharePoint visual installation, but you will get
two great benefits:
1) The configuration for all servers in the farm is the same
2) It’s a more advisable approach in terms of disaster recovery
 There are some good scripts that show how you can automate all the
installation and configuration of a SharePoint farm:
 AutoSPInstaller: http://autospinstaller.codeplex.com/
PowerShell Usage Scenarios
 Installation & Configuration:
Using
PowerShell
Side effects of a
Visual Installation
PowerShell Usage Scenarios
 Farm / Tenant Administration:
 A SharePoint Administrator can do more administration tasks using PowerShell
than using the SharePoint Central Administration
 For instance, the farm passphrase can only be changed by means of
PowerShell
Add-PSSnapin Microsoft.SharePoint.PowerShell
$passphrase = ConvertTo-SecureString –string “<NewPassword>” -asPlainText –Force
Set-SPPassPhrase -PassPhrase $passphrase -Confirm
PowerShell Usage Scenarios
 Farm / Tenant Administration:
 Example 2 – How to re-start all the timer service instances in a SharePoint farm:
$spFarm=Get-SPFarm
$spfTimerServcicesInstance=$spFarm.TimerService.Instances
foreach ($spfTimerServiceInstance in $spfTimerServcicesInstances)
{
Write-Host "Re-starting the instance " $spfTimerServiceInstance.TypeName
$spfTimerServiceInstance.Stop()
$spfTimerServiceInstance.Start()
Write-Host "SharePoint Timer Service Instance" $spfTimerServiceInstance.TypeName "Re-Started"
}
PowerShell Usage Scenarios
 SharePoint Migration & Upgrade scenarios:
 For SharePoint 2013 we have several PowerShell cmdlets for doing SharePoint
migration and upgrading tasks
• Content Databases level:
• Mount-SPContentDatabase
• Test-SPContentDatabase
• Upgrade-SPContentDatabase
• Site Collections level:
• Test-SPSite
• Repair-SPSite
• Upgrade-SPSite
• Request-
SPUpgradeEvaluationSiteCollection
• Farm level:
• Upgrade-SPFarm
• Queues Administration:
• Get-SPSiteUpgradeSession
• Remove-SPSiteUpgradeSession
• Service Applications level:
• New-SPBusinessDataCatalogServiceApplication
• Restore-SPEnterpriseSearchServiceApplication
• Upgrade-SPEnterpriseSearchServiceApplication
• Upgrade-
SPEnterpriseSearchServiceApplicationSiteSettin
gs
• New-SPMetadataServiceApplication
• New-SPPerformancePointServiceApplication
• New-SPProfileServiceApplication
• New-SPProjectServiceApplication
• New-New-SPSecureStoreApplication
• New-SPSubscriptionSettingsServiceApplication
PowerShell Usage Scenarios
 SharePoint Migration & Upgrade scenarios:
 Example – How to execute Test-SPContentDatabase in all the Content DBs in a
SharePoint 2010 / 2013 Farm:
$sServerInstance=“<Server_Instance>”
$spWebApps = Get-SPWebApplication -IncludeCentralAdministration
foreach($spWebApp in $spWebApps)
{
$ContentDatabases = $spWebApp.ContentDatabases
foreach($ContentDatabase in $ContentDatabases)
{
Test-SPContentDatabase –Name $ContentDatabase.Name -ServerInstance
$sServerInstance -WebApplication $spWebApp.Url
}
}
PowerShell Usage Scenarios
 Auditing SharePoint Environments:
 Through PowerShell you can do auditing tasks such as:
 Get detailed information about the logical and information architecture of a
farm: Web Applications Site Collections Sites Lists / Document Libraries …
 Get information about Content DB sizes, storage being used by Site
Collections and Sites
 Access to security information in the farm such as authentication methods in
use, SharePoint Groups and SharePoint Users defined in farm Site Collections
and Sites, Permissions levels, …
 Get all the customizations deployed and installed in the farm (.WSPs) + a list
of all the features available at different scopes
PowerShell Usage Scenarios
 SharePoint Auditing Environments:
 Example # 1 - How to get the size of all the Content DBs in a farm:
$spWebApps = Get-SPWebApplication -IncludeCentralAdministration
foreach($spWebApp in $spWebApps)
{
#$spWebApp.Name
$ContentDBs = $spWebApp.ContentDatabases
foreach($ContentDB in $ContentDBs)
{
$ContentDBsize = [Math]::Round(($ContentDB.disksizerequired/1GB),2)
$ContentDBInfo= $spWebApp.DisplayName + "," + $ContentDB.Name + ","
+ $ContentDBsize + " GB"
$ContentDBInfo
}
}
PowerShell Usage Scenarios
 SharePoint Auditing Environments:
 Example # 2 - How to extract all the WSPs deployed to a SharePoint farm:
$spFarm=Get-SPFarm
$spSolutions = $spFarm.Solutions
$iSolutionsNumber=0
foreach($spSolution in $spSolutions)
{
$spSolutionFile=$spSolution.SolutionFile
$spSolutionFile.SaveAs($ScriptDir + "" + $spSolution.DisplayName)
$iSolutionsNumber+=1
}
PowerShell Usage Scenarios
 Troubleshooting:
 Through specific cmdlets to deal with SharePoint LOGS:
PowerShell Usage Scenarios
 Troubleshooting:
 Example # 1 – Enable / Disable the Developer Dashboard:
$svc=[Microsoft.SharePoint.Administration.SPWebService]::ContentService
$ddsetting=$svc.DeveloperDashboardSettings
$ddsetting.DisplayLevel=[Microsoft.SharePoint.Administration.SPDeveloperDashboardLevel]::On
$ddsetting.Update()
PowerShell Usage Scenarios
 Solutions deployment / Artifacts provisioning:
 Install & deploy SharePoint Solutions (.WSP)
 Enable /disable SharePoint features
 Apply recursively look & feel customizations to all the sites in a Site
Collection
 Artifacts provisioning to deploy a complete SharePoint solution by creating
Sites, Content Types, Site Columns, Lists, Document Libraries, …
 Office 365 PnP Provisioning Engine is a good example of how to automate the
provisioning of SharePoint artifacts
 https://github.com/OfficeDev/PnP-Provisioning-Schema/wiki
 https://github.com/jcgonzalezmartin/jcgonzalez
PowerShell Usage Scenarios for
SharePoint
Conclusions
 PowerShell for SharePoint is a tool not only for administration and configuration
stuff but also for Auditing Troubleshooting Solutions Deployment Use of the
SharePoint API
 We can work with PowerShell for SharePoint using different development
environments:
 SharePoint Administration Console PowerShell ISE Visual Studio Windows
PowerShell PowerShell Web Acces SPO Administration Console
 We have a bunch of PowerShell cmdlets for SharePoint OnPremises and SPO
 > 800 cmdlets for SharePoint OnPremises
 > 40 cmdlets for SPO
 …and we can create our custom cmdlets
 In addition to the default cmdlets, we can use SharePoint APIs in our PowerShell
scripts…so we can do everything with PowerShell 
Q & A Barcelona
Juan Carlos González
 Office 365 MVP
 Cloud & Productivity Advisor at MVP CLUSTER
 SUGES, Comunidad de O365 & Nuberos.Net coordinator
 CompartiMOSS Co-Director (www.compartimoss.com)
 Some ways to contact me:
 Twitter: jcgm1978
 LinkedIn: https://nl.linkedin.com/in/juagon
 Contact E-Mails:
 jcgonzalezmartin1978@Hotmail.com
 juancarlos.gonzalez@fiveshareit.es
 Blog: https://jcgonzalezmartin.wordpress.com/
 MVP CLUSTER Web Site: www.mvpcluster.com

How to do everything with PowerShell

  • 1.
    How to doeverything with PowerShell Juan Carlos González September 26th, 2015 Barcelona
  • 2.
    Thanks to ourSponsors
  • 3.
    Get stamps fromall the sponsors Deposit the passport to enter the prize raffle Good luck! The SPSBCN Passport
  • 4.
    FREE BEER! Get yourticket at the registration desk Network and have fun with your colleagues! SharePint sponsored by Michael Collins Pub Plaça de Sagrada Família Starts at 19:30h
  • 5.
    They help usimprove for SharePoint Saturday 2016! Remember to fill your evaluation forms
  • 6.
    Juan Carlos González Office 365 MVP  Cloud & Productivity Advisor at MVP CLUSTER  SUGES, Comunidad de O365 & Nuberos.Net coordinator  CompartiMOSS Co-Director (www.compartimoss.com)  Some ways to contact me:  Twitter: jcgm1978  LinkedIn: https://nl.linkedin.com/in/juagon  Contact E-Mails:  [email protected][email protected]  Blog: https://jcgonzalezmartin.wordpress.com/  MVP CLUSTER Web Site: www.mvpcluster.com
  • 7.
  • 8.
    PowerShell for SharePointpossibilities  What can I do with PowerShell for SharePoint? Backup & Restore Operations PowerShell provides several ways to interact and work with SharePoint (OnPrem & Online) Platform Administration Use Server Side / Client Side APIs Troubleshooting Everything !!!
  • 9.
    PowerShell Development Environments SharePoint OnPremises – SharePoint Management Shell:  Available by default on every WFE / Application Server in a SharePoint Farm  It provides access in the box to all the SharePoint cmdlets
  • 10.
    PowerShell Development Environments SharePoint OnPremises – PowerShell Web Access:
  • 11.
    PowerShell Development Environments SharePoint OnPremises – PowerShell Web Access:  It provides a way to execute PowerShell cmdlets in the Browser  In order to use it you have to:  Enable PowerShell Web Access feature at the WFE(s) level  Install/Enable PowerShell Web Access components using PowerShell  Configure Default IIS Web Site on every SharePoint WFE(s) you want to use PowerShell Web Access Install-PswaWebApplication –UseTestCertificate Add-PswaAuthorizationRule -UserName [Dominio][Usuario] -ComputerName [NombreComputador] -ConfigurationName Microsoft.Powershell
  • 12.
    PowerShell Development Environments SharePoint OnPremises y SPO – Windows PowerShell:  For SP OnPremises, the SharePoint Snap-In has to be loaded at first
  • 13.
    PowerShell Development Environments SharePoint OnPremises y SPO – ISE:
  • 14.
    PowerShell Development Environments PowerShell ISE:  It’s “almost” a development environment for PowerShell providing features such as Debugging Intellisense Code Coloring …  Each new ISE version release adds new features and improvements  It is a feature provided by default in Windows (Client and Server Editions), although there are some cases you need to enable it (Windows Server 2008 R2)  SharePoint Snap-In must be previously loaded in the ISE in order to be able to use SharePoint OnPremises PowerShell cmdelts  This step is not required for SPO
  • 15.
    PowerShell Development Environments SharePoint OnPremises y SPO – Visual Studio:
  • 16.
    PowerShell Development Environments SharePoint OnPremises y SPO – Visual Studio:  PowerShell support in Visual Studio is provided by means of the PowerShell Tools for Visual Studio:  https://visualstudiogallery.msdn.microsoft.com/c9eb3ba8-0c59-4944-9a62- 6eee37294597  Some features provided by PowerShell Tools for Visual Studio are:  Create projects for PowerShell scripts and modules  Execute PowerShell Scripts & Commands right from Visual Studio  Edit, run and debug PowerShell scripts locally and remotely using the Visual Studio debugger  Leverage Visual Studio’s locals, watch, call stack, … for your scripts and modules 
  • 17.
    PowerShell Development Environments SPO – SharePoint Online Management Shell:  It provides a shortcut to SharePoint Online default cmdlets  Updated quite often by Microsoft (last update available: August 2015)
  • 18.
    PowerShell Development Environments forSharePoint OnPremises and SPO
  • 19.
    Default PowerShell Cmdlets Components of a PowerShell Cmdlet: We can create custom cmdlets
  • 20.
    Default PowerShell Cmdlets SharePoint OnPremises:  More tan 800 cmdlets in SharePoint 2013 SP 1 (861 cmdlets in SP 2016 IT Preview) Get-Command –PSSnapin "Microsoft.SharePoint.PowerShell"
  • 21.
    Default PowerShell Cmdlets Example # 1 - Get-SPSite  It allows to get all the Site Collections in the Farm that match the specified conditions  http://technet.microsoft.com/es-es/library/ff607950(v=office.15).aspx Get-SPSite | select url, @{Expression={$_.Usage.Storage/1MB}}
  • 22.
    Default PowerShell Cmdlets SharePoint Online:  More than 40 cmdlets available for SPO (August 2015 update) $spoCmdlets=Get-Command | where {$_.ModuleName -eq “Microsoft.Online.SharePoint.PowerShell"} $spoCmdlets.Count $spoCmdlets.Name
  • 23.
    Default PowerShell Cmdlets Example # 1 - Get-SPOSite  It allows to get all the Site Collections in a SharePoint Online tenant that match the specified conditions  https://technet.microsoft.com/es-es/library/FP161380.aspx #Ejecución en la Consola de Administración de SharePoint Online $sUserName="[email protected]" $sMessage="Introduce your SPO Credentials" $sSPOAdminCenterUrl="https://nuberosnet-admin.sharepoint.com/" $msolcred = Get-Credential -UserName $sUserName -Message $sMessage Connect-SPOService -Url $sSPOAdminCenterUrl -Credential $msolcred $spoSiteCollections=Get-SPOSite
  • 24.
    Default PowerShell cmdletsfor SharePoint OnPremises and SPO
  • 25.
    Using SharePoint APIsin PowerShell  SharePoint Server Side API:  SharePoint PowerShell Snap-In provides also access to the full server side API:  You can use SharePoint objects in the same way you do in Visual Studio  Example # 1 – How to create a SharePoint list and a add a column to the list: $spSite=Get-SPSite -Identity $sSiteUrl $spWeb=$spSite.OpenWeb() $spWeb.Lists.Add("Lista Grande","Lista Grande",100) $spFieldType = [Microsoft.SharePoint.SPFieldType]::Text $spList = $spWeb.Lists["Lista Grande"] $spList.Fields.Add(“Datos”,$spFieldType,$false) $spList.Fields["Datos"].Update() $spList.Update()
  • 26.
    Using SharePoint APIsin PowerShell  SharePoint Server Side API:  Example # 2 – How to do a CAML Query: $spSite=Get-SPSite -Identity $sSiteCollection $spwWeb=$spSite.OpenWeb() $splList = $spwWeb.Lists.TryGetList($sListName) $spqQuery = New-Object Microsoft.SharePoint.SPQuery $spqQuery.Query = " <Where> <Contains> <FieldRef Name='FileLeafRef' /> <Value Type='File'>Farm</Value> </Contains> </Where>" $spqQuery.ViewFields = "<FieldRef Name='FileLeafRef' /><FieldRef Name='Title' />" $spqQuery.ViewFieldsOnly = $true $splListItems = $splList.GetItems($spqQuery)
  • 27.
    Using SharePoint APIsin PowerShell  SharePoint Client Side API:  Client Side API (better known as CSOM) can be used in PowerShell for both SharePoint OnPremises and SPO  First step before using the API is to load the required assemblies:  Once the assemblies are loaded, simply follow CSOM rules Add-Type -Path "<CSOM_Path>Microsoft.SharePoint.Client.dll" Add-Type -Path "<CSOM_Path>Microsoft.SharePoint.Client.Runtime.dll"
  • 28.
    Using SharePoint APIsin PowerShell  SharePoint Client Side API:  Example 1 – Using CSOM for SharePoint OnPremises in PowerShell: #SharePoint Client Object Model Context $spCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteColUrl) $spCredentials = New-Object System.Net.NetworkCredential($sUserName,$sPassword,$sDomain) $spCtx.Credentials = $spCredentials #Root Web Site $spRootWebSite = $spCtx.Web #Collecction of Sites under the Root Web Site $spSites = $spRootWebSite.Webs #Loading operations $spCtx.Load($spRootWebSite) $spCtx.Load($spSites) $spCtx.ExecuteQuery()
  • 29.
    Using SharePoint APIsin PowerShell  SharePoint Client Side API:  Example 2 – Using CSOM for SPO in PowerShell: $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteColUrl) $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUsername, $sPassword) $spoCtx.Credentials = $spoCredentials #Root Web Site $spoRootWebSite = $spoCtx.Web #Collecction of Sites under the Root Web Site $spoSites = $spoRootWebSite.Webs #Loading operations $spoCtx.Load($spoRootWebSite) $spoCtx.Load($spoSites) $spoCtx.ExecuteQuery() #We need to iterate through the $spoSites Object in order to get individual sites information foreach($spoSite in $spoSites){ $spoCtx.Load($spoSite) $spoCtx.ExecuteQuery() Write-Host $spoSite.Title " - " $spoSite.Url -ForegroundColor Blue } Do you see the difference?
  • 30.
    Using SharePoint APIsfrom PowerShell
  • 31.
    PowerShell Usage Scenarios Platform Administration Thereare several PowerShell usage scenarios in SharePoint (OnPremises and Online) SharePoint Migrations Auditing Tasks (Farm / Tenant Level) Troubleshooting Solutions Deployment
  • 32.
    PowerShell Usage Scenarios Installation & Configuration:  PowerShell provides more control over the SharePoint installation & configuration process in regards to:  Installation Accounts Database Names Service Applications Configuration …  It takes some more time than the SharePoint visual installation, but you will get two great benefits: 1) The configuration for all servers in the farm is the same 2) It’s a more advisable approach in terms of disaster recovery  There are some good scripts that show how you can automate all the installation and configuration of a SharePoint farm:  AutoSPInstaller: http://autospinstaller.codeplex.com/
  • 33.
    PowerShell Usage Scenarios Installation & Configuration: Using PowerShell Side effects of a Visual Installation
  • 34.
    PowerShell Usage Scenarios Farm / Tenant Administration:  A SharePoint Administrator can do more administration tasks using PowerShell than using the SharePoint Central Administration  For instance, the farm passphrase can only be changed by means of PowerShell Add-PSSnapin Microsoft.SharePoint.PowerShell $passphrase = ConvertTo-SecureString –string “<NewPassword>” -asPlainText –Force Set-SPPassPhrase -PassPhrase $passphrase -Confirm
  • 35.
    PowerShell Usage Scenarios Farm / Tenant Administration:  Example 2 – How to re-start all the timer service instances in a SharePoint farm: $spFarm=Get-SPFarm $spfTimerServcicesInstance=$spFarm.TimerService.Instances foreach ($spfTimerServiceInstance in $spfTimerServcicesInstances) { Write-Host "Re-starting the instance " $spfTimerServiceInstance.TypeName $spfTimerServiceInstance.Stop() $spfTimerServiceInstance.Start() Write-Host "SharePoint Timer Service Instance" $spfTimerServiceInstance.TypeName "Re-Started" }
  • 36.
    PowerShell Usage Scenarios SharePoint Migration & Upgrade scenarios:  For SharePoint 2013 we have several PowerShell cmdlets for doing SharePoint migration and upgrading tasks • Content Databases level: • Mount-SPContentDatabase • Test-SPContentDatabase • Upgrade-SPContentDatabase • Site Collections level: • Test-SPSite • Repair-SPSite • Upgrade-SPSite • Request- SPUpgradeEvaluationSiteCollection • Farm level: • Upgrade-SPFarm • Queues Administration: • Get-SPSiteUpgradeSession • Remove-SPSiteUpgradeSession • Service Applications level: • New-SPBusinessDataCatalogServiceApplication • Restore-SPEnterpriseSearchServiceApplication • Upgrade-SPEnterpriseSearchServiceApplication • Upgrade- SPEnterpriseSearchServiceApplicationSiteSettin gs • New-SPMetadataServiceApplication • New-SPPerformancePointServiceApplication • New-SPProfileServiceApplication • New-SPProjectServiceApplication • New-New-SPSecureStoreApplication • New-SPSubscriptionSettingsServiceApplication
  • 37.
    PowerShell Usage Scenarios SharePoint Migration & Upgrade scenarios:  Example – How to execute Test-SPContentDatabase in all the Content DBs in a SharePoint 2010 / 2013 Farm: $sServerInstance=“<Server_Instance>” $spWebApps = Get-SPWebApplication -IncludeCentralAdministration foreach($spWebApp in $spWebApps) { $ContentDatabases = $spWebApp.ContentDatabases foreach($ContentDatabase in $ContentDatabases) { Test-SPContentDatabase –Name $ContentDatabase.Name -ServerInstance $sServerInstance -WebApplication $spWebApp.Url } }
  • 38.
    PowerShell Usage Scenarios Auditing SharePoint Environments:  Through PowerShell you can do auditing tasks such as:  Get detailed information about the logical and information architecture of a farm: Web Applications Site Collections Sites Lists / Document Libraries …  Get information about Content DB sizes, storage being used by Site Collections and Sites  Access to security information in the farm such as authentication methods in use, SharePoint Groups and SharePoint Users defined in farm Site Collections and Sites, Permissions levels, …  Get all the customizations deployed and installed in the farm (.WSPs) + a list of all the features available at different scopes
  • 39.
    PowerShell Usage Scenarios SharePoint Auditing Environments:  Example # 1 - How to get the size of all the Content DBs in a farm: $spWebApps = Get-SPWebApplication -IncludeCentralAdministration foreach($spWebApp in $spWebApps) { #$spWebApp.Name $ContentDBs = $spWebApp.ContentDatabases foreach($ContentDB in $ContentDBs) { $ContentDBsize = [Math]::Round(($ContentDB.disksizerequired/1GB),2) $ContentDBInfo= $spWebApp.DisplayName + "," + $ContentDB.Name + "," + $ContentDBsize + " GB" $ContentDBInfo } }
  • 40.
    PowerShell Usage Scenarios SharePoint Auditing Environments:  Example # 2 - How to extract all the WSPs deployed to a SharePoint farm: $spFarm=Get-SPFarm $spSolutions = $spFarm.Solutions $iSolutionsNumber=0 foreach($spSolution in $spSolutions) { $spSolutionFile=$spSolution.SolutionFile $spSolutionFile.SaveAs($ScriptDir + "" + $spSolution.DisplayName) $iSolutionsNumber+=1 }
  • 41.
    PowerShell Usage Scenarios Troubleshooting:  Through specific cmdlets to deal with SharePoint LOGS:
  • 42.
    PowerShell Usage Scenarios Troubleshooting:  Example # 1 – Enable / Disable the Developer Dashboard: $svc=[Microsoft.SharePoint.Administration.SPWebService]::ContentService $ddsetting=$svc.DeveloperDashboardSettings $ddsetting.DisplayLevel=[Microsoft.SharePoint.Administration.SPDeveloperDashboardLevel]::On $ddsetting.Update()
  • 43.
    PowerShell Usage Scenarios Solutions deployment / Artifacts provisioning:  Install & deploy SharePoint Solutions (.WSP)  Enable /disable SharePoint features  Apply recursively look & feel customizations to all the sites in a Site Collection  Artifacts provisioning to deploy a complete SharePoint solution by creating Sites, Content Types, Site Columns, Lists, Document Libraries, …  Office 365 PnP Provisioning Engine is a good example of how to automate the provisioning of SharePoint artifacts  https://github.com/OfficeDev/PnP-Provisioning-Schema/wiki  https://github.com/jcgonzalezmartin/jcgonzalez
  • 44.
  • 45.
    Conclusions  PowerShell forSharePoint is a tool not only for administration and configuration stuff but also for Auditing Troubleshooting Solutions Deployment Use of the SharePoint API  We can work with PowerShell for SharePoint using different development environments:  SharePoint Administration Console PowerShell ISE Visual Studio Windows PowerShell PowerShell Web Acces SPO Administration Console  We have a bunch of PowerShell cmdlets for SharePoint OnPremises and SPO  > 800 cmdlets for SharePoint OnPremises  > 40 cmdlets for SPO  …and we can create our custom cmdlets  In addition to the default cmdlets, we can use SharePoint APIs in our PowerShell scripts…so we can do everything with PowerShell 
  • 46.
    Q & ABarcelona
  • 47.
    Juan Carlos González Office 365 MVP  Cloud & Productivity Advisor at MVP CLUSTER  SUGES, Comunidad de O365 & Nuberos.Net coordinator  CompartiMOSS Co-Director (www.compartimoss.com)  Some ways to contact me:  Twitter: jcgm1978  LinkedIn: https://nl.linkedin.com/in/juagon  Contact E-Mails:  [email protected][email protected]  Blog: https://jcgonzalezmartin.wordpress.com/  MVP CLUSTER Web Site: www.mvpcluster.com

Editor's Notes

  • #2 Template may not be modified Twitter hashtag: #spsbe for all sessions
  • #3 So first of all, thanks to the organization and thanks to all the sponsors for making possible this fantastic event
  • #7 Here you can see some of my community activities and also some of my contact details (contact e-mails, Twitter, LinkedInd)
  • #9 As I like to say on my trainings and conferences: everything!!!