C# 如何使用PowerShell重建Windows搜索索引?
由于我们没有找到任何解决方案来解决不断增长的Windows Search数据库(甚至在微软的帮助下也没有),我们决定由SCOM定期重建数据库,当它们达到特定限制时。这与Windows Server 2012 R2有关 因此,我需要一个调用属于接口的或方法的PowerShell脚本 到目前为止,我得出了以下结论:C# 如何使用PowerShell重建Windows搜索索引?,c#,windows,powershell,C#,Windows,Powershell,由于我们没有找到任何解决方案来解决不断增长的Windows Search数据库(甚至在微软的帮助下也没有),我们决定由SCOM定期重建数据库,当它们达到特定限制时。这与Windows Server 2012 R2有关 因此,我需要一个调用属于接口的或方法的PowerShell脚本 到目前为止,我得出了以下结论: # Load DLL containing classes & interfaces Add-Type -path "C:\Temp\SearchIndex\Microsoft.
# 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()
但是,这会引发以下异常:
新对象:未找到构造函数。找不到类型为Microsoft.Search.Interop.ISearchManager的适当构造函数。
在C:\Users\myuser\Desktop\test.ps1:8 char:6
+$sm=新对象Microsoft.Search.Interop.ISearchManager
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+CategoryInfo:ObjectNotFound:(:)[New Object],pArgumentException
+FullyQualifiedErrorId:CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand
我在这里做错了什么?我发现我使用的是一个过时版本的
Microsoft.Search.Interop.dll
我是这样解决的:
首先从Microsoft下载。忽略有关系统需求的部分。所需的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()
您正在尝试实例化一个接口。这在C#中“起作用”,因为它透明地魔术般地创建了正确的CoClass,但在PowerShell中却不起作用,因为在PowerShell中,您可以得到您想要的东西。程序集是否包含
SearchManagerClass
?感谢您的澄清。程序集包含CSearchManagerClass
(注意它前面的C),但是如果我随后调用GetCatalog()
,它不包含方法Reindex()
或Rebuild()
太好了,谢谢!出于某种奇怪的原因,最后一行代码对我不起作用(方法调用失败,因为[System.\uu ComObject]不包含名为“Reset”的方法。
)。我可以通过使用[Microsoft.Search.Interop.ISearchCatalogManager].GetMethod(“Reset”).Invoke($catalog,@())
来修复它。