Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 885
user24442 Avatar asked Nov 15 '12 02:11

user24442


2 Answers

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!

like image 166
user24442 Avatar answered Oct 20 '22 15:10

user24442


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.

like image 2
Garett Avatar answered Oct 20 '22 14:10

Garett