-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEverything3-PowerShell-Wrapper.psm1
More file actions
411 lines (350 loc) · 14.4 KB
/
Copy pathEverything3-PowerShell-Wrapper.psm1
File metadata and controls
411 lines (350 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# Everything3 PowerShell Wrapper - Vollständig korrigiert
# Erfordert Everything3_x64.dll im gleichen Verzeichnis oder im PATH
#region Helper Classes
class Everything3Client {
[IntPtr]$Handle
[bool]$IsConnected
Everything3Client([string]$instanceName) {
$this.Handle = [Everything3SDK]::Everything3_ConnectW($instanceName)
$this.IsConnected = $this.Handle -ne [IntPtr]::Zero
if (-not $this.IsConnected) {
$error = [Everything3SDK]::Everything3_GetLastError()
throw "Fehler beim Verbinden mit Everything-Instanz '$instanceName'. Fehler: $error"
}
}
[void] Dispose() {
if ($this.IsConnected -and $this.Handle -ne [IntPtr]::Zero) {
[Everything3SDK]::Everything3_DestroyClient($this.Handle)
$this.Handle = [IntPtr]::Zero
$this.IsConnected = $false
}
}
}
class Everything3Result {
[string]$FullPath
[string]$Name
[string]$Directory
[hashtable]$Properties = @{}
[bool]$Exists
Everything3Result([string]$fullPath) {
$this.FullPath = $fullPath
$this.Name = [System.IO.Path]::GetFileName($fullPath)
$this.Directory = [System.IO.Path]::GetDirectoryName($fullPath)
$this.Exists = Test-Path -LiteralPath $fullPath -ErrorAction SilentlyContinue
}
}
#endregion
#region Private Functions
function Get-PropertyId {
param([string]$PropertyName)
$propertyMap = @{
'Name' = [Everything3Properties]::EVERYTHING3_PROPERTY_ID_NAME
'Path' = [Everything3Properties]::EVERYTHING3_PROPERTY_ID_PATH
'FullPath' = [Everything3Properties]::EVERYTHING3_PROPERTY_ID_FULL_PATH
'Size' = [Everything3Properties]::EVERYTHING3_PROPERTY_ID_SIZE
'DateCreated' = [Everything3Properties]::EVERYTHING3_PROPERTY_ID_DATE_CREATED
'DateModified' = [Everything3Properties]::EVERYTHING3_PROPERTY_ID_DATE_MODIFIED
'DateAccessed' = [Everything3Properties]::EVERYTHING3_PROPERTY_ID_DATE_ACCESSED
'Attributes' = [Everything3Properties]::EVERYTHING3_PROPERTY_ID_ATTRIBUTES
'Type' = [Everything3Properties]::EVERYTHING3_PROPERTY_ID_TYPE
'Extension' = [Everything3Properties]::EVERYTHING3_PROPERTY_ID_EXTENSION
}
return $propertyMap[$PropertyName]
}
function ConvertFrom-FileTime {
param([ulong]$FileTime)
try {
if ($FileTime -eq 0 -or $FileTime -eq 0xFFFFFFFFFFFFFFFF) {
return $null
}
return [DateTime]::FromFileTime($FileTime)
}
catch {
return $null
}
}
#endregion
#region Public Functions
function Connect-Everything {
<#
.SYNOPSIS
Verbindet sich mit der Everything-Suchmaschine.
#>
[CmdletBinding()]
param(
[string]$InstanceName = $null
)
try {
Write-Verbose "Verbinde mit Everything-Instanz: $($InstanceName ?? 'Standard')"
return [Everything3Client]::new($InstanceName)
}
catch {
if ([string]::IsNullOrEmpty($InstanceName)) {
Write-Verbose "Standard-Verbindung fehlgeschlagen, versuche '1.5a'-Instanz"
try {
return [Everything3Client]::new("1.5a")
}
catch {
throw "Fehler beim Verbinden mit Standard- und '1.5a'-Everything-Instanzen: $($_.Exception.Message)"
}
}
else {
throw
}
}
}
function Search-Everything {
<#
.SYNOPSIS
Führt eine Suche mit der Everything-Suchmaschine durch.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[Everything3Client]$Client,
[Parameter(Mandatory)]
[string]$Query,
[int]$MaxResults = 1000,
[int]$Offset = 0,
[switch]$MatchCase,
[switch]$MatchWholeWord,
[switch]$MatchPath,
[switch]$Regex,
[string[]]$Properties = @(),
[hashtable]$SortBy = @{}
)
if (-not $Client.IsConnected) {
throw "Client ist nicht mit Everything verbunden"
}
# Search state erstellen
$searchState = [Everything3SDK]::Everything3_CreateSearchState()
if ($searchState -eq [IntPtr]::Zero) {
throw "Fehler beim Erstellen des Search State"
}
try {
# Suchoptionen konfigurieren
[void][Everything3SDK]::Everything3_SetSearchTextW($searchState, $Query)
[void][Everything3SDK]::Everything3_SetSearchViewportOffset($searchState, [uint32]$Offset)
[void][Everything3SDK]::Everything3_SetSearchViewportCount($searchState, [uint32]$MaxResults)
[void][Everything3SDK]::Everything3_SetSearchMatchCase($searchState, $MatchCase.IsPresent)
[void][Everything3SDK]::Everything3_SetSearchMatchWholeWords($searchState, $MatchWholeWord.IsPresent)
[void][Everything3SDK]::Everything3_SetSearchMatchPath($searchState, $MatchPath.IsPresent)
[void][Everything3SDK]::Everything3_SetSearchRegex($searchState, $Regex.IsPresent)
# Property-Requests hinzufügen
$propertyIds = @()
foreach ($prop in $Properties) {
$propId = Get-PropertyId -PropertyName $prop
if ($propId) {
[void][Everything3SDK]::Everything3_AddSearchPropertyRequest($searchState, $propId)
$propertyIds += @{Name = $prop; Id = $propId }
}
else {
Write-Warning "Unbekannte Eigenschaft: $prop"
}
}
# FIX: Wenn benutzerdefinierte Eigenschaften angefordert werden, wird der Pfad nicht mehr standardmäßig mitgeliefert.
# Wir müssen ihn daher ebenfalls explizit anfordern, damit Everything3_GetResultFullPathNameW ihn finden kann.
if ($Properties.Count -gt 0) {
$fullPathId = Get-PropertyId -PropertyName 'FullPath'
if ($fullPathId) {
[void][Everything3SDK]::Everything3_AddSearchPropertyRequest($searchState, $fullPathId)
}
}
# Sortierung hinzufügen
if ($SortBy.Count -gt 0) {
$sortPropId = Get-PropertyId -PropertyName $SortBy.Property
if ($sortPropId) {
$ascending = -not $SortBy.Descending
[void][Everything3SDK]::Everything3_AddSearchSort($searchState, $sortPropId, $ascending)
}
}
# Suche ausführen
Write-Verbose "Führe Suche aus: $Query"
$resultList = [Everything3SDK]::Everything3_Search($Client.Handle, $searchState)
if ($resultList -eq [IntPtr]::Zero) {
$error = [Everything3SDK]::Everything3_GetLastError()
throw "Suche fehlgeschlagen mit Fehler: $error"
}
try {
# Ergebnis-Anzahl abrufen
$viewportCount = [int][Everything3SDK]::Everything3_GetResultListViewportCount($resultList)
$totalCount = [int][Everything3SDK]::Everything3_GetResultListCount($resultList)
Write-Verbose "Gefunden $totalCount Ergebnisse insgesamt, gebe $viewportCount zurück"
# Ergebnisse verarbeiten
$results = @()
for ($i = 0; $i -lt $viewportCount; $i++) {
# Vollständigen Pfad abrufen
$pathBuffer = New-Object System.Text.StringBuilder(32768)
$pathLength = [Everything3SDK]::Everything3_GetResultFullPathNameW($resultList, [uint32]$i, $pathBuffer, [uint32]$pathBuffer.Capacity)
Write-Verbose("Pathlength: " + $pathLength)
if ($pathLength -gt 0) {
$fullPath = $pathBuffer.ToString()
$result = [Everything3Result]::new($fullPath)
# Zusätzliche Eigenschaften abrufen
foreach ($propInfo in $propertyIds) {
# Write-Verbose("propInfo.Name: " + $propInfo.Name)
try {
if ($propInfo.Name -in @('DateCreated', 'DateModified', 'DateAccessed')) {
$value = [Everything3SDK]::Everything3_GetResultPropertyUINT64($resultList, [uint32]$i, $propInfo.Id)
$result.Properties[$propInfo.Name] = ConvertFrom-FileTime -FileTime $value
}
elseif ($propInfo.Name -eq 'Size') {
$value = [Everything3SDK]::Everything3_GetResultPropertyUINT64($resultList, [uint32]$i, $propInfo.Id)
$result.Properties[$propInfo.Name] = $value
}
elseif ($propInfo.Name -eq 'Attributes') {
$value = [Everything3SDK]::Everything3_GetResultPropertyDWORD($resultList, [uint32]$i, $propInfo.Id)
$result.Properties[$propInfo.Name] = $value
}
else {
$textBuffer = New-Object System.Text.StringBuilder(1024)
$textLength = [Everything3SDK]::Everything3_GetResultPropertyTextW($resultList, [uint32]$i, $propInfo.Id, $textBuffer, [uint32]$textBuffer.Capacity)
if ($textLength -gt 0) {
$result.Properties[$propInfo.Name] = $textBuffer.ToString()
}
}
}
catch {
Write-Warning "Fehler beim Abrufen der Eigenschaft $($propInfo.Name) für $fullPath : $($_.Exception.Message)"
}
}
$results += $result
}
}
return $results
}
finally {
[void][Everything3SDK]::Everything3_DestroyResultList($resultList)
}
}
finally {
[void][Everything3SDK]::Everything3_DestroySearchState($searchState)
}
}
function Disconnect-Everything {
<#
.SYNOPSIS
Trennt die Verbindung zur Everything-Suchmaschine.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[Everything3Client]$Client
)
$Client.Dispose()
Write-Verbose "Everything-Client getrennt"
}
function Find-Files {
<#
.SYNOPSIS
Praktische Wrapper-Funktion für die Dateisuche mit Everything.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$Pattern,
[int]$MaxResults = 1000,
[string[]]$Extensions = @(),
[switch]$IncludeProperties,
[switch]$CaseSensitive,
[switch]$Regex,
[string]$InstanceName = $null
)
$client = $null
try {
# Mit Everything verbinden
$client = Connect-Everything -InstanceName $InstanceName
# Query mit Erweiterungen erstellen
$query = $Pattern
if ($Extensions.Count -gt 0) {
$extQuery = ($Extensions | ForEach-Object { "ext:$_" }) -join "|"
$query = "$Pattern ($extQuery)"
}
# Eigenschaften festlegen
$properties = @()
if ($IncludeProperties) {
$properties = @("Size", "DateModified", "DateCreated", "Attributes")
}
# Suche ausführen
$searchParams = @{
Client = $client
Query = $query
MaxResults = $MaxResults
Properties = $properties
MatchCase = $CaseSensitive
Regex = $Regex
}
Write-Verbose($searchParams | ConvertTo-Json)
return Search-Everything @searchParams
}
finally {
if ($client) {
Disconnect-Everything -Client $client
}
}
}
function Test-EverythingConnection {
<#
.SYNOPSIS
Testet die Verbindung zu Everything und zeigt Systeminformationen an.
#>
[CmdletBinding()]
param()
Write-Host "=== Everything3 Verbindungstest ===" -ForegroundColor Cyan
try {
$client = Connect-Everything
try {
Write-Host "✓ Erfolgreich mit Everything verbunden" -ForegroundColor Green
# Version abrufen
try {
$majorVersion = [Everything3SDK]::Everything3_GetMajorVersion($client.Handle)
$minorVersion = [Everything3SDK]::Everything3_GetMinorVersion($client.Handle)
Write-Host "✓ Everything Version: $majorVersion.$minorVersion" -ForegroundColor Green
}
catch {
Write-Host "⚠ Konnte Versionsinformationen nicht abrufen" -ForegroundColor Yellow
}
# DB-Status prüfen
try {
$dbLoaded = [Everything3SDK]::Everything3_IsDBLoaded($client.Handle)
if ($dbLoaded) {
Write-Host "✓ Everything-Datenbank ist geladen" -ForegroundColor Green
}
else {
Write-Host "⚠ Everything-Datenbank ist nicht geladen" -ForegroundColor Yellow
}
}
catch {
Write-Host "⚠ Konnte DB-Status nicht prüfen" -ForegroundColor Yellow
}
# Test-Suche
Write-Host "Führe Test-Suche durch..." -NoNewline
$testResults = Search-Everything -Client $client -Query "*.txt" -MaxResults 5
Write-Host " ✓ Erfolgreich ($($testResults.Count) Ergebnisse)" -ForegroundColor Green
}
finally {
Disconnect-Everything -Client $client
}
}
catch {
Write-Host "✗ Verbindung fehlgeschlagen: $($_.Exception.Message)" -ForegroundColor Red
Write-Host ""
Write-Host "Fehlerbehebung:" -ForegroundColor Yellow
Write-Host "1. Stellen Sie sicher, dass Everything 1.5 läuft"
Write-Host "2. Überprüfen Sie, ob Everything3_x64.dll im PATH ist"
Write-Host "3. Versuchen Sie es mit Administratorrechten"
}
}
#endregion
#region Module Exports
Export-ModuleMember -Function @(
'Connect-Everything',
'Search-Everything',
'Disconnect-Everything',
'Find-Files',
'Test-EverythingConnection'
)
#endregion
# Modul-Initialisierung
Write-Verbose "Everything3 PowerShell Wrapper geladen. Verwenden Sie Test-EverythingConnection zum Testen."
Write-Host "Everything3 PowerShell Wrapper bereit. Verwenden Sie Test-EverythingConnection zum Testen der Verbindung." -ForegroundColor Green