title: PowerShell length function
body: <p>Just yesterday I posted a question for getting the length of a text file. It was working perfectly. I haven't changed anything, but still I am unable to get the length of <code>$select1</code>.</p>
  
  <pre class="lang-bsh prettyprint-override"><code>Param([string]$servername)
  
  $filePath1 = "D:\temp\result.txt"
  
  try {
    $SqlQuery = "select SUBSTRING(CONVERT(sysname, SERVERPROPERTY('ProductVersion')),0,CHARINDEX('.',convert(sysname,SERVERPROPERTY('ProductVersion')),0))"
    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
  
    $SqlConnection.ConnectionString = "Server = $Servername; Database = master; Integrated Security = SSPI;"
    #write-host $SqlConnection.ConnectionString
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
    $SqlCmd.CommandText = $SqlQuery
    $SqlCmd.Connection = $SqlConnection
    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
    $SqlAdapter.SelectCommand = $SqlCmd
    $DataSet = New-Object System.Data.DataSet
    $SqlAdapter.Fill($DataSet)
    $version = $DataSet.Tables[0].rows[0].Column1
    #Write-Host $version
    $DataSet.Tables[0] | Out-File "C:\temp\version.csv"
  
    switch -exact ($version) {
      '8'  {$version = 80}    # SQL Server 2000 =  8.00.xxxx
      '9'  {$version = 90}    # SQL Server 2005 =  9.00.xxxx
      '10' {$version = 100}   # SQL Server 2008 = 10.00.xxxx
      '11' {$version = 110}   # SQL Server 2012 = 11.00.xxxx
      '12' {$version = 120}   # SQL Server 2014 = 12.00.xxxx
      '13' {$version = 130}   # SQL Server 2016 = 13.00.xxxx
    }
  
    Write-Host $version
    $filePath="C:\Program Files\Microsoft SQL Server\$version\Setup Bootstrap\Log\Summary.txt"
    Write-Host $filePath
  
    $select = Select-String -Pattern "Final result:" -Path $filePath #| Out-File c:\pattern.txt
    $select1 = Select-String -Pattern "Final result:" -Path $filePath | Out-File D:\temp\pattern.txt
  
    Write-Host($select1.Line.Length)
    Write-Host($select1.Matches.Count)
  
    if ($select1.Line.Length -gt 0) {
    #   Write-Host 'Contains String'
      $select2 = Select-String -Pattern "Passed" -Path 'D:\temp\pattern.txt'
      Write-Host($select2.Line.Length)
      if ($select2.Line.Length -gt 0) {
        Out-File -FilePath $filePath1
        'Success'| Out-File -FilePath $filePath1
      } else {
        Out-File -FilePath $filePath1
        'failed'| Out-File -FilePath $filePath1
      }
    } else {
      Write-Host 'Does not contain String'
    }
  } catch {
    Write-Host -BackgroundColor Red -ForegroundColor White "Fail"
    $errText =  $Error[0].ToString()
  
    if ($errText.Contains("network-related")) {
      Write-Host "Connection Error. Check server name, port, firewall."
    }
  
    Write-Host $errText
    continue
  }
  </code></pre>
  
  <p>Loop is not working, it was working till yesterday.</p>
  
  <pre><code>Write-Host($select1.Line.Length)
  Write-Host($select1.Matches.Count)
  
  if($select1.Line.Length -gt 0) {
  #  Write-Host 'Contains String'
  </code></pre>