Getting Error in following $serverone.ForEach

Hi

 

I’m trying to run the following script:

$serverone = Select-String -Path “C:\Users\spate\Documents\sample*.txt” -Pattern ‘server one’

$serverone.ForEach(
{
if($serverone.contains(‘SampleFile’))
{
write-host “Sample File is” $_.Filename
}
else
{
Write-Host ‘False’
}
}

 

At line:11 char:2

  • }
  • ~
    Missing ‘)’ in method call.
  • CategoryInfo : ParserError: (:slight_smile: [], ParentContainsErrorRecordException
  • FullyQualifiedErrorId : MissingEndParenthesisInMethodCall

This should do the trick:

Select-String -Path 'C:\Users\spate\Documents\sample*.txt' -Pattern 'server one' |
    Select-Object -Property Path

:wink:

To address the reason you’re getting the error, you’re missing the closing parenthesis ‘)’ for the ForEach() method call.

These errors are easier to spot if you indent your code and use an editor with highlighting such as VSCode (which will also automatically insert closing brackets). Also consider adding comments at closing brackets.

$serverone = Select-String -Path “C:\Users\spate\Documents\sample*.txt” -Pattern ‘server one’

$serverone.ForEach(

    {

        if ($serverone.contains(‘SampleFile’)) {
            write-host “Sample File is” $_.Filename
        } #end if
        
        else {
            Write-Host ‘False’
        } #end else

    } #end script block

) # end $serverone.ForEach

Thanks Matt and Olaf:

 

Matt,

The script know works with the fix you suggested.

But know i wonder if you can help me further. I’m not getting the right output?

You see $serverone contains the folowing:

C:\Users\spate\Documents\SampleFile.txt
C:\Users\spate\Documents\SampleFile10.txt
C:\Users\spate\Documents\SampleFile3.txt
C:\Users\spate\Documents\SampleFile4.txt

So i’m using “$serverone.Contains(‘SampleFile’)”

In follwing script below the if staement shoud goto “write-host “Sample File is” $_.Filename”

But i’m getting

False
False
False
False

$serverone.ForEach(

{

if ($serverone.contains(‘SampleFile’)) {
write-host “Sample File is” $_.Filename
} #end if
else {
Write-Host ‘False’
} #end else

} #end script block

) # end $serverone.ForEach

Could you please format your code as code?

Here you can read how that works: Guide to Posting Code.

What is it actually what you’re after? I’d recommend using the pure Powershell way like I showed above.

$serverone.ForEach(

{

if ($serverone.contains(‘SampleFile’)) {
write-host “Sample File is” $_.Filename
} #end if
else {
Write-Host ‘False’
} #end else

} #end script block

) # end $serverone.ForEach

OLAF —The above code should be true for the “IF” statemen and do the following

“write-host “Sample File is” $_.Filename”

 

It is going to Else statement and doing

Write-Host ‘False’.

 

Thanks

For every line in $serverone, you’re checking if $serverone contains ‘SampleFile’. The correct syntax for what you’re trying to do is:

$sampleone.ForEach({$_.Contains('SampleFile)})

In all honesty though, that’s horrible code. Contains is meant for checking for the existence of objects in collections, it’s not really meant for comparing bits of Strings. As Olaf suggests, use native PowerShell for this.

Select-String -Path .\sample.txt -Pattern 'SampleFile' | Select -ExpandProperty Line

 

 

Thank You Matt and Olaf.