One of the most annoying things I regularly encounter is undocumented environments. Nothing freaks me out more than this.
For instance, last week, I stumbled onto a search results page which didn’t work anymore. Seems that once upon a time, someone added a content editor webpart with some javascript on there. That javascript called a webservice and this failed.
If I didn’t have the reflex of putting that page in edit mode, I would never have guessed this. This lead me to the creation of the following… a PowerShell script which identifies all pages, forms and views for a webapplication which has:
- a CEWP with the <script> tag
- a CEWP with a contentlink that points to a textfile wich has the <script> tag
This way, I don’t have to go fish for these things if people are too damn lazy to create some documentation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
<# .SYNOPSIS Returns a list of pages for a specific webapplication which contain CEWP's with script. .DESCRIPTION Returns a list of pages for a specific webapplication which contain CEWP's with script. .NOTES File Name: Get-CEWPWithScript.ps1 Version : 1.0 .PARAMETER WebApplication Specifies the URL of the Web Application. .EXAMPLE PS > .\Get-CEWPWithScript.ps1 -WebApplication http://intranet.westeros.local Description ----------- Returns all Content Editor Web Parts which contains Javascript on the http://intranet.westeros.local webapplication #> [CmdletBinding()] param( [Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$false,HelpMessage="Specifies the URL of the Web Application.")] [string]$WebApplication ) function Get-CEWP([string]$url) { $manager = $web.GetLimitedWebPartManager($url, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared) $webParts = $manager.WebParts if ($webParts.Count -ne 0) { foreach ($webPart in $webParts) { if ($webPart.GetType() -eq [Microsoft.SharePoint.WebPartPages.ContentEditorWebPart]) { if ($webPart.ContentLink.Length -gt 0) { # Check file in ContentLink for script tags $file = $web.GetFile($webPart.ContentLink) $data = $file.OpenBinary() $encode = New-Object System.Text.ASCIIEncoding $contents = $encode.GetString($data) if ($contents.ToLower().Contains("<script>")) { Write-Output "$($web.Url)/$url (CONTENTLINK)" } break } if ($webPart.Content.InnerText.Contains("<script>")) { Write-Output "$($web.Url)/$url (HTML)" } } } } } # Load the SharePoint PowerShell snapin if needed if ((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null) { Write-Host "Loading the SharePoint PowerShell snapin..." Add-PSSnapin Microsoft.SharePoint.PowerShell } $SPWebApp = Get-SPWebApplication $WebApplication -EA SilentlyContinue if ($SPWebApp -eq $null) { Write-Error "$WebApplication is not a valid SharePoint Web application. Aborting execution!" } else { Write-Host -ForegroundColor Green "Please wait... gathering data." $sites = $SPWebApp.Sites foreach ($site in $sites) { try { $webs = $site.AllWebs foreach ($web in $webs) { try { # For publishingwebs, check all publishingpages if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web)) { $pubweb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web) $pages = $pubweb.GetPublishingPages() foreach($page in $pages) { Get-CEWP -url $page.Url } } # Libraries and lists have views and forms which can contain webparts... let's get them also $lists = $web.GetListsOfType("DocumentLibrary") | ? {$_.IsCatalog -eq $false} foreach ($list in $lists) { # Check the views $views = $list.Views foreach ($view in $views) { Get-CEWP -url $view.Url } # Check the forms $forms = $list.Forms foreach ($form in $forms) { Get-CEWP -url $form.Url } } } catch {} finally { $web.Dispose() } } } catch {} finally { $site.Dispose() } } } |