3

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?

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Matthias Güntert
  • 4,013
  • 6
  • 41
  • 89
  • 1
    You're trying to instantiate an interface. This "works" in C# because it transparently magics up the correct CoClass, but not in PowerShell, where you get what you ask for. Does the assembly contain `SearchManagerClass`? – Jeroen Mostert Sep 16 '16 at 15:52
  • Thanks for clarifying. The assembly contains `CSearchManagerClass` (note the C infront of it), but if I then call `GetCatalog()` it doesn't contain the method `Reindex()`nor `Rebuild()` – Matthias Güntert Sep 16 '16 at 15:56

1 Answers1

1

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()
Matthias Güntert
  • 4,013
  • 6
  • 41
  • 89
  • Great, thanks! For some strange reason, the last lined didn't work for me (`Method invocation failed because [System.__ComObject] does not contain a method named 'Reset'.`). I was able to fix it by using `[Microsoft.Search.Interop.ISearchCatalogManager].GetMethod("Reset").Invoke($catalog, @())` instead. – Heinzi Mar 25 '19 at 13:30