<# SCRIPT 23 –, step-by-step, production-ready SCRIPT 23 – SHAREPOINT CU INSTALL SCRIPT Purpose: - Install SharePoint CU across a server - Apply patch safely using controlled sequence - Execute PSConfig to complete upgrade IMPORTANT: - RUN ON EACH SERVER INDIVIDUALLY - RUN AS ADMIN IN SHAREPOINT MANAGEMENT SHELL - EXPECT SERVICE INTERRUPTION #> [CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$CULocation, [Parameter(Mandatory = $true)] [string]$LogPath, [switch]$SkipPrompt ) Set-StrictMode -Version Latest $ErrorActionPreference = "Stop" Write-Host "SCRIPT 23 - SHAREPOINT CU INSTALL" -ForegroundColor Cyan Write-Host "THIS SCRIPT INSTALLS SHAREPOINT PATCHES" -ForegroundColor Yellow # -------------------------------------------------- # STEP 1 – VALIDATION # -------------------------------------------------- if (-not (Test-Path $CULocation)) { throw "CU location not found: $CULocation" } if (-not (Test-Path $LogPath)) { New-Item -Path $LogPath -ItemType Directory -Force | Out-Null } if (-not $SkipPrompt) { $resp = Read-Host "Type YES to proceed with CU install" if ($resp -ne "YES") { Write-Host "Cancelled by user" -ForegroundColor Yellow return } } # -------------------------------------------------- # STEP 2 – LOAD SHAREPOINT SNAP-IN # -------------------------------------------------- try { if (-not (Get-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue)) { Add-PSSnapin Microsoft.SharePoint.PowerShell } } catch { throw "Failed to load SharePoint snap-in" } # -------------------------------------------------- # STEP 3 – STOP SHAREPOINT SERVICES # -------------------------------------------------- function Stop-SPServices { Write-Host "Stopping SharePoint services..." -ForegroundColor Yellow $services = @( "SPTimerV4", # Timer Service "SPAdminV4", # Admin Service "SPTraceV4", # Logging "W3SVC", # IIS "OSearch16", # Search "SPSearchHostController", "SPCache" # Distributed Cache ) foreach ($svc in $services) { $service = Get-Service -Name $svc -ErrorAction SilentlyContinue if ($service -and $service.Status -ne "Stopped") { Stop-Service $svc -Force Write-Host "Stopped: $svc" } } } # -------------------------------------------------- # STEP 4 – INSTALL CU # -------------------------------------------------- function Install-CU { Write-Host "Installing CU from: $CULocation" -ForegroundColor Yellow $installers = Get-ChildItem $CULocation -Filter *.exe if ($installers.Count -eq 0) { throw "No CU installers found" } foreach ($exe in $installers) { Write-Host "Running: $($exe.Name)" Start-Process -FilePath $exe.FullName -ArgumentList "/quiet /norestart" -Wait } } # -------------------------------------------------- # STEP 5 – RUN PSCONFIG # -------------------------------------------------- function Run-PSConfig { Write-Host "Running SharePoint configuration update..." -ForegroundColor Yellow $psconfig = "$env:COMMONPROGRAMFILES\Microsoft Shared\Web Server Extensions\16\BIN\psconfig.exe" if (-not (Test-Path $psconfig)) { throw "PSConfig not found" } Start-Process -FilePath $psconfig ` -ArgumentList "-cmd upgrade -inplace b2b -wait -force -cmd applicationcontent -install -cmd installfeatures" ` -Wait } # -------------------------------------------------- # STEP 6 – START SERVICES # -------------------------------------------------- function Start-SPServices { Write-Host "Starting SharePoint services..." -ForegroundColor Yellow $services = @( "SPTimerV4", "SPAdminV4", "SPTraceV4", "W3SVC", "OSearch16", "SPSearchHostController", "SPCache" ) foreach ($svc in $services) { $service = Get-Service -Name $svc -ErrorAction SilentlyContinue if ($service -and $service.Status -ne "Running") { Start-Service $svc Write-Host "Started: $svc" } } } # -------------------------------------------------- # EXECUTION FLOW # -------------------------------------------------- try { Stop-SPServices Install-CU Run-PSConfig Start-SPServices Write-Host "CU INSTALL COMPLETED SUCCESSFULLY" -ForegroundColor Green } catch { Write-Host "INSTALL FAILED: $($_.Exception.Message)" -ForegroundColor Red throw }