We help IT Professionals succeed at work.

Poweshell Script to schedule

A D
A D asked
on
I have a folder named Error Log with thousands of txt files being logged by a third party application. Its difficult to find a specific error from those files and proactively fix related issues timely.

Can you help me with a powershell script which can search specific words from within those files in Error Log folder (e.g. '.' is missing or schema mismatch) and copy only those specific files to other folder named Status Errors?

I can then check Status Errors folder every evening and fix any errors so as to avoid any issues start of next day?
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
Something like this?
$source = 'C:\Temp\Error Log\*.log'
$target = 'C:\Temp\Status Errors'
$pattern = "('\.' is missing)|(schema mismatch)"

Select-String -Path $source -Pattern $pattern | Copy-Item -Path $_.Path -Destination $target -Verbose -WhatIf

Open in new window


Edit:
On second thought, if you don't need the full power of regex, this is a bit easier to handle, as it generates the RegEx pattern on the fly, but it'll only search for literal strings.
$source = 'C:\Temp\Error Log\*.log'
$target = 'C:\Temp\Status Errors'
$strings = @(
	"'.' is missing"
	"schema mismatch"
)

$pattern = ($strings | ForEach-Object {"($([regex]::Escape($_)))"}) -join '|'
Select-String -Path $source -Pattern $pattern | Copy-Item -Path $_.Path -Destination $target -Verbose -WhatIf

Open in new window

Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
That still needs a correction:
Select-String -Path $source -Pattern $pattern  -List | Copy-Item -Path {$_.Path} -Destination $target -Verbose -WhatIf

Open in new window

-List is an optimization to only report the first match for each file. I didn' test whether it is optional or mandatory so files are copied only once.
Shaun VermaakSenior Consultant
CERTIFIED EXPERT
Awarded 2017
Distinguished Expert 2019

Commented:
or
copy *.txt combined.log

Open in new window

and then just inspect combined.log with something like TailBlazer
https://chocolatey.org/packages/tailblazer
A D

Author

Commented:
ok thanks for your responses.. I'll check this today!