Tuesday, 7 June 2011

PowerShell script to set the SharePoint office server search Performance Level to Maximum

### This script has has C# code wrapped inside PowerShell

### The first part is the C# code that does the work mapped to a variable $Source

$Source = @"

using Microsoft.SharePoint;
using Microsoft.Office.Server.Search;
using Microsoft.Office.Server.Search.Administration;

namespace PowerShellWrapper
{
public class Program
{
public static void PerformanceLevel_set(string[] args)
{
SPSite site = new SPSite(args[0]);
SearchService ss = site.WebApplication.Farm.Services.GetValue("OSearch");
ss.PerformanceLevel = SearchPerformanceLevel.Maximum;
ss.Update();
site.Dispose();
}
}
}

"@
##################################################################

### This next bit prepares Powershell for compiling the C# code

### Obtains an ICodeCompiler from a CodeDomProvider class.

$provider = New-Object Microsoft.CSharp.CSharpCodeProvider

## Get the location for System.Management.Automation DLL

$dllName = [PsObject].Assembly.Location

## Configure the compiler parameters

$compilerParameters = New-Object System.CodeDom.Compiler.CompilerParameters

### Next load the assemblies

$assemblies = @("System.dll", $dllName)

$compilerParameters.ReferencedAssemblies.AddRange($assemblies)

$compilerParameters.ReferencedAssemblies.Add("C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI\Microsoft.SharePoint.dll")

$compilerParameters.ReferencedAssemblies.Add("C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI\Microsoft.Office.Server.dll")

$compilerParameters.ReferencedAssemblies.Add("C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI\Microsoft.Office.Server.Search.dll")

$compilerParameters.IncludeDebugInformation = $true

$compilerParameters.GenerateInMemory = $true

### This next line does the work

$compilerResults = $provider.CompileAssemblyFromSource($compilerParameters, $Source)

### some error handling

if($compilerResults.Errors.Count -gt 0) {

$compilerResults.Errors
% { Write-Error ("{0}:`t{1}" -f $_.Line,$_.ErrorText) }

}

[PowerShellWrapper.Program]::PerformanceLevel_set(http://localhost/)

### Because it points to localhost it will only work when run locally

################################################################### END

No comments:

Post a Comment