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

2023-12-08

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

因此我需要一个 PowerShell 脚本来调用Reset or Reindex方法属于ISearch目录管理器界面。

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

# 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

我在这里做错了什么?


我发现我使用的是过时的版本Microsoft.Search.Interop.dll.

我是这样解决的:

首先下载Windows 搜索 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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用PowerShell重建Windows搜索索引? 的相关文章

随机推荐