Searching the source of ALL pages in a SharePoint farm using PowerShell

Recently I had to find all instances of a jQuery specific reference in a small to mid sized farm. I had not had to do this before and after a few minutes I decided that PowerShell was the tool to use. Here’s what I cobbled together. It’s neither great nor finished but it is probably useful for somebody right now.

###########################################################################################################################
# BEGIN: Get all pages ASPX & HTM*
function TrawlInventory($Url) {
    $farm = [Microsoft.SharePoint.Administration.SPFarm]::Local;
    $site = new-object Microsoft.SharePoint.SPSite $Url;

    foreach ($web in $site.AllWebs) {
        $webUrl = $web.Url;
        write-host “- Checking: $webUrl”;

        foreach ($list in $web.Lists) {
            $listitemsCount = $list.Items.Count;
            write-host “- – Checking: $list which has $listitemsCount items”;

            if ($listitemsCount -lt 1000) {
                foreach ($item in $list.Items) {
                    $itemName = $item.name.ToString().ToLower();
                    write-host “- – – – Checking file: $itemName”;

                    if ($itemName -like ‘*aspx’ -Or $itemName -like ‘*htm’) {
                        $thisURL = $item.Url;
                        $thisWEB = $web.Url;
                        if ($thisURL){
                            $thisItemsFullURL = “$thisWEB/$thisURL”;
                            $itemsFullURL = $thisItemsFullURL.ToString();
                            $uri = “$itemsFullURL”;
                            write-host “- – – – – Checking source for: $uri”;
                            $wc = New-Object System.Net.WebClient;
                            $wc.UseDefaultCredentials = $true;
                            $json = $wc.DownloadString($uri);
                            $jsonLC = $json.ToLower();
                            if ($jsonLC.Contains(“string1“) -Or $jsonLC.Contains(“string2”)) {
                                write-host “Found reference: $itemsFullURL”;
                                Add-Content -Path “curls.txt” -Value “$itemsFullURL”;
                            }
                            $json = “”;
                            $jsonLC = “”;
                        }
                    }
    $itemName = “”;

                }

            } else {
                write-host “Too many itemns to check: $listitemsCount”;            
            }

        }
        $web.Dispose();                
    }
}
###########################################################################################################################
$thisDomain = “http://WebAppName/site-collection/”;
TrawlInventory($thisDomain);
# END: Get all pages ASPX & HTM*
###########################################################################################################################