2

enter code hereI am trying to conduct an audit on all the sites in the farm as to which use JavaScript. I assume that parsing for the script HTML tag would be the best way, but I don't know of a proper, efficient way to do this.

[edit] I should have been more specific, but essentially, I need to find all the sites that use JS and also refer to jquery as well. The problem is that I did a file search for jquery.js and was shocked to find several dozen files and different versions. The idea is to consolidate it to one file/version if possible.

So, essentially, I need to find both references and log it to a file.

My problem is figuring out the proper command/syntax to parse for the specific lines of code as well as where to put it during the loops to log the URL's.

Here is the crap psuedo PS script I came up with:

function RecurseSite() {
    param([Microsoft.SharePoint.SPWeb]$SiteIdentity) # Calls SharePoint PowerShell command to identify site(s)

    Write-Output "Site: $($SiteIdentity.Url)" # Grabs site URL

    if($SiteIdentity.Webs.Count -gt 0)  # Cursed Recursion
    {
        foreach($subWeb in $SiteIdentity.Webs)
        {
            RecurseSite -SiteIdentity $subWeb
        }
    }
}

    $contentWebAppServices = (Get-SPFarm).services |
     ? {$_.typename -eq "Microsoft SharePoint Foundation Web Application"}

    foreach($webApp in $contentWebAppServices.WebApplications)
    {
        Write-Output "Web Application: $($webApp.name)"
        Out-File -filepath output.txt
        foreach($siteColl in $webApp.Sites)
        {
            Write-Output "Site Collection: $($siteColl.Url)"
            Out-File -filepath output.txt
            RecurseSite -SiteIdentity $($siteColl.RootWeb)
        }
    } 
  • 2
    all sites will use javascript, unless you mean custom javascript...? – Mike May 15 '14 at 14:15
  • To support @Mike comment, even SharePoint it self uses many javascript code and that has even increased with 2013 version of SharePoint. – Ravi Ranjan May 15 '14 at 14:43
  • This will be for custom JavaScript calls - mostly to jquery – user3620624 May 15 '14 at 15:06
2

I did a some security audits ;) Nearly all sites in SharePoint use JavaScript. I think for an audit it would be more interesting to look for custom JavaScript.

For that i would traverse through all sites and search for .js files that are not in the hive folder:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\TEMPLATE\LAYOUTS

To search for inline JavaScript i would write a powershell script that opens up all my sites and search for inline tags.

Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue

function Get-WebPage($url)
{
    [net.httpWebRequest] $request = [net.webRequest]::create($url)
    $request.Timeout = 600000
    $request.UseDefaultCredentials = $true
    #$req.Credentials = new NetworkCredential($username, $pwd, $domain); 
    [net.httpWebResponse] $response = $request.getResponse()
    $responseStream = $response.getResponseStream()
    $sr = new-object IO.StreamReader($responseStream)
    $result = $sr.ReadToEnd()
    return $result
}  

$URLs =
@("https://www.yourFirstSite.com",
  "https://www.yourSecondSite.com",
  )

$URLs | ForEach-Object {
    try{
        $content = Get-WebPage($_);
        //do your <script> tag search here
        //...
    }
    catch{
        Write-Host "An error occured: "
        Write-Host $_.Exception.ToString()
    }
}

But trust me there is much more you need to consider on an audit... Especially if you have many custom solutions deployed.

  • 1
    I like your approach. I was tryign to recursively find all the sites and then doing the <script> tag search and if it came back TRUE, then it would log it to a file. – user3620624 May 15 '14 at 14:50
  • Then vote me up :D – hupseb May 15 '14 at 14:51
  • It won't let me upvote... not enough reputation points... so lame – user3620624 May 15 '14 at 14:53
  • Now it should work. You have 15 points. – hupseb May 15 '14 at 15:01
  • 1
    Yup! I smashed that up arrow like it owes me money! – user3620624 May 15 '14 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.