如何重建Windows搜索索引使用PowerShell

本文关键字:索引 搜索 PowerShell Windows 何重建 重建 | 更新日期: 2023-09-27 18:07:28

由于我们没有找到任何解决方案来解决我们不断增长的Windows搜索数据库(甚至没有microsoft的帮助),我们决定在数据库达到特定限制时,通过SCOM定期重建数据库。这涉及到Windows Server 2012 R2。

因此,我需要一个PowerShell脚本来调用属于ISearchCatalogManager接口的Reset或Reindex方法。

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

# 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

我在这里做错了什么?

如何重建Windows搜索索引使用PowerShell

我发现我使用的是过时的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()