Matthias Güntert
Matthias Güntert

Reputation: 4668

How to rebuild Windows Search Index by using PowerShell?

As we didn't find any solution to our ever growing Windows Search DBs (not even with Microsofts help) we decided to rebuild the DBs regularily by SCOM, when they hit a specific limit. This relates to Windows Server 2012 R2.

I therefor need a PowerShell script that calls the Reset or Reindex method belonging to the ISearchCatalogManager interface.

So far I came up with the following:

# Load DLL containing classes & interfaces
Add-Type -path "C:\Temp\SearchIndex\Microsoft.Search.Interop.dll"

# Create new ISearchManager object 
$sm = New-Object Microsoft.Search.Interop.ISearchManager

# should return ISearchCatalogManager object 
$catalog = $sm.GetCatalog("SystemIndex")

# Call the method 
$catalog.Reindex()

This however throws the following exception:

New-Object : A constructor was not found. Cannot find an appropriate constructor for type Microsoft.Search.Interop.ISearchManager.
At C:\Users\myuser\Desktop\test.ps1:8 char:6
+ $sm = New-Object Microsoft.Search.Interop.ISearchManager
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand

What am I doing wrong here?

Upvotes: 3

Views: 12515

Answers (1)

Matthias Güntert
Matthias Güntert

Reputation: 4668

I turned out that I was using an outdated version of Microsoft.Search.Interop.dll.

Here is how I solved it:

First download the Windows Search 3.x SDK from Microsoft. Ignore the part about the system requirements. The required DLL can also be used on 2012 R2 (and very likely on 8.1). Then use below PowerShell code to reset the Search Index.

# Load DLL containing classes & interfaces
Add-Type -path "C:\Temp\SearchIndexSdk\Microsoft.Search.Interop.dll"

# Provides methods for controlling the Search service. This 
# interface manages settings and objects that affect the search engine 
# across catalogs. 
#
# https://msdn.microsoft.com/en-us/library/bb231485(v=vs.85).aspx
$sm = New-Object Microsoft.Search.Interop.CSearchManagerClass

# Retrieves a catalog by name and creates a new ISearchCatalogManager 
# object for that catalog.
$catalog = $sm.GetCatalog("SystemIndex")

# Resets the underlying catalog by rebuilding the databases 
# and performing a full indexing. 
#
# https://msdn.microsoft.com/en-us/library/bb266414(v=vs.85).aspx
$catalog.Reset()

Upvotes: 1

Related Questions