dimanche 31 octobre 2021

If loop not working inside powershell function

Function RemoveDataFromFile ([string] $FileType,[string] $RecordToRemove)
{
$stupidShit=$RecordToRemove
Write-Host $stupidShit
$RemovedLines = @()
Write-Host "Entering Remove File Proc"
$FolderName="C:\Temp\GTR"
$typeToCheck = switch ($FileType) {
    "PDT" {"pdtcode" ; break}
    "DET" {"detnumber"; break}
    "ADR" {"detnumber"; break}
    "POS" {"detnumber"; break}
    "PYD" {"detnumber"; break}
    "SMN" {"detnumber"; break}
    "REL" {"pdtcode"; break}
    "DED" {"detnumber"; break}
    "ALW" {"detnumber"; break}
    "TER" {"detnumber"; break}
    "RTN" {"rtnparam01"; break}

   default {"Something else happened"; break}
   }


 $FileList = Get-ChildItem -Path $FolderName -Filter "*$FileType*"
#write-host $FileType "_" $FileList "++" $recCheck

foreach($singleFiles in $FileList)
{
Write-Host "Checking -" $singleFiles.Name "++ " $recCheck
    $data = foreach($line in Get-Content $singleFiles.FullName)
            {
           
            Write-Host "checiking"
           
            $recCheck="$typeToCheck=""$RecordToRemove"""
            Write-Host "Record Trying to check " $RecordToRemove
              Write-Host "Entering COntent to Check - " $recCheck
           Write-Host $line
           $what=($line -match $recCheck )
           Write-Host "out before loop"  $what
        
                if( $what )
                {
                Write-Host "Match Found"
                $object = New-Object -TypeName PSObject
                $object | Add-Member -Name 'Type' -MemberType NoteProperty -Value $FileType
                $object | Add-Member -Name 'FileName' -MemberType NoteProperty -Value $singleFiles.Name
                $object | Add-Member -Name 'Info' -MemberType NoteProperty -Value $line

                $RemovedLines+=$object
                }
                else
                {
                  $line
                }
              

            }
  #  $data | Set-Content $singleFiles.FullName 
}

return $RemovedLines

}

# call
$Det_RemovedLines=RemoveDataFromFile "DET" $DetRecord.EmployeeID.Trim() 

($line -match $recCheck ) is false - but is correct!!!

($line -match $recCheck ) is false but im checking detnumber="303353y" in a line cbr="detadd",detnumber="303355y",dettitle="Mrs",detsurname="Awesome123",detdatejnd= so the condition should return true but doesn't. any help would be much appreciated.

Aucun commentaire:

Enregistrer un commentaire