Home > OS >  Powershell content of the file
Powershell content of the file

Time:08-03

can you please help me with this one.

I need to create a script to check for a file (file name change everyday but it always starts with 'CA') in a folder (c:\temp) and check if this file (the file has no extension) contains word 'error' in it and export results to log file

CodePudding user response:

$working_directory = "C:\test\"

$name_string = Get-ChildItem -Path $working_directory | select Name | Foreach { "{0}" -f $_.Name }

if ($name_string.Contains("CA")) 
{
$read_lines = Get-Content -Path $working_directory$name_string
foreach($item in $read_lines) 
{
    if($item.Contains("error"))
    {
        $item | Out-File -FilePath C:\test\$item    
    }
}
}

Read directory of file with "CA" if line in file contains "error" output that line to another file.

CodePudding user response:

sls error c:\temp\ca* | ac c:\temp\logfile.txt

  • Related