<# .SYNOPSIS Script 12 - Feature Inventory Report .DESCRIPTION READ-ONLY. WebAppUrlScans SharePoint feature activations across: Target SharePoint Web Application URL .PARAMETER OutputCsv Full path to output CSV .PARAMETER SiteCollectionUrl Optional - limit scan to one site collection .PARAMETER NoPrompt Skip confirmation prompt #> [CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$WebAppUrl, [Parameter(Mandatory = $true)] [string]$OutputCsv, [string]$SiteCollectionUrl, [switch]$NoPrompt ) Set-StrictMode -Version Latest $ErrorActionPreference = "Stop" Write-Host "" Write-Host "SCRIPT 12 - FEATURE INVENTORY REPORT" -ForegroundColor Cyan Write-Host "READ-ONLY. NO CHANGES ARE MADE." -ForegroundColor Green Write-Host "" # ------------------------------------------------------------ # Load SharePoint Snap-in # ------------------------------------------------------------ try { if (-not (Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue)) { Add-PSSnapin "Microsoft.SharePoint.PowerShell" } } catch { throw "Run in SharePoint Management Shell. Error: $($_.Exception.Message)" } # ------------------------------------------------------------ # Output Setup # ------------------------------------------------------------ $outDir = Split-Path -Path $OutputCsv -Parent if ([string]::IsNullOrWhiteSpace($outDir)) { throw "OutputCsv must be full path (e.g., C:\Temp\FeatureInventory.csv)" } if (-not (Test-Path $outDir)) { New-Item -Path $outDir -ItemType Directory -Force | Out-Null } $timestamp = (Get-Date).ToString("yyyyMMdd_HHmmss") $baseName = [System.IO.Path]::GetFileNameWithoutExtension($OutputCsv) $summaryPath = Join-Path $outDir "$baseName`_$timestamp`_Summary.csv" $logPath = Join-Path $outDir "$baseName`_$timestamp`_RunLog.txt" $errorPath = Join-Path $outDir "$baseName`_$timestamp`_Errors.csv" # ------------------------------------------------------------ # Logging Helpers # ------------------------------------------------------------ $errors = New-Object System.Collections.Generic.List[object] $log = New-Object System.Collections.Generic.List[string] function Log { param($msg) $line = "[{0}] {1}" -f (Get-Date -Format "yyyy-MM-dd HH:mm:ss"), $msg $log.Add($line) | Out-Null Write-Host $line } function Add-Error { param($scope, $msg) $errors.Add([pscustomobject]@{ Timestamp = Get-Date Scope = $scope Message = $msg }) | Out-Null } # ------------------------------------------------------------ # Prompt # ------------------------------------------------------------ if (-not $NoPrompt) { Write-Host "This script scans feature activations only." -ForegroundColor Yellow if ((Read-Host "Type YES to continue") -ne "YES") { return } } # ------------------------------------------------------------ # Resolve Sites # ------------------------------------------------------------ function Get-Sites { if ($SiteCollectionUrl) { return @(Get-SPSite -Identity $SiteCollectionUrl) } return @(Get-SPSite -WebApplication $WebAppUrl -Limit All) } # ------------------------------------------------------------ # Risk Model # ------------------------------------------------------------ function Get-RiskLevel { param($FeatureId, $DisplayName) if ($DisplayName -match "Workflow|Publishing|Custom") { return "High" } if ($DisplayName -match "Search|Metadata") { return "Medium" } return "Low" } function Get-Score { param($risk) switch ($risk) { "High" { return 30 } "Medium" { return 60 } "Low" { return 90 } default { return 50 } } } function Get-Recommendation { param($risk, $DisplayName) if ($risk -eq "High") { return "Validate feature availability in target farm. Rebuild or replace before migration." } if ($risk -eq "Medium") { return "Validate compatibility and confirm feature usage." } return "Low-impact feature. Include in baseline validation." } # ------------------------------------------------------------ # Main Execution # ------------------------------------------------------------ $results = New-Object System.Collections.Generic.List[object] try { $sites = Get-Sites Log ("Resolved {0} site collections" -f $sites.Count) } catch { Add-Error $WebAppUrl $_.Exception.Message throw } foreach ($site in $sites) { try { Log ("Scanning site collection: {0}" -f $site.Url) # --- SITE FEATURES --- foreach ($feature in $site.Features) { try { $displayName = "" try { $displayName = $feature.Definition.DisplayName } catch { } $risk = Get-RiskLevel -FeatureId $feature.DefinitionId -DisplayName $displayName $results.Add([pscustomobject]@{ ScopeType = "SiteCollection" SiteCollectionUrl = $site.Url WebUrl = "" FeatureId = $feature.DefinitionId FeatureName = $displayName Version = $feature.Version RiskLevel = $risk Score = Get-Score $risk Category = "FeatureInventory" ActionRecommendation = Get-Recommendation -risk $risk -DisplayName $displayName }) | Out-Null } catch { Add-Error $site.Url $_.Exception.Message } } # --- WEB FEATURES --- foreach ($web in $site.AllWebs) { try { foreach ($feature in $web.Features) { try { $displayName = "" try { $displayName = $feature.Definition.DisplayName } catch { } $risk = Get-RiskLevel -FeatureId $feature.DefinitionId -DisplayName $displayName $results.Add([pscustomobject]@{ ScopeType = "Web" SiteCollectionUrl = $site.Url WebUrl = $web.Url FeatureId = $feature.DefinitionId FeatureName = $displayName Version = $feature.Version RiskLevel = $risk Score = Get-Score $risk Category = "FeatureInventory" ActionRecommendation = Get-Recommendation -risk $risk -DisplayName $displayName }) | Out-Null } catch { Add-Error $web.Url $_.Exception.Message } } } finally { try { $web.Dispose() } catch {} } } $site.Dispose() } catch { Add-Error $site.Url $_.Exception.Message } } # ------------------------------------------------------------ # Export Reports # ------------------------------------------------------------ $results | Export-Csv -Path $OutputCsv -NoTypeInformation -Encoding UTF8 $results | Group-Object RiskLevel | ForEach-Object { [pscustomobject]@{ RiskLevel = $_.Name Count = $_.Count } } | Export-Csv -Path $summaryPath -NoTypeInformation -Encoding UTF8 $log | Set-Content $logPath if ($errors.Count -gt 0) { $errors | Export-Csv -Path $errorPath -NoTypeInformation -Encoding UTF8 Write-Host "ERROR REPORT: $errorPath" -ForegroundColor Yellow } Write-Host "" Write-Host "DETAIL REPORT: $OutputCsv" -ForegroundColor Green Write-Host "SUMMARY REPORT: $summaryPath" -ForegroundColor Green Write-Host "RUN LOG: $logPath" -ForegroundColor Green Write-Host "" Write-Host "Complete." -ForegroundColor Green - Site collections - Webs Used for: - Migration planning - Test-SPContentDatabase remediation - Feature dependency analysis