divinefaith
Goto Top

Powershell Pfad nach einem Wort durchsuchen

Moin moin,

ich müsste in powershell in einem Logpfad nach einem Wort durchsuchen, ob dieser als Dateiname und als Inhalt in den Dateien vorkommt. Ich habe schon eine Textbox als User Input und diesen User Input in der Variablen $UserInput eingegeben.
das mit dem Dateinamen suchen funktioniert, allerdings mit dem inhalt noch nicht.
Außerdem soll das Skript die unterverzeichnise durchsuchen können.

Danke schon mal für eure Hilfe.


#TextBox User Input
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Textbox'
$form.Size = New-Object System.Drawing.Size(350,200)
$form.StartPosition = 'CenterScreen'

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,120)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(160,120)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(320,20)
$label.Text = 'Please enter the word you are searching for:'
$form.Controls.Add($label)

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,60)
$textBox.Size = New-Object System.Drawing.Size(285,80)
$form.Controls.Add($textBox)

$form.Topmost = $true

$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()

#Put User Input in a variable
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$UserInput = $textBox.Text
}

#Select the path where the word is to be searched for

$Directory = "C:\Program Files (x86)\Common Files\enteo\NiLogs"

#Search for the Word in the path
Get-ChildItem -Path $Directory -recurse | Where-Object {$_.Name -like "*$UserInput*"} |Format-Table Name
Get-ChildItem -Path $Directory -recurse | Select-string -SimpleMatch "$UserInput" |Format-Table Name

Content-Key: 4074544213

Url: https://administrator.de/contentid/4074544213

Printed on: May 11, 2024 at 10:05 o'clock

Member: em-pie
em-pie Sep 27, 2022 updated at 06:51:39 (UTC)
Goto Top
Moin,

zunächst: Nutze bitte die < code >< /code > Tags. Das macht das Lesen deutlich leichter

allerdings mit dem inhalt noch nicht.
Dafür nutzt du auch das falsche CMDLet. was du brauchst ist ein Get-Content()

Gruß
em-pie
Member: chkdsk
chkdsk Sep 27, 2022 at 06:55:31 (UTC)
Goto Top
Moin,
ich habe es so gelöst.
Function Get-Folder($initialDirectory)
{
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null  
    $Ordnername = New-Object System.Windows.Forms.FolderBrowserDialog
    $Ordnername.Description = "Ordner auswählen"  
    $Ordnername.rootfolder = "MyComputer"   
    if($Ordnername.ShowDialog() -eq "OK")  
    {
        $Ordner += $Ordnername.SelectedPath 
    }
    return $Ordner
}
$Path = Get-Folder 

$Text = read-host "dein Wort"   
$PathArray = @()
Get-ChildItem $Path -recurse -Filter "*" |  
Where-Object { $_.Attributes -ne "Directory"} |  
ForEach-Object {
If (Get-Content $_.FullName | Select-String -Pattern $Text) {
$PathArray += $_.FullName
}
}
Write-Host "Contents of ArrayPath:"  
$PathArray | % {$_} | Out-File "C:\temp\Results.txt"  

$FileLocation = 'C:\temp\results.txt'  
Start-Process notepad.exe $FileLocation
Member: colinardo
Solution colinardo Sep 27, 2022 updated at 09:34:48 (UTC)
Goto Top
Servus,
# Suchbegriff
$searchterm = 'test'  

# Rekursiv zu durchsuchender Ordner
$folder = 'C:\DeinOrdner'  

# Abfrage ausführen und komplette Pfade der zutreffenden Dateien in Variable $files speichern
$files = Get-ChildItem -LiteralPath $folder -File -Recurse -Force | ?{$_.Name -like "*$searchterm*" -or (Select-String $_.FullName -Pattern $searchterm -SimpleMatch -Quiet)} | select -Expand Fullname  

# Ergebnis verwenden
$files
Grüße Uwe
Member: DivineFaith
DivineFaith Sep 27, 2022 at 11:29:28 (UTC)
Goto Top
Danke das hat genauso funktioniert, wie ich es mir vorgestellt habe. ^^