2016-09-16 74 views
1

因爲我們沒有找到任何解決方案來發展我們不斷增長的Windows搜索數據庫(甚至沒有微軟的幫助),我們決定在SCOM定期重建數據庫時,他們達到了一個特定的限制。這涉及到Windows Server 2012 R2。如何使用PowerShell重建Windows搜索索引?

我爲此需要一個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 

我在做什麼錯在這裏?

+1

你正試圖實例化一個接口。這在C#中「起作用」,因爲它透明地將正確的CoClass魔法化,但不是在PowerShell中,在那裏你得到你所要求的東西。程序集是否包含「SearchManagerClass」? –

+0

感謝您澄清。程序集包含'CSearchManagerClass'(注意它的C infront),但如果我調用'GetCatalog()'它不包含方法'Reindex()'和'Rebuild()' – Matze

回答

0

我發現我使用的是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()