How to add a location to windows 7/8 search index using batch or vbscript?
Asked Answered
S

2

6

I'm trying to add a location (scope) to my Windows 8 Search Index programmatically, and after some googling, I found this code:

Set objISAdm = CreateObject("Microsoft.ISAdm")
Set objCatalog = objISAdm. GetCatalogByName("MyCatatlog")
Set objScope= objCatalog.AddScope("C:\myfiles",False)
objScope.Alias = "MyCatalogScope"

Unfortunately, a 800A01AD error prompts suggesting object Microsoft.ISAdm cannot be created; with some further digging, it seems the above code doesn't work with the newer version of Windows Search on Windows 8.

Does anyone know how to do that using VB scripts or from command line, presumably something works under windows 7 will work on Windows 8 as well?

Semiology answered 15/11, 2012 at 2:1 Comment(0)
S
8

Garett, you are a genius! This is the code I learned from the links you provided:

#Code copied from "Powershell Tackles Windows Desktop Search" http://powertoe.wordpress.com/2010/05/17/powershell-tackles-windows-desktop-search/
#Microsoft.Search.Interop.dll is needed, download from http://www.microsoft.com/en-us/download/details.aspx?id=7388
#Load the dll
Add-Type -path "D:\Unattend\UserFiles\Tools\Microsoft.Search.Interop.dll"
#Create an instance of CSearchManagerClass
$sm = New-Object Microsoft.Search.Interop.CSearchManagerClass 
#Next we connect to the SystemIndex catalog
$catalog = $sm.GetCatalog("SystemIndex")
#Get the interface to the scope rule manager
$crawlman = $catalog.GetCrawlScopeManager()
#add scope
$crawlman.AddUserScopeRule("file:///D:\*",$true,$false,$null)
$crawlman.SaveAll()

Save the code as AddScope.ps1, and run it from a elevated cmd console:

PowerShell Set-ExecutionPolicy Unrestricted -force
PowerShell D:\Unattend\UserFiles\AddScope.ps1

That's it!

Semiology answered 19/11, 2012 at 13:14 Comment(2)
You can run it without changing the execution policy: powershell -executionpolicy unrestricted d:\unattend\userfiles\addscope.ps1 The purpose of the execution policy is to not unknowingly execute a script.Lymphoid
For those coming to find a solution to do this under .NET, you can use "tlbimp-Microsoft.Search.Interop" nuget package. The code will be almost exactlly the same as in this PowerShell solution.Zetland
J
2

In the code you provided you're attempting to use the Indexing service interface. The indexing service is no longer available in Windows 8. From the documentation:

Indexing Service is no longer supported as of Windows XP and is unavailable for use as of Windows 8. Instead, use Windows Search for client side search and Microsoft Search Server Express for server side search.

As the documentation states, you will want to look into Windows Search.

UPDATE:

I've not done this, but to accomplish what you are seeking the documentation states

Before you can use any of the Crawl Scope Manager (CSM) interfaces, you must perform the following prerequisite steps:

  1. Create the CrawlSearchManager object and obtain its ISearchManager interface.
  2. Call ISearchManager::GetCatalog for "SystemIndex" to obtain an instance of an ISearchCatalogManager interface.
  3. Call ISearchCatalogManager::GetCrawlScopeManager to obtain an instance of ISearchCrawlScopeManager interface.

After making any changes to the Crawl Scope Manager (CSM), you must call ISearchCrawlScopeManager::SaveAll. This method takes no parameters and returns S_OK on success.

Here's one example and another for doing this.

Unfortunately, I don't think this can be done from VBScript, because the COM interfaces provided by the Windows Search API don't implement the IDispatch interface, which allows scripting languages like VBScript to call COM objects via late binding.

Does it have to be from VBScript, or can you do it in .NET? If it has to be from VBScript then one approach would be to write a wrapper in .NET and expose it as a COM object.

Jonjona answered 15/11, 2012 at 3:5 Comment(5)
Thank you Garett. The information you provided is exactly what I found from MSDN. I think using ISearchCrawlScopeManager::AddRoot method is the right direction. But I'm not a VB/VBS programmer, can you give me some working codes?Semiology
Does anybody have any idea how to use the ISearchCrawlScopeManager::AddRoot method?Semiology
Thank you Garett again! It will be a little too complicated If it has to be done with executable binary. I'm asking because I want to automatically add a location to the index following an unattended windows setup. Your answer is valuable. It saved me from chasing ghost.Semiology
One more question, it is possible to be done using PowerShell?Semiology
@Semiology Yes it can be done from PowerShell. See yellowonline.tweakblogs.net/blog/7285/… or powertoe.wordpress.com/2010/05/17/… for examplesJonjona

© 2022 - 2024 — McMap. All rights reserved.