Hans Blafoo
Hans Blafoo

Reputation: 75

Problems querying the Windows search index

I want to write a powershell script which searches the Windows index for files containing names which are provided as a parameter to the script. I searched the internet and came up with the following solution:

function Search-FileInIndex ($firstname, $lastname) {
$sql = "SELECT System.ItemName, System.DateCreated, System.ItemPathDisplay, System.Search.Rank FROM SYSTEMINDEX WHERE CONTAINS('`"$($firstname)`" NEAR `"$($lastname)`"') and SCOPE= 'c:\' and System.Search.Rank > 0 ORDER BY System.ItemPathDisplay"
$provider = "provider=search.collatordso;extended properties=’Application=Windows’;" 
$connector = new-object system.data.oledb.oledbdataadapter -argument $sql, $provider 
$dataset = new-object system.data.dataset 
if ($connector.fill($dataset)) {
    $dataset.tables[0] | format-table -autosize *
}

$dataset.Dispose()
$connector.Dispose()

}

Search-FileInIndex john doe

However, I have two problems.

Problem 1

The provided solution works and finds files, but sometimes I get really strange error messages like these ones resulting in an exception instead of a file listing:

    Ausnahme beim Aufrufen von "Fill" mit 1 Argument(en):  "Fehler E_FAIL(0x80004005) in IErrorInfo.GetDescription."
    In C:\Search-FileInIndex.ps1:11 Zeichen:9
    +     if ($connector.fill($dataset)) {
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : OleDbException

or

    Ausnahme beim Aufrufen von "Fill" mit 1 Argument(en):  "Die Pipe wurde beendet."
    In C:\Search-FileInIndex.ps1:11 Zeichen:9
    +     if ($connector.fill($dataset)) {
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : OleDbException

I read on the internet that especially the first error results from keywords being used but I don't see any reserved keywords in my query and as I already said, it works sometimes. Is this a race condition or memory issue? I'm running this on Windows 10 Enterprise 1703 with 16 GB of RAM.

EDIT: I got some new insights.

  1. If I run the script for the first time, it usually works. If I run it again, the crashes are likely to occur, but I didn't find a waiting time between these two runs yet.
  2. If it crashes there is an entry in the application log in the Event Viewer that IndexSearcher.exe was faulty.
  3. The problem seems to be the field System.Search.Rank. If I remove it, it runs flawlessy. However, I need the rank because otherwise I cannot filter the wrong results (due to the NEAR-search). Do you know a replacement for the NEAR-search?

Problem 2

Actually, I would like to have hit-highlighting showing me a snippet of the found matches in the file. However, the MSDN states that the API originally used for that is deprecated. I read on stackoverflow that I would have to use an explorer shell interface, but I have no clue how to do this. Can you provide a working example?

Thanks and best regards, Hans

Upvotes: 1

Views: 1241

Answers (1)

f6a4
f6a4

Reputation: 1782

I can reproduce the error and the troublemaker seems surprisingly the "NEAR" statement in SQL. This solution is working for me:

function Search-FileInIndex($firstname, $lastname) {

    $con = New-Object -ComObject ADODB.Connection
    $rs  = New-Object -ComObject ADODB.Recordset
    $sql = "SELECT System.ItemName, System.DateCreated, System.ItemPathDisplay, System.Search.Rank FROM SYSTEMINDEX WHERE CONTAINS(System.Search.Contents, '$($firstname)') and CONTAINS(System.Search.Contents, '$($lastname)') and SCOPE= 'c:\' and System.Search.Rank > 0 ORDER BY System.ItemPathDisplay"

    $con.Open("Provider=Search.CollatorDSO;Extended Properties='Application=Windows';")
    $rs.Open($sql, $con)

    While(-Not $rs.EOF){
        $rs.Fields.Item("System.ItemPathDisplay").Value
        $rs.MoveNext()
    }
    $rs.Close()
    $con.Close()
}

Search-FileInIndex john doe

Upvotes: 1

Related Questions