CSE RootLog powershell script


# Specify the regions
# All regions "AUS","CHN","HKG","ID1","KDF","KLM","MAC","MYS","SGP","THA","THD","TWN","VNM"
$regions = "AUS","CHN","HKG","ID1","KDF","KLM","MAC","MYS","SGP","THA","THD","TWN","VNM"

# Specify the target string pattern
$pattern = "error:ORA-01013: user requested cancel of current operation"
$targetDate = "2024-10-23"

$outputFilePath = "C:\Users\chnadmin\Desktop\RootLog ora error\"

foreach($region in $regions){
$directory = "F:\CSE_PRD\CSE_$region\logs"
$filePattern = "Rootlog*"

$outputFileName = -join($region, "output.txt")
$outputFileDest = -join($outputFilePath, $outputFileName)


# Get a list of files starting with $filePattern in the specified directory
$files = Get-ChildItem -Path $directory -Filter $filePattern


# Loop through each file and search for the string $pattern in files starting with $filePattern
foreach ($file in $files) {
    $matches = Select-String -Path $file.FullName -Pattern $pattern -Context 0, 1
    if ($matches) {
        foreach ($match in $matches) {
            # Get the first 16 characters of the line, first 16 characters = datetime
            $first16 = $match.Line.Substring(0, [Math]::Min($match.Line.Length, 16))
            
			# Check the $targetDate match or not
			if(-not [string]::IsNullOrEmpty($targetDate) -and $first16.Substring(0, 10) -ne $targetDate){
              continue
            }

            # Construct the output line with the first 16 characters and the matched pattern
            $outputLine = "$first16, $pattern, $region"
            
            # Append the output line to the output.txt file
            Add-Content -Path $outputFileDest -Value  $outputLine
        }
    }
}
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *