如何使用PowerShell重新构建Windows搜索索引?

3

由于我们甚至在微软的帮助下都没有找到解决我们不断增长的Windows搜索数据库的方法,因此我们决定在其达到特定限制时通过SCOM定期重建数据库。这涉及到Windows Server 2012 R2。

因此,我需要一个PowerShell脚本,调用ResetReindex方法,这些方法属于ISearchCatalogManager接口。

到目前为止,我想到了以下内容:

# 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()

然而,这会抛出以下异常:

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

我在这里做错了什么?


1
你正在尝试实例化一个接口。在C#中,这是“可行的”,因为它会透明地生成正确的CoClass,但在PowerShell中不行,你得到你所要求的内容。该程序集是否包含“SearchManagerClass”? - Jeroen Mostert
感谢您澄清。汇编包含 CSearchManagerClass(请注意前面的 C),但如果我调用 GetCatalog(),则不包含方法 Reindex()Rebuild() - Matthias Güntert
1个回答

1
我发现我使用的是过时的Microsoft.Search.Interop.dll版本。
以下是我的解决方法:
首先从微软官网下载Windows Search 3.x SDK。忽略系统要求部分,所需的DLL也可以在2012 R2(很可能也适用于8.1)上使用。然后使用下面的PowerShell代码重置搜索索引。
# 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()

太好了,谢谢!出于某种奇怪的原因,最后一行对我不起作用(“方法调用失败,因为[System.__ComObject]不包含名为'Reset'的方法。”)。我通过使用[Microsoft.Search.Interop.ISearchCatalogManager].GetMethod("Reset").Invoke($catalog, @())来解决它。 - Heinzi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接