Powershell – SharePoint Deployment Standard Init Call


In a typical SharePoint deployment script, I would have the following part to make sure that all required ammunitions are there:

# Load SharePoint PS snap-in. # Nothing will occur if the Add-Type cmdlet tries to load an assembly multiple times, because .NET Framework assemblies only load once. Add-Type -AssemblyName "Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Function WriteLine { Write-Host -ForegroundColor White "--------------------------------------------------------------" } Function Confirm-LocalSession { If ($Host.Name -eq "ServerRemoteHost") {Return $false} Else {Return $true} } Function Load-SharePoint-PowerShell { If ((Get-PsSnapin |?{$_.Name -eq "Microsoft.SharePoint.PowerShell"})-eq $null) { WriteLine Write-Host -ForegroundColor White " - Loading SharePoint PowerShell Snapin..." # Added the line below to match what the SharePoint.ps1 file implements (normally called via the SharePoint Management Shell Start Menu shortcut) If (Confirm-LocalSession) {$Host.Runspace.ThreadOptions = "ReuseThread"} Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction Stop | Out-Null WriteLine } } Write-Host -ForegroundColor White " - Re-importing SP PowerShell Snapin to enable new cmdlets..." Remove-PSSnapin Microsoft.SharePoint.PowerShell Load-SharePoint-PowerShell