What is health analyzer? This is the copy/pasta from msdns Overview of SharePoint Health Analyzer “SharePoint Health Analyzer is a feature in Microsoft SharePoint Foundation 2010 that enables administrators to schedule regular, automatic checks for potential configuration, performance, and usage problems in the server farm.”
So what we need to create a health analyzer rule?
- Create a subclass of the SPHealthAnalysisRule or SPRepairableHealthAnalysisRule
- Register the rule
- Test/execute the rule
1) Create a subclass
In this demo I create a subclass of the SPRepairableHealthAnalysisRule and besides for implement the abstract properties from SPRepairableHealthAnalysisRule, I also override Check and Repair method.
The Check method is used to identify a single potential problem in the SharePoint environment. In this demo we are a test if a very important file exists on the c: drive.
public override SPHealthCheckStatus Check()
{
FileInfo info
= new FileInfo
(@"c:\importantfile.txt"); return info.Exists ? SPHealthCheckStatus.Passed : SPHealthCheckStatus.Failed;
}
Repair method is used to repair the problem that if the Check method return SPHealthCheckStatus.Failed. In the demo we just create the very important file on the c drive.
public override SPHealthRepairStatus Repair()
{
try
{
if (Check().Equals(SPHealthCheckStatus.Failed))
{
FileInfo info
= new FileInfo
(@"c:\importantfile.txt"); info.Create();
return SPHealthRepairStatus.Succeeded;
}
else
{
return SPHealthRepairStatus.Succeeded;
}
}
catch
{
//TODO: Log the error
return SPHealthRepairStatus.Failed;
}
}
2) Register the rule
Microsoft has a “How to: Create a Feature to Register a Health Rule” where they use a feature receiver to register rule with. But I’m big PowerShell fan, so it was naturally for me to create a simple PowerShell script to register and unregister.
Register PowerShell:
First we load the SharePoint DLL and my custom DLL. Then we call the static RegisterRules method with take the custom assembly as parameter. The RegisterRules return an IDictionary<Type, Exception>. We test if the IDictionary<Type, Exception> count is 0, if not, we display the exceptions by pipe the collection to the Format-List cmdlet.
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | Out-Null
$assembly = [System.Reflection.Assembly]::LoadWithPartialName("HealthAnalyzerDemo")
$exceptions = [Microsoft.SharePoint.Administration.Health.SPHealthAnalyzer]::RegisterRules($assembly);
if($exceptions -ne $null)
{
if($exceptions.Count -eq 0)
{
Write-Host "All rules were registered.";
}
else
{
$exceptions | fl
}
}
$assembly = $null
Unregister PowerShell:
The only difference between the register script and the unregister script is, instead of call the RegisterRules method we call the UnRegisterRules.
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | Out-Null
$assembly = [System.Reflection.Assembly]::LoadWithPartialName("HealthAnalyzerDemo")
$exceptions = [Microsoft.SharePoint.Administration.Health.SPHealthAnalyzer]::UnregisterRules($assembly);
if($exceptions -ne $null)
{
if($exceptions.Count -eq 0)
{
Write-Host "All rules were unregistered.";
}
else
{
$exceptions | fl
}
}
$assembly = $null
3) Test/execute the rule
Microsoft has a “How to: Test a Health Rule During Development” where they use a console application to test/execute the health rule. But this is so obviously a PowerShell thing :)
Execute rule PowerShell:
First we load the SharePoint DLL and my custom DLL. Then we create a new object of the rule class. Write the current return status from the Check method. Test if the status is equal to failed, if it is, we call the Repair method.
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("HealthAnalyzerDemo") | Out-Null
$checkIfFileOnDiskExistRuleRepair = New-Object "HealthAnalyzerDemo.HealthAnalyzers.CheckIfFileOnDiskExistRuleRepair"
$status = $checkIfFileOnDiskExistRuleRepair.Check()
Write-Host "Status for"$checkIfFileOnDiskExistRuleRepair.Summary"is:"$status
if($status -eq [Microsoft.SharePoint.Administration.Health.SPHealthCheckStatus]::Failed)
{
Write-Host "Calling the Repair method"
Write-Host "Result:" $checkIfFileOnDiskExistRuleRepair.Repair()
}
Summary:
It's really easy to make rules, that will give the It-pros a fight change to managed and maintain the SharePoint platform. Microsoft has some “Guidelines for Designing Health Rules”.
My demo code