deepti
deepti

Reputation: 729

length function not giving output

$select.Length is not giving output. Please find code below:

clear
$filePath = "c:\temp\result.txt"
$select = Select-String -Pattern "Final result:" -Path 'C:\Program Files\Microsoft SQL Server\100\Setup Bootstrap\Log\Summary.txt' #| Out-File c:\pattern.txt
$select1 = Select-String -Pattern "Final result:" -Path 'C:\Program Files\Microsoft SQL Server\100\Setup Bootstrap\Log\Summary.txt' | Out-File D:\temp\pattern.txt
Write-Host($select.Length)

if ($select.Length -gt 0) {
    Write-Host 'Contains String'
    $select2 = Select-String -Pattern "Passed" -Path 'D:\temp\pattern.txt'

    if ($select2.Length -gt 0) {
        #Out-File C:\temp\result.txt $filePath -Append 
        New-Item $filePath -ItemType file
        'Success' | Out-File -FilePath $filePath -Append   
    } else {
        New-Item $filePath -ItemType file
        'Failed' | Out-File -FilePath $filePath -Append  
    }
} else {
    Write-Host 'Does not contain String'
}

Ideally it should generate file with success as text in result.txt, but it's not working. I'm not getting any error.

Upvotes: 0

Views: 176

Answers (2)

G42
G42

Reputation: 10019

  • $select does not contain type [String], which I assume is what you expect to have the property Length. Instead, use $select.Line.Length, where $select.Line is the matched line. Repeat for $select2

Get-Member shows Line property

  • $select1 - don't pipe to out file at the same time as assigning variable. I think below gets you what you want and avoids that.

$select = Select-String -Pattern "Final result:" -Path 'C:\Program Files\Microsoft SQL Server\100\Setup Bootstrap\Log\Summary.txt'

# This outputs the matching line only
$select.Line |Out-File D:\temp\pattern.txt

Upvotes: 1

guiwhatsthat
guiwhatsthat

Reputation: 2434

this will count the matches:

$select2.Matches.Count

Upvotes: 1

Related Questions