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*
###########################################################################################################################

An opinion on including multiple JS files in your project

There are many ways to solve an issue. Some work better than others though their pureness may be questionable. Getting many JS scripts and modules loaded is one such puzzle. I’m not a huge fan of require.js for a number of reasons that are already clearly, and eloquently, expressed by others. It is great at what it does, just not what I really want to use.What I do like is using a tool such as YUI compressor to facilitate delivering robust production code.

The following code is something I found on http://www.laurencegellert.com/ which I have since extended and included into my own projects.

#!/bin/bash

# This script generates the following:
# Combined and minified javascript file for use in the app

echo
"Combining JavaScript into temp files."

# temp.js is the uncompressed file with everything combined
cat
./app/js/jquery.js
./app/js/file1.js
./app/js/scriptX.js
./app/js/xyz.js
> ./app/js/temp.js

echo
"Compressing Combined JavaScript."
java
-jar yuicompressor-2.4.7.jar --type js -o ./app/js/temp.js --charset utf-8 ./app/js/combined.min.js

echo
"Cleaning up temporary files."

rm
./app/js/temp.js

And yes, I do work on OSX as well as Win8.