Reputation: 75
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.
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.
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
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