简体   繁体   中英

Powershell script to extract data from file without an extension

Presently executing a batch file on an hourly schedule to kick off our testing software and export the results into log files. The log files are a generic file type, with no file extension.

I am currently running three powershell lines manually to extract specified lines from the log files and export them into one large text file. First is to change to the directory in which the logs are saved, second is to scrape the current directory and add it to a file, and the third is to scrape selected text from the file and append to the same output file as the directory. LINES:

  1. cd directorypath
  2. (Get-Item -Path ".\\" -Verbose).FullName | Add-Content c:\\TestLogs\\LogData.txt
  3. Get-Content -Path .\\"{0CF215CA-7E09-4280-A792-6184B9366FCA}" | Select-String Last | Add-Content c:\\TestLogs\\LogData.txt

For line 1 the directory path will always change. The log files are stored in C:\\TestLogs. Within that directory are subfolder with the following format example: Log072020170800

Within line 3, the data is contained within a file entitled {0CF215CA-7E09-4280-A792-6184B9366FCA}. This file name will always change, but it will be within the same 8-4-4-4-12 (GUID) format. There is no extension on this file name, and there are two other files within the directory which have this same file name structure. However, the file I wish to scrape is usually around 60kb in size, while the others are between 2-5kb.

I would like to automate the process of running these three PowerShell lines, but I am not sure on the best course of action. Any suggestions would be appreciated.

Something like this should do the trick assuming those files are always enclosed in curly braces. Otherwise, you can modify the wildcard search on the path to match better what you're trying to do

$directories = get-childitem directorypath
foreach ($directory in $directories){
Get-item -path $directory.fullname | add-content C:\TestLogs\LogData.txt
get-content -path "{*}" | Select-String Last | Add-Content c:\TestLogs\LogData.txt}

Given your requirement to sort out files with the same GUID naming scheme, you will need to determine what to base your sorting on. Ideally, there would be something inside or about the file that would tell you for sure. Thinks like modified time, or modifying user might be worth checking to see if they would work for you. Since you provided approximate file sizes as your determining factor we can work with that.

get-childitem "C:\\YourPath" | where { $_.length -gt 10kb -and $_.name -like "{*}" } | Select-String Last | Add-Content c:\\YourOutput.txt

I would tweek that 10kb up as high as you think you could get away with to cut down on false positives with this method.

You could do this at the top level of your log folder assuming everything is under one parent: get-childitem "C:\\YourPath" -Recurse | where { $_.length -gt 10kb -and $_.name -like "{*}" } | Select-String Last | Add-Content c:\\YourOutput.txt get-childitem "C:\\YourPath" -Recurse | where { $_.length -gt 10kb -and $_.name -like "{*}" } | Select-String Last | Add-Content c:\\YourOutput.txt

or wrap the first version in a foreach loop where you pass in your directories.

I think this would help you out!

Let's set a place for the output data to go

$CollectedInfo = 'C:\Testlogs\Logdata.txt'

Let's define where we will be searching for the subfolders.

$MainLogLocation = 'C:\TestLogs'

Now that we know where to look for the possible folders, let's narrow things down. You may want to use LastAccessTime vs. LastWriteTime .

$Newest = Get-ChildItem -Path $MainLogLocation | Sort-Object LastWriteTime -Descending | Select-Object -First 1

Great, now we know the newest subfolder/batch. Let's find the files within that are a GUID and at the same time, let's pick the largest as required.

$WorkFile = Get-ChildItem $Newest.FullName | Where-Object {$_.Name -match "^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$"} | Sort-Object Length -Descending | Select-Object -First 1

Now we wanted to put a line in the log that indicates what folder we are pulling the data from.

Add-Content -Value $WorkFile.Directory $CollectedInfo

Finally, we look at the file, and search for whatever line we want, and append it to the output file $CollectedInfo . This could be run a few times to get different information, or setup to use an array and ForEach loop.

Get-Content -Path $WorkFile.FullName | Select-String 'STUFF I NEED' | Add-Content $CollectedInfo
Get-Content -Path $WorkFile.FullName | Select-String 'OTHER STUFF I NEED' | Add-Content $CollectedInfo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM