0
votes

Trying to learn how to use Powershell to help with pulling data from various sources and banging my head on a poorly formed CSV. I have a CSV that just has a text string in the first cell of each row. Each row has a hash of a file, some data and the hash of the program that wrote it. I need to extract the first hash after "link hash=" below to another file.

[enter code here][1]

If the image doesn't work the first lines read:

The file "<share><link hash="randomhash1">C:\random\location\file.exe</link></share>" was first detected on a local disk. The file was created by the application "<share><link hash="differenthash">C:\windows\explorer.exe</link></share>".
The file "<share><link hash="randomhash2">C:\random\location\file.exe</link></share>" was first detected on a local disk. The file was created by the application "<share><link hash="differenthash">C:\windows\explorer.exe</link></share>".
The file "<share><link hash="randomhash3">C:\random\location\file.exe</link></share>" was first detected on a local disk. The file was created by the application "<share><link hash="differenthash">C:\windows\explorer.exe</link></share>".
The file "<share><link hash="randomhash4">C:\random\location\file.exe</link></share>" was first detected on a local disk. The file was created by the application "<share><link hash="differenthash">C:\windows\explorer.exe</link></share>".

I have been looking for a way to tell powershell to give me the value for "hash=" "" in a new CSV, but hitting syntax errors and obviously just doing it tremendously wrong. As I am trying to learn this, if somebody could state this is the regex and why I would be eternally grateful.

Below is what I have tried. Looking at Select-String I have a knowledge gap on how to get the regex match to become an object I can output. If I take out the ForEach-Object argument I get a csv with a true statement for each line.

Import-Csv C:\Users\me\Desktop\Bad_hash.csv | Select-string -Pattern "(?\w+)" | ForEach-Object {$hash = $_.Matches[0].Groups['hash'].Value [PSCustomObject] @{ Hash = $hash}} | | Export-Csv C:\Users\me\Desktop\test.csv

1
You have not included any code or example data. Please update your question with sample CSV data as well as the script/code you are using as well as the output you are expecting.Bill_Stewart
Check out Select-String.AdminOfThings
Your update only has the text you're looking for, which is good, but we really need a sample of the CSV file (shortened to a few lines and redacted appropriately, of course). You also haven't posted the script/code you are using to attempt to parse it.Bill_Stewart
@Bill_Stewart - Thanks I have been poking at Select-String to see if I could figure this out. Only about a month into learning PowerShell. I have put in what I have been working on.Hezikiah
You still not have included a CSV sample.Bill_Stewart

1 Answers

0
votes

Select-String -pattern is your RegEx friend when Import-Csv isn't a viable option. I'd be more helpful if I had samples.