OK, so we have (let’s say) 300+ custom solutions on our SPS2010 environment and want to create a SPDisposeCheck report against them. The report should then be presented in the form of a CSV file.
So here goes:
- First download SPDisposeCheck for SharePoint 2010 (note: it’s only for SPS2010, as it does not check .NET 4 assemblies) from here: https://gallery.technet.microsoft.com/office/SharePoint-Dispose-Checker-01da48e8
- Extract all the farm’s WSPs using the PowerShell script below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Add-PSSnapin Microsoft.SharePoint.PowerShell foreach($solution in Get-SPSolution) { try { $filename = $solution.Name; $solution.SolutionFile.SaveAs("C:\MySolutions\$filename") } catch { Write-Host "-error:$_"”-foreground red } } |
3. As WSPs are basically CAB archives, we would need to extract the contents of the WSPs in order to get the DLLs contained within them:
|
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 |
clear Write-Host "**************************************************************************************************" Write-Host "| This script will unpack the WSP packages that it finds in a provided directory |" Write-Host "**************************************************************************************************" Write-Host cls $wspdir = Read-Host "Provide directory where all the WSPs are located" $exportdir = "$wspdir\Export-$((get-date).toString('yyyyMMdd-hhmmss'))\" #Retrieve the wsp files in this folder and subfolders $s = [system.IO.SearchOption]::AllDirectories $fileEntries = [IO.Directory]::GetFiles($wspdir,"*.wsp",$s); foreach($fullFileName in $fileEntries) { $fileName = $(Get-Item $fullFileName).Name; $dirName = $fileName.Replace(".wsp",""); $path = $exportdir + $dirName; $dir = [IO.Directory]::CreateDirectory($path) #uncab Write-Host "Export $fileName started" -ForegroundColor Yellow $destination = $path C:\Windows\System32\extrac32.exe $fullFileName /e /Y /L $destination } |
4. Then have all those DLLs checked with SPDisposeCheck:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
clear Write-Host "**************************************************************************************************" Write-Host "| This script will run the SPDisposeCheck tool on each dll that it finds in a provided directory |" Write-Host "**************************************************************************************************" Write-Host $path = Read-Host "Directory path with the WSP assemblies to check are located" $Dir = get-childitem $path -recurse $List = $Dir | where {$_.extension -eq ".dll"} $export = $path+"\"+"SPDisposeCheck_$((get-date).toString('yyyyMMdd-hhmmss'))" $dir = md $export $List | ForEach-Object { Write-Host $_.name "is beeing checked..." $report = $export+"\"+$_.name+".spdisposecheck.txt" & "C:\Program Files (x86)\Microsoft\SharePoint Dispose Check\SPDisposeCheck.exe" $_.fullname | Out-File $report } Write-Host Write-Host "The reports are stored in "$export Write-Host |
The above script will create multiple reports (one for each verified DLL).
5. Now to create the CSV report based on the multiple report files:
|
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 |
clear Write-Host "********************************************************************************************************************" Write-Host "| This script will collect all the SPDisposeCheck result files and create one csv file with them for reporting |" Write-Host "********************************************************************************************************************" Write-Host cls $wspdir = Read-Host "Provide the directory where all the result files are located" #Retrieve the wsp files in this folder $fileEntries = [IO.Directory]::GetFiles($wspdir,"*.txt"); $counter = 0; $coll = @(); foreach($fileName in $fileEntries) { $content = Get-Content $fileName; $obj = new-object PSObject foreach($line in $content){ #check if this line starts with ID: SPDisposeCheckID_ if($line.StartsWith("ID: SPDisposeCheckID_")){ $ID = $line.Replace("ID: SPDisposeCheckID_",""); if($ID -eq 140){ continue #false positive } $obj = new-object PSObject $obj | add-member -membertype NoteProperty -name "Path" -value $fileName $obj | add-member -membertype NoteProperty -name "SPDisposeCheckID" -value $ID -Force $counter = 0; $coll += $obj; } #Method information if($counter -eq 2){ $line = $line.Remove(0,8) #remove Method: from string $obj | add-member -membertype NoteProperty -name "Method" -value $line -Force } #Statement information if($counter -eq 3){ $line = $line.Remove(0,11) #remove Statement: from string $obj | add-member -membertype NoteProperty -name "Statement" -value $line -Force } #Notes information if($counter -eq 4){ $line = $line.Remove(0,7) #remove Notes: from string $obj | add-member -membertype NoteProperty -name "Notes" -value $line -Force } #More information if($counter -eq 4){ $line = $line.Remove(0,18) #remove More information: from string $obj | add-member -membertype NoteProperty -name "More information" -value $line -Force } $counter++ } $coll | export-csv -Delimiter ';' "$wspdir\spdisposecheckrapport.csv" -notypeinformation } |
