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?

Alex Guteniev
  • 12,039
  • 2
  • 34
  • 79
user24442
  • 161
  • 1
  • 5

2 Answers2

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!

user24442
  • 161
  • 1
  • 5
  • 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. – js2010 Jun 23 '20 at 20:29
  • 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. – ElektroStudios Oct 10 '22 at 10:33
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.

Garett
  • 16,632
  • 5
  • 55
  • 63
  • 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? – user24442 Nov 15 '12 at 08:16
  • Does anybody have any idea how to use the ISearchCrawlScopeManager::AddRoot method? – user24442 Nov 16 '12 at 14:16
  • 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. – user24442 Nov 18 '12 at 15:38
  • One more question, it is possible to be done using PowerShell? – user24442 Nov 18 '12 at 16:03
  • @user24442 Yes it can be done from PowerShell. See http://yellowonline.tweakblogs.net/blog/7285/scripting-the-windows-search-service-in-w7-and-w2k8r2.html or http://powertoe.wordpress.com/2010/05/17/powershell-tackles-windows-desktop-search/ for examples – Garett Nov 18 '12 at 21:38