title: PowerShell The Try statement is missing its Catch or Finally block
body: <p>I am <strong>very</strong> new to PowerShell and have only recently discovered this gem of a tool. I have pieced together a few different scripts to allow me to search a drive for Excel files and see if they contain a pattern of characters. I think I have everything in it I need, however I keep getting an error with the braces (as far as I can tell).</p>
  
  <p>Does the REGEX portion of my script make sense? Will the two lines below be sufficient in determining whether each cell value matches the prescribed pattern? </p>
  
  <pre><code>$formula = $workSheet.cells.item($row,$column).value
  if($formula -match [regex]$REGEX)
  </code></pre>
  
  <p>On a separate note, I keep getting an error (see after code for details) when trying to run the script. I have tried adding and removing braces since that is what the message keeps telling me if the problem.</p>
  
  <p>My script is written as such:</p>
  
  <pre><code>Function Search-Files-For-Patterns {
      [cmdletbinding()]
      Param (
          [parameter(Mandatory)]
          [ValidateScript({
              Try {
                  If (Test-Path -Path $_) {$True}
                  Else {Throw "$($_) is not a valid path!"}
              }
              Catch {
                  Throw $_
              }
          })]
          [String]$Source,       
          [parameter(Mandatory)]
          [string]$SearchText
      )
      $LogCSV = "X:\PowerShell\Scrape.csv"
      $Filelist =  Get-ChildItem $Source -Filter '*.xls*' -Recurse
      $Excel = New-Object -ComObject Excel.Application
      $DisableAutomationSecurity = [Microsoft.Office.Core.MsoAutomationSecurity]::msoAutomationSecurityForceDisable
      $EnableAutomationSecurity = [Microsoft.Office.Core.MsoAutomationSecurity]::msoAutomationSecurityByUI
      $REGEX = "\d\d\d-\d\d\d-\d\d\d"
  
      $Excel.Application.AutomationSecurity = $DisableAutomationSecurity
      Remove-Item $LogCSV -ErrorAction SilentlyContinue
  
      Out-File -FilePath $LogCSV -InputObject "File;Count;NAS"
      #"File;Count" &gt; $LogCSV                    
      foreach ($File in $Filelist) {
          $password = ""
          try {
              $Workbook = $Excel.Workbooks.Open($File.FullName, 0, 0, 5, $password)
              $Numbers = New-Object System.Collections.ArrayList
              $NumberCount = 0
              ForEach ($Worksheet in @($Workbook.Sheets)) {
                  $rowMax = ($Worksheet.usedRange.rows).count
                  $columnMax = ($Worksheet.usedRange.columns).count
                  For($row = 1 ; $row -le $rowMax ; $row ++) {
                      For($column = 1 ; $column -le $columnMax ; $column ++) {
                          $formula = $workSheet.cells.item($row,$column).value
                          #[string]$formula = $workSheet.cells.item($row,$column).value
                          if($formula -match [regex]$REGEX) {
                              $Numbers.Add("$($formula.Text);")
                              $NumberCount++
                              if ($NumberCount -eq 25) {break}
                          } #end for if $formula 
                      } #end for $column
                  } #end for $row            
                  if ($NumberCount -ge 10) {
                      Out-File -FilePath $LogCSV -InputObject "$($File.FullName);$NumberCount;$Numbers" -Append
                  } #end if $numbercount &gt;= 10
                  $workbook.close($false)
              } #end foreach worksheet
              catch {
                  Out-File -FilePath $LogCSV -InputObject "$($File.FullName);File is password protected - skipped;" -Append
              } #end catch
          } #end try &lt;--ERROR OCCURS HERE
      } #end for each file
  
      $Excel.Application.AutomationSecurity = $EnableAutomationSecurity
      [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$excel)
      [gc]::Collect()
      [gc]::WaitForPendingFinalizers()
      Remove-Variable excel -ErrorAction SilentlyContinue
  } #end function
  Search-Files-For-Patterns "X:\SomeFolder" "\d\d\d-\d\d\d-\d\d"
  </code></pre>
  
  <p>The error message I'm getting when I try to run this is:</p>
  
  <pre><code>At X:\PowerShell\ScrapeG\Search-Files-For-Patterns.ps1:58 char:10
  +         } #end try
  +          ~
  The Try statement is missing its Catch or Finally block.
      + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
      + FullyQualifiedErrorId : MissingCatchOrFinally
  </code></pre>
  
  <p>Any direction / help would be greatly appreciated.</p>
  
  <p>Thanks in advance.</p>