Generate a SharePoint upgrade report with PowerShell and Test-SPContentDatabase

When running Test-SPContentDatabase to check for upgrade issues before mounting a content database to a web application the output from this command can sometimes be a little overwhelming if there are lots of issues found:

image

 

…row after row of issues. Finding the important (blocking issues) in this list can sometimes be a time consuming process.

To help analyse the test results automatically the I’ve developed the PowerShell script (included below). This script parses the test results and shows a summary and highlights (or are they lowlights?) of the test results before optionally showing you the full results. I’ve found this speeds up my repetitive testing of content databases significantly and almost makes the process of finding, tracing and fixing deployment issues a pleasure!

Test Results Summary:

image

PS C:\> .\test_db.ps1 moss_content http://bc01:2007

Testing moss_content against http://bc01:2007…Done

Analysing results…Done

Test results for moss_content against http://bc01:2007

Upgrade Blocking Issues: 0
Upgrade non-Blocking Issues: 40

Issue Summary
————-
SiteOrphan : 1
MissingFeature : 12
MissingSetupFile : 11
MissingAssembly : 1
MissingWebPart : 15

Issue Details – SiteOrphan
————————-
Database [moss_content] contains a site (Id = [37b206a2-16f8-42b2-b307-e2a6be895fa2], Url = [/]) whose url is already u
sed by a different site, in database (Id = [c6afed7f-a591-4f17-b0e1-90fb2be7a6f6], name = [dummy_upgrade]), in the same
web application. Consider deleting one of the sites which have conflicting urls.

Issue Details – MissingFeature
————————-
Database [moss_content] has reference(s) to a missing feature: Id = [1d761b30-7eed-11db-9fe1-0800200c9a67].
Database [moss_content] has reference(s) to a missing feature: Id = [99bf9746-84d2-4672-aff3-1d677d714910].
…etc, etc

Would you like to see the full details?
[Y] yes  [N] no  [?] Help (default is "N"):

Answering ‘Yes’ the question above will display the full test results as per the Test-SPContentDatabase command.

The PowerShell script to create this report is below, simply save the PowerShell script to a file and execute it:

param ([String]$dbName, [String]$webApp)

## Load the SharePoint Snapin so the script can be executed from PowerShell editor
Add-PSSnapin Microsoft.SharePoint.PowerShell –erroraction SilentlyContinue

# get the database name if not specified
if ($dbName -eq "")
{
    Write-Host
    $dbName = Read-Host -Prompt " – Please enter the name of the database to be tested"
}
# get the web application name if not specified
if ($webApp -eq "")
{
    Write-Host
    $webApp = Read-Host -Prompt " – Please enter the url of the web application to be tested"
}
Write-Host

# test the database
Write-Host "Testing database $dbName against web application $webApp…" -nonewline
$results = Test-SPContentDatabase -name $dbName -WebApplication $webApp
Write-Host "Done" -Foreground Green
Write-Host

# analyse the results
Write-Host "Analysing results…" -nonewline
$listCategory = @{"Dummy"=0}; $listCategory.Clear()
$listBlocking = @{[string]$true=0;[string]$flase=0}
foreach ($result in $results)
{   
    #count the distinct categories
    if($listCategory.ContainsKey($result.Category))
    {
        #increment the counter
        $listCategory.Set_Item($result.Category,($listCategory.Get_Item($result.Category) +1))
    }
    else
    {
        #add the counter
        $listCategory.Add($result.Category,1)
    }
   
    #count the distinct blockings
    if($listBlocking.ContainsKey([string]$result.UpgradeBlocking ))
    {
        #increment the counter
        $listBlocking.Set_Item([string]$result.UpgradeBlocking ,($listBlocking.Get_Item([string]$result.UpgradeBlocking ) +1))
    }
    else
    {
        #add the counter
        $listBlocking.Add([string]$result.UpgradeBlocking ,1)
    }
}
Write-Host "Done" -Foreground Green
Write-Host

# now write the results
Write-Host "Test results for " -nonewline ; Write-Host $dbName -nonewline -foreground Yellow; write-host " against " -nonewline; write-host $webApp -foreground Yellow
Write-Host
Write-Host "Upgrade Blocking Issues: " -nonewline
if ($listBlocking.Get_Item([string]$true) -gt 0)
{
    Write-Host $listBlocking.Get_Item([string]$true) -foreground Red
}
else
{
    Write-Host $listBlocking.Get_Item([string]$true) -foreground Green
}
Write-Host "Upgrade non-Blocking Issues: " -nonewline
if ($listBlocking.Get_Item([string]$false) -gt 0)
{
    Write-Host $listBlocking.Get_Item([string]$false) -foreground Yellow
}
else
{
    Write-Host $listBlocking.Get_Item([string]$false) -foreground Green
}

# issue summary
Write-Host
write-host "Issue Summary"
write-host "————-"
foreach ($category in $listCategory.Keys)
{
    $v = $listCategory.Get_Item($category)
    write-host $category ": " -nonewline ; write-host $v -foreground Yellow
}

# issue details
foreach ($category in $listCategory.Keys)
{
    Write-Host
    write-host "Issue Details – $category"
    write-host "————————-"
    foreach ($result in $results)
    {   
        #count the distinct categories
        if($category -eq $result.Category)
        {
            $t = $result.Message
            $t
        }
    }
}

# shall we show full details
Write-Host
$yes = ([System.Management.Automation.Host.ChoiceDescription]"&yes")
$no = ([System.Management.Automation.Host.ChoiceDescription]"&no")
$selection = [System.Management.Automation.Host.ChoiceDescription[]] `
  ($yes,$no)
$answer = $host.ui.PromptForChoice(”,
  ‘Would you like to see the full details?’,$selection,1)

Write-Host
if ($answer -eq 0)
{
   $results

… enjoy

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: