write-output "Function to fix MIS 140 errors entered"
write-output "------------------"
pause
$badid = 'SB140.'
$SBDAT = Get-Content C:\Users\username\Desktop\U14187SB_testing.DAT
foreach ($line in $SBDAT)
{
write-output 'Processing a line' // this line displays
if($line -contains 'SB140') // this fails when I know that get-content file has the string in it many times
{ # pause
write-output 'found a SB140'
}
}
I have tried very hard to read on this and experiment, but being new to PS, I am not so good at its grammerI have tried very hard to read on this and experiment, but being new to PS, I am not so good at its grammer
-contains works by selecting exact matches only out of an array. -in works the same way, just semantically reversed; you can do $array -contains $item or $item -in $array, but neither will search within a string.
In order to determine whether an individual string contains some substring, use $Line.Contains($Value)
JS, yes all correct. But the object I am searching into is piped from a xxx=get-content and then foreach $line of xxx (and I am not sure I understand what type that is for the right search or comparison command), but not searching against a interactive defined for sure string type.
Js, even some thing so direct as if($line -macth’SB140’) is not working. I am wondering if it’s something entirely different? Though the ‘processing line’ output works. All the statements within the brackets { } after the if condition should execute, should they not?
-match should work. What does the input file look like? “select-string sb140 -inputobject $sbdat” will turn the whole file into one line. “if ($line | select-string sb140)” might do what you want.
“When you use the InputObject parameter to submit a collection of strings, Select-String treats the collection as a single combined string. Select-String returns the strings as a unit if it finds the search text in any string.”
Unless you want to do some delayed binding like:
if ($line | select-string -inputobject {$_})
If the data file is unicode no bom, which can happen in windows, you can try “get-content filename -encoding unicode”.