【问题标题】:How to rebuild Windows Search Index by using PowerShell?如何使用 PowerShell 重建 Windows 搜索索引?
【发布时间】:2017-01-24 21:59:22
【问题描述】:

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

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

到目前为止,我想出了以下几点:

# 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

我在这里做错了什么?

【问题讨论】:

  • 您正在尝试实例化一个接口。这在 C# 中“有效”,因为它透明地魔术了正确的 CoClass,但不是在 PowerShell 中,您可以在其中获得所需的内容。程序集是否包含SearchManagerClass
  • 感谢您的澄清。该程序集包含CSearchManagerClass(注意它前面的C),但如果我随后调用GetCatalog(),它不包含方法Reindex()Rebuild()

标签: c# windows powershell


【解决方案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()

【讨论】:

  • 太好了,谢谢!出于某种奇怪的原因,最后一行对我不起作用(Method invocation failed because [System.__ComObject] does not contain a method named 'Reset'.)。我可以改用[Microsoft.Search.Interop.ISearchCatalogManager].GetMethod("Reset").Invoke($catalog, @()) 来修复它。
猜你喜欢
  • 2012-12-04
  • 1970-01-01
  • 2020-07-28
  • 2013-05-02
  • 1970-01-01
  • 2019-07-07
  • 2016-01-12
  • 1970-01-01
  • 2012-07-23
相关资源
最近更新 更多