Powershell If Statement Loop

I have the following code that I am using to look at the contents of a file.

 Foreach ($file In $files) # Loop through all *.txt files in the $path directory
  {
    
    If ($file.FullName -eq $path + "test.txt")
      {
      ForEach ($control In $controls) # Loop through the search strings in the control file
            {
              $result = Get-Content $file.FullName | Select-String $control -quiet -casesensitive
      
              If ($result -eq $True)
              {
                $match = $file.FullName
                "Test :  $control  in file :  $match" | Out-File $output -Append

                #delete file for this section
                #run batchfile to load data

                #reloop this sequence

              }#end of string if statement
      

            } # end of search through control file
      }
How can I tell my code to run that particular if statement again without moving to the next if statement if a the "test.txt" file is there.

Thanks

April 23rd, 2015 3:35pm

Just use a loop.

Invert the logic so that the second if executes when the first one fails.

Free Windows Admin Tool Kit Click here and download it now
April 23rd, 2015 6:09pm

This is my entire script so far. If the result of any of the Get-Content is TRUE, I need it to remove the $file.FullName file, run a batch script. Then I need it to loop back through the entire script to refresh the $files variable. I know this is a strange one.

$path     =  "F:\PERSONAL\POWERSHELL\"   
$files    = Get-Childitem $path *.txt -Recurse | Where-Object { !($_.psiscontainer) }
$controls = Get-Content ($path + "\controlfile.rdl")
$output   = $path + "\output.log"
$output2   = $path + "\output2.log"
$output3   = $path + "\output3.log"

Function getStringMatch
{
  
  Foreach ($file In $files) # Loop through all *.txt files in the $path directory
  {
    

                 If ($file.FullName -eq $path + "test.txt")
                      {
                      ForEach ($control In $controls) # Loop through the search strings in the control file
                            {
                              $result = Get-Content $file.FullName | Select-String $control -quiet -casesensitive
      
                              If ($result -eq $True)
                              {
                                $match = $file.FullName
                                "Test :  $control  in file :  $match" | Out-File $output -Append
                                #Remove-Item $file.FullName #delete file for this section
                                #run batchfile to load data

                                #reloop this sequence

                              }#end of string if statement
                                   

                            } # end of search through control file
                      }
    }
   


      ###################################################################################################################

              elseif ($file.FullName -eq $path + "test2.txt")
        {
                    ForEach ($control In $controls) # Loop through the search strings in the control file
                {
                  $result = Get-Content $file.FullName | Select-String $control -quiet -casesensitive
      
                  If ($result -eq $True)
                  {
                    $match = $file.FullName
                    "Test2 :  $control  in file :  $match" |  Out-File $output2 -Append



                  }#end of string if statement
      

         } # end of search through control file
        
        
        }

        ##################################################################################################################

        elseif ($file.FullName -eq $path + "test3.txt")
        {
                ForEach ($control In $controls) # Loop through the search strings in the control file
                    {
                      $result = Get-Content $file.FullName | Select-String $control -quiet -casesensitive
      
                      If ($result -eq $True)
                      {
                        $match = $file.FullName
                        "Test3 :  $control  in file :  $match" |  Out-File $output3 -Append



                      }#end of string if statement
      

                    } # end of search through control file
        
        
         }

        #####################################################################################################################

     

  } #end of ForEach for the file





getStringMatch

April 24th, 2015 8:04am

All of you if/elseif statements are identical. Only the first one will lever be executed.

Get rid of the non-standard formatting and comments so you can actually see the logic.

Free Windows Admin Tool Kit Click here and download it now
April 24th, 2015 10:27am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics