Skip to content

Instantly share code, notes, and snippets.

@joerodgers
joerodgers / Add-UserToSharePointGroup.ps1
Created October 18, 2017 15:24
Add Users to a SharePoint Group in Bulk
function Add-UserToSharePointGroup
{
   param
(
[Parameter(Mandatory=$true)][Microsoft.SharePoint.SPWeb]$Web,
[Parameter(Mandatory=$true)][string]$SharePointGroupName,
[Parameter(Mandatory=$true)][string]$UserName
)
@joerodgers
joerodgers / New-ClientContextWithRetry.ps1
Last active February 9, 2018 18:00
Create a new client context with logic to handle service throttling.
function New-ClientContextWithRetry
{
[cmdletbinding()]
param
(
[Parameter(Mandatory=$true, ParameterSetName = "CredentialComponents")]
[Parameter(Mandatory=$true, ParameterSetName = "SharePointOnlineCredential")]
[string]$ContextUrl,
@joerodgers
joerodgers / Invoke-ClientContextWithRetry.ps1
Last active February 9, 2018 18:02
Invoke-ClientContextWithRetry.ps1 - Executes a client context query, with logic to handle service throttling and other transient errors.
function Invoke-ClientContextWithRetry
{
[cmdletbinding()]
param
(
[parameter(Mandatory=$true)][Microsoft.SharePoint.Client.ClientContext]$ClientContext,
[parameter(Mandatory=$false)][int]$Delay = 10,
[parameter(Mandatory=$false)][int]$RetryAttempts = 5
)
@joerodgers
joerodgers / Import-ClientSideObjectModelAssemblies.ps1
Created October 18, 2017 18:34
Loads all of the CSOM assemblies into the current PowerShell process
function Import-ClientSideObjectModelAssemblies
{
[cmdletbinding()]
param
(
[Parameter(Mandatory=$true)][string]$AssemblyPath
)
begin
{
@joerodgers
joerodgers / Select-PowerShellScriptString.ps1
Created October 23, 2017 18:45
Search all PowerShell Scripts in a Directory (and sub-directories) for a Specific String
$pattern = "Get-SPSite"
$path = "C:\_scripts\SharePoint\OnPrem"
$files = Get-ChildItem -Path $path -Recurse -Include "*.ps1", "*.psm1"
$scanned = @()
foreach( $file in $files )
@joerodgers
joerodgers / Add-DomainGroupToSPOSite.ps1
Last active January 2, 2018 13:49
Adds Domain Group(s) and Permission Levels to SharePoint Online Sites (from CSV file)
Add-Type -Path "C:\Microsoft.SharePointOnline.CSOM.16.1.6008.1200\lib\net45\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Microsoft.SharePointOnline.CSOM.16.1.6008.1200\lib\net45\Microsoft.SharePoint.Client.Runtime.dll"
$script:TRACE_LOG_PATH = "C:\_temp\permissions_$(Get-Date -Format "MMddyyyy").csv"
$inputFile = "C:\_temp\missing-perms.csv"
$credential = Get-Credential
function Write-TraceLogEntry
@joerodgers
joerodgers / Remove-NonProvisionedSPWeb.ps1
Last active October 26, 2017 22:12
Delete all SPWeb objects that are not provisioned.
Add-PSSnapin Microsoft.SharePoint.PowerShell
# get all webs that are not read locked, not provisioned and created more than 5 minutes ago (so we don't delete anything that might be in the middle of provisioning)
$unprovisionedWebInfo = Get-SPSite -Limit All | ? { -not $_.ReadLocked } | Get-SPWeb -Limit All | ? { -not $_.Provisioned -and $_.Created.ToLocalTime().AddMinutes(5) -lt (Get-Date) } | SELECT URL, Created, WebTemplate, Provisioned, IsRootWeb
# write the data to csv for record keeping
$unprovisionedWebInfo | Export-Csv -Path "E:\UnprovisionedWebsToDelete.csv" -NoTypeInformation
foreach( $webInfo in $unprovisionedWebInfo )
{
@joerodgers
joerodgers / Upgrade-SearchAnalyticsDatabase.ps1
Created October 26, 2017 20:12
Upgrade SharePoint 2013 Search Analytics Database to SharePoint 2016 Farm
$sa = Get-SPEnterpriseSearchServiceApplication
# show all the scaled out databases
Get-SPServerScaleOutDatabase -ServiceApplication $sa | FT Name, NormalizedDataSource -AutoSize
# add the 2013 analytics database to the 2016 farm
Add-SPServerScaleOutDatabase -ServiceApplication $sa -DatabaseServer "SQL01\SHAREPOINT" -DatabaseName "SP2013_SERVICE_SEARCH_ADMIN_AnalyticsReportingStore"
# get all the scaled out databases
@joerodgers
joerodgers / Test-ExcelServices.ps1
Last active November 1, 2017 21:53
This script can be used a synthetic read-only transaction against an Excel workbook in a document library.
function Test-ExcelServices
{
[cmdletbinding()]
param
(
[parameter(Mandatory=$true)][System.Uri]$Uri,
[parameter(Mandatory=$false)][System.Management.Automation.PSCredential]$Credential, # if you don't want to connect as the current user
[parameter(Mandatory=$false)][int]$CacheThreshold = 5 # minutes
)
@joerodgers
joerodgers / Repair-ConfigCache.ps1
Last active May 2, 2018 17:58
Script will clear and rebuild the config cache on servers in a SharePoint farm.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$serversNames = Get-SPServer | ? { $_.Role -ne "Invalid" } | % { $_.Address }
function Test-RunAsAdministrator
{
<#
.Synopsis
Returns True if the shell is running as an administrator, otherwise false.