SharePoint / Guide 2
SharePoint Server (on-premises) tips
Farm administration from SharePoint 2010 to Subscription Edition: ULS, patching, recovery, performance.
These apply to SharePoint 2010 through Subscription Edition unless noted.
1. Dispose of SPSite and SPWeb objects
SPSite and SPWeb hold unmanaged memory. In a long loop over thousands of webs, forgetting to dispose will eat the shell's memory and can slow the server.
foreach ($site in Get-SPSite -Limit All) {
try {
foreach ($web in $site.AllWebs) {
try { <# work with $web #> }
finally { $web.Dispose() }
}
}
finally { $site.Dispose() }
}
For quick one-liners, wrap the work in Start-SPAssignment -Global / Stop-SPAssignment -Global.
2. Correlation IDs are your best friend
Users send a screenshot with a correlation ID. Instead of opening ULS files on every server, merge the entries from the whole farm:
Merge-SPLogFile -Path C:\Temp\issue.log -Correlation 'a1b2c3d4-...' -StartTime (Get-Date).AddHours(-2) -Overwrite
See Get-SPLogsByCorrelation.ps1.
3. Translate your old STSADM habits
| STSADM | PowerShell |
|---|---|
stsadm -o backup -url |
Backup-SPSite |
stsadm -o restore |
Restore-SPSite |
stsadm -o enumsites |
Get-SPSite -Limit All |
stsadm -o addsolution |
Add-SPSolution |
stsadm -o deploysolution |
Install-SPSolution |
stsadm -o addcontentdb |
Mount-SPContentDatabase |
stsadm -o execadmsvcjobs |
Start-SPAdminJob |
stsadm -o preupgradecheck |
Test-SPContentDatabase |
4. Plan for the list view threshold before users hit it
The default threshold is 5,000 items per query. Don't wait for the error: find lists above ~4,000 items and index the columns people filter and sort on.
.\Get-SPLargeLists.ps1 -Threshold 4000
Adding an index to a list that is already over the threshold may itself be blocked outside the daily time window, which is another reason to act early.
5. Recover a deleted site collection without a restore
Since SharePoint 2010 SP1, deleted site collections go to a farm recycle bin first:
Get-SPDeletedSite | Select-Object Path, DeletionTime, SiteId
Restore-SPDeletedSite -Identity <SiteId>
6. Back up a single site safely
Backup-SPSite -Identity https://sharepoint.ravindran.in/sites/HR -Path D:\Backups\HR.bak -UseSqlSnapshot
-UseSqlSnapshot avoids locking the site during backup (needs a SQL edition that supports database snapshots). Without it, consider Set-SPSite -LockState ReadOnly during the backup so content doesn't change mid-way.
7. Patching: check before and after
(Get-SPFarm).BuildVersion
Get-SPProduct -Local
Get-SPContentDatabase | Where-Object NeedsUpgrade | Select-Object Name
After installing a CU on every server, run the configuration wizard from the command line, one server at a time:
PSConfig.exe -cmd upgrade -inplace b2b -wait -cmd applicationcontent -install -cmd installfeatures -cmd secureresources -cmd services -install
8. Move a big site collection to its own content database
New-SPContentDatabase -Name WSS_Content_Projects -WebApplication https://sharepoint.ravindran.in
Move-SPSite https://sharepoint.ravindran.in/sites/Projects -DestinationDatabase WSS_Content_Projects
iisreset # on each web server when finished
Keep content databases in the ~200 GB range for general collaboration sites where you can. Backup, restore and upgrade times are much easier to manage.
9. Find failed timer jobs quickly
Get-SPTimerJob | ForEach-Object {
$job = $_
$job.HistoryEntries | Where-Object Status -eq 'Failed' |
Select-Object -First 1 @{n='Job';e={$job.DisplayName}}, StartTime, ErrorMessage
} | Sort-Object StartTime -Descending
10. Clearing the configuration cache (the classic fix)
When timer jobs stall or servers disagree about farm state, clearing the config cache still works. On every server: stop the SharePoint Timer Service, open C:\ProgramData\Microsoft\SharePoint\Config\<GUID>, delete all .xml files (not cache.ini), set the contents of cache.ini to 1, then start the Timer Service again. Do it on all servers before starting the timers back up.
11. Lock sites during a migration cutover
Set-SPSite -Identity https://sharepoint.ravindran.in/sites/Finance -LockState ReadOnly
# Values: Unlock, NoAdditions, ReadOnly, NoAccess
12. Clean up what Test-SPContentDatabase finds before an upgrade
Missing features, web parts and setup files from old custom solutions are the most common upgrade headaches. Run Test-SPUpgradeReadiness.ps1 against every database and deal with anything flagged UpgradeBlocking first.
13. Know your farm before you touch it
On a new engagement, the first thing I run is an inventory. Get-SPFarmInventory.ps1 gives you servers, web apps, databases, service apps and solutions in a few minutes, all read-only.