Select- 字符串问题间歇性工作
Select-String issue working intermittently
我有一些代码有时可以工作,而另一些则不能。当它失败时我没有看到任何错误,所以我不知道为什么它是间歇性的。
我正在使用 Select-String cmdlet,它将找到我想要的许多匹配项。
我也使用与我的测试相同的文本文件,所以它不会改变正在搜索的数据。
$Hospinput.Text
是正在输入的搜索项。
搜索文件内容为:
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
通过在搜索框中输入 win,它将显示此.. 任何带有 'win' 的内容。
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
..这行上面没有代码只有文本文件内容和搜索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)"
}
上面的代码是表单的一部分。它将搜索文本文件并根据输入进行匹配。这很好用。但只是有时。要以表格形式显示结果,我单击搜索按钮。当我点击搜索时,它有时会显示结果。有时它没有。我每次测试都没有做任何不同的事情。
任何人都可以阐明吗?
它间歇性地执行此操作
您不应在此处使用 Format-Table
cmdlet,只需使用 Select-Object
cmdlet 上的 -expand
开关即可检索所需的输出:
$list = Select-String -AllMatches -Path "x:\Scripts\PowerShell\HospDepartments.txt" -pattern ($HospInput.Text) | Select -expand line
除此之外,您的问题可能与 Select-String
cmdlet 使用正则表达式 并且您可能在搜索框中输入了一些正则表达式字符有关?您可以通过将 -SimpleMatch
开关添加到 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.
编辑:
您可以将代码重构为:
Select-String -AllMatches -Path "x:\Scripts\PowerShell\HospDepartments.txt" -pattern ($HospInput.Text) |
Select-Object -expand line | ForEach-Object {
$textbox1.Items.Add($_)
}
我有一些代码有时可以工作,而另一些则不能。当它失败时我没有看到任何错误,所以我不知道为什么它是间歇性的。
我正在使用 Select-String cmdlet,它将找到我想要的许多匹配项。
我也使用与我的测试相同的文本文件,所以它不会改变正在搜索的数据。
$Hospinput.Text
是正在输入的搜索项。
搜索文件内容为:
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
通过在搜索框中输入 win,它将显示此.. 任何带有 'win' 的内容。
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
..这行上面没有代码只有文本文件内容和搜索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)"
}
上面的代码是表单的一部分。它将搜索文本文件并根据输入进行匹配。这很好用。但只是有时。要以表格形式显示结果,我单击搜索按钮。当我点击搜索时,它有时会显示结果。有时它没有。我每次测试都没有做任何不同的事情。
任何人都可以阐明吗?
它间歇性地执行此操作
您不应在此处使用 Format-Table
cmdlet,只需使用 Select-Object
cmdlet 上的 -expand
开关即可检索所需的输出:
$list = Select-String -AllMatches -Path "x:\Scripts\PowerShell\HospDepartments.txt" -pattern ($HospInput.Text) | Select -expand line
除此之外,您的问题可能与 Select-String
cmdlet 使用正则表达式 并且您可能在搜索框中输入了一些正则表达式字符有关?您可以通过将 -SimpleMatch
开关添加到 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.
编辑:
您可以将代码重构为:
Select-String -AllMatches -Path "x:\Scripts\PowerShell\HospDepartments.txt" -pattern ($HospInput.Text) |
Select-Object -expand line | ForEach-Object {
$textbox1.Items.Add($_)
}