powershell -match 默认为垃圾

powershell -match defaulting to garbage

我有一个经常使用的方法,用于从文件中获取内容,然后 returns 两个给定参数之间的内容。它适用于所有其他文件(大约 15 个文件),但对于我刚刚添加的文件,它默认为甚至不在读取文件中的垃圾文本。我试过重新使用提供给该方法的文件名,以及使用不同的文件名。我尝试在返回垃圾的区域的文件中使用不同的 from/to 字符串。

这是调用的方法:

#Function to look for method content with to parse and return contents in method as string
#Note that $followingMethodName is where we parse to, as an end string. $methodNameToReturn is where we start getting data to return.
Function Get-MethodContents{
  [cmdletbinding()]
  Param ( [string]$codePath, [string]$methodNameToReturn, [string]$followingMethodName)
  Process
  {
      $contents = ""
      Write-Host "In GetMethodContents method File:$codePath method:$methodNameToReturn followingMethod:$followingMethodName"  -ForegroundColor Green

      $contents = Get-Content $codePath -Raw #raw gives content as single string instead of a list of strings
      $null = $contents -match  "($methodNameToReturn[\s\S]*)$followingMethodName" ###############?? wrong for just the last one added
      
      Write-Host "File Contents Found: $($Matches.Item(1))" -ForegroundColor DarkYellow
      Write-Host "File Contents Found: $($Matches.Item(0))" -ForegroundColor Cyan
      Write-Host "File Contents Found: $($Matches[0])" -ForegroundColor Cyan
      Write-Host "File Contents Found: $($Matches[1])" -ForegroundColor Cyan

      return $Matches.Item(1) 
  }#End of Process
}#End of Function

这是调用代码。 FileHandler2 的 GetMethodContents 默认为 $currentVersion (6000) returns,它甚至不在提供的文件中。

  elseif($currentVersion -Match '^(6000)') #6000
  {   
      $HopResultMap = [ordered]@{}
      $HopResultMap2 = [ordered]@{}
      #call method to return basePathFull cppFile method contents
      $matchFound = Get-MethodContents -codePath $File[0] -methodNameToReturn "Build the HOP error map" -followingMethodName "CHop2Windows::getHOPError" #correct match
      #call method to get what is like case info but is map in 6000 case....it's 2 files so 2 maps for 6000
      $HopResultMap = (Get-Contents60 -fileContent $matchFound)  #error map of ex: seJam to HOP_JAM
      $FileHandler = Join-Path -Path $basePathFull -ChildPath "Hop2Windows\XXHandler.cpp"
      $matchFound2 = Get-MethodContents -codePath $FileHandler -methodNameToReturn "XXHandler::populateVec" -followingMethodName "m_Warnings" #matches correctly
      $HopResultMap2 = (Get-Contents60_b -fileContent $matchFound2) #used in foreach
      #sdkErr uses Handler file too but it Get-methodContents is returning 6000 so try diff filename
      $FileHandler2 = Join-Path -Path $basePathFull -ChildPath "Hop2Windows\XXHandler.cpp"
      $matchFound3 = Get-MethodContents -codePath $FileHandler2 -methodNameToReturn "feed from No. 0" -followingMethodName "class CHop2Windows;" # returns 6000-wrong######################??
      $HopResultMap3 = (Get-Contents60_c -fileContent $matchFound3) #used in foreach
      #next need to put these 2 maps together
      #need to test I got matches correct in 6000 map still################
      #combine the maps so can reuse 7000's case/design.  $hopResultMap key is the common tie with $hopResultMap2 and thrown away
      $resultCase = foreach ($key in $HopResultMap.Keys){ 
        [PSCustomObject][ordered]@{
          sdkErr      = "HopResultMap3[$key]" # 0x04
          sdkDesc     = "HopResultMap2[$key]" # Fatal Error
          sdkOutErr   = "$($HopResultMap[$key])"
        }
      }
      
  }//else

这是 powershell 5.1 和 VSCode。

更新(根据要求):

$pathViewBase = 'C:\Data\CC_SnapViews\EndToEnd_view\' 
$HopBase = '\Output\HOP\'
$basePathFull = Join-Path -Path $pathViewBase -ChildPath $HopBase
$Hop2PrinterVersionDirs = @('Hop2Windowsxx\Hop2Windowsxx.cpp')


...
foreach($cppFile in $Hop2VersionDirs) #right now there is only one
{ 
   $File = @(Get-ChildItem -Path (Join-Path -Path $basePathFull -ChildPath $cppFile))

更新 2:

我试过这样转义并返回有问题的内容:

$matchFound3 = Get-MethodContents -codePath $FileHandler2 -methodNameToReturn [regex]::Escape("feed from No. 0") -followingMethodName regex[Escape("class CHop2Windowsxx;")

并看到这个错误:

Get-MethodContents : A positional parameter cannot be found that accepts argument 'Roll feed from No. 0'.

我想通了。我给它的是 .cpp 文件名,但我要查找的内容在 .h 文件名中。 :\