Select-String issue working intermittently

108 views Asked by At

I have some code that works sometimes and others not. I see no errors when it fails so I am stuck as to why its intermittent.

I am using the Select-String cmdlet which will find many matches which is what I want.

I am also using the same text file as my test so its not the data being searched changing.

$Hospinput.Text is the item being input to search for.

The content of the search file is:

Windows 7 Clinical Complete Nursing A3S
Windows 7 Clinical Complete Nursing A3S Wireless
Windows 7 Clinical Complete Nursing A4N
Windows 7 Clinical Complete Nursing A4N Wireless
Windows 7 Clinical Complete Nursing A4S
Windows 7 Clinical Complete Nursing A4S Wireless
Windows 7 Clinical Complete Observation
Windows 7 Clinical Complete Observation Wireless
SPU Fastpass
SPU Fastpass Wireless
24-7 - Windows 7 Pro x86
Admitting General - Windows 7 x86 - v1.7

By entering win into the search box it will display this.. Anything with 'win' in it.

Windows 7 Clinical Complete Nursing A3S
Windows 7 Clinical Complete Nursing A3S Wireless
Windows 7 Clinical Complete Nursing A4N
Windows 7 Clinical Complete Nursing A4N Wireless
Windows 7 Clinical Complete Nursing A4S
Windows 7 Clinical Complete Nursing A4S Wireless
Windows 7 Clinical Complete Observation
Windows 7 Clinical Complete Observation Wireless
24-7 - Windows 7 Pro x86
Admitting General - Windows 7 x86 - v1.7

..There is no code above this line only text file contents and what should be displayed after searching for win.

$list = (Select-String -AllMatches -Path "x:\Scripts\PowerShell\HospDepartments.txt" -pattern $HospInput.Text | Select line | ft -HideTableHeaders | Out-String).Trim()
$separator = "`n`r"
$Array = @($list.split($separator,     [System.StringSplitOptions]::RemoveEmptyEntries))
$Array.Length

If ($list) 

{
$Array.trim()
$Array | foreach{$textbox1.Items.Add($_) }

}Else 
{
$TextBox1.Text = "Error in finding $($hospInput.Text)"
}

The code above is part of a form. It will search a text file and match based on input. This works great. But only sometimes. To display the results in the form i click a search button. It's when i click search sometimes it displays the results. sometimes it does not. And i am doing nothing different each time i test.

can anyone shed any light?

It does this intermittently

1

There are 1 answers

7
Martin Brandl On BEST ANSWER

You shouldn't use the Format-Table cmdlet here, just use the -expand switch on the Select-Object cmdlet to retrieve your desired output:

$list = Select-String -AllMatches -Path "x:\Scripts\PowerShell\HospDepartments.txt" -pattern ($HospInput.Text) | Select -expand line

Beside that, your issue is probably related to the fact, that the Select-String cmdlet is using regex and you probably enter some regex characters into the searchbox? You could change this behaviour by adding the -SimpleMatch switch to the cmdlet:

-SimpleMatch

Uses a simple match rather than a regular expression match. In a simple match, Select-String searches the input for the text in the Pattern parameter. It does not interpret the value of the Pattern parameter as a regular expression statement.


Edit:

You could refactor your code to:

Select-String -AllMatches -Path "x:\Scripts\PowerShell\HospDepartments.txt" -pattern ($HospInput.Text) | 
    Select-Object -expand line | ForEach-Object {
        $textbox1.Items.Add($_)
    }