Author Posts

March 7, 2017 at 8:57 pm

Hello,

I have a function called Search to search for a string in files in a folder. If it found a file without the string it moves it to another folder.

So the Function in line 26 works. If found one file not containing text and moved it to the appropriate folder

The issue with function starting in 35, it think none of the files contain that word $Lookup, so it moved all files into the folder. Is the string too long?

$Choice = $null

Function Search($Folder, $CopyTo, $Pattern, $Lookup){
    Remove-Item $CopyTo\*
    $Read = Get-ChildItem $Folder | Where-Object {$_.FullName -match "$Pattern"}    
    $Found = 0
    $NotFound = 0
    $FileCount = 0
    foreach($Files in $Read){    
        $FileCount++    
        $ReadFile = Get-Content ($Files.FullName)   
        if($ReadFile -ccontains $Lookup){                   
            $Found++      
        }

        else{                      
            $NotFound++        
            Copy-Item ($Files.FullName) -Destination $CopyTo
        }
    }
    Write-Host "There are" $FileCount "text files matching $Pattern!"
    Write-Host "Found" $Found "files matching text!"
    Write-Host "Copied" $NotFound "files to $CopyTo`n" -ForegroundColor Red
}

Function BackupRouterConfigs(){
    $RouterFolder = "\\hqfs1\Shares\HQShared\IT\NetworkBackups\BackupRouterConfigs"
    $RouterCopyTo = "\\hqfs1\Shares\HQShared\IT\NetworkBackups\RouterSearchResults"
    $Pattern = "Current.Running.MR"
    $Lookup = "event manager environment _email_server 000.00.00.000"

    Search $RouterFolder $RouterCopyTo $Pattern $Lookup
}

Function BranchSwitchBackups(){    
    $SwitchFolder = "\\hqfs1\Shares\HQShared\IT\NetworkBackups\BranchSwitchBackups"
    $SwitchCopyTo = "\\hqfs1\Shares\HQShared\IT\NetworkBackups\SwitchSearchResults"
    $Pattern = "Current.Running"    
    $Lookup = "69643a697070686f6e652e6d6974656c2e636f6d3b73775f746674703d31302e36302e3130312e343b63616c6c5f7372763d31302e36302e3"

    Search $SwitchFolder $SwitchCopyTo $Pattern $Lookup
}

Write-Host "Make a selection and press enter to continue!" -ForegroundColor Green
Write-Host "---------------------------------------------`n" -ForegroundColor Cyan

While($Choice -ne 0)
{
    Write-Host "[1]`tSearch BackupRouterConfigs folder" -ForegroundColor Green
    Write-Host "[2]`tSearch BranchSwitchBackups folder" -ForegroundColor Green
    Write-Host "[0]`tQuit program" -ForegroundColor Green
    $Choice = Read-Host "`tSelection: "
    Write-Host "`n"

    switch($Choice)
    {
        1 {BackupRouterConfigs}      
        2 {BranchSwitchBackups}      
    }
}

Thanks,

Tony

March 7, 2017 at 10:58 pm

Found the solution by adding select-string.

Thanks,

Tony