6 hours later and I am getting closer. Started learning Visual Studio Code vs ISE so I can easily use PowerShell 7. Learned about -Parallel, then how you can't call variables in the Script Block without using $using:. (That took forever!) Tried the -Asjob part but that doesn't seem right for this. I am basically taking a text file with 20 lines and search for matches in a 300k+ lines of a CSV.
I believe I have gotten to a part I just can't figure out. I think I need to know how to use "thread safe" objects based upon the error.
$textfile = "C:\test_delete\key_words.txt"
$loglocation = "C:\test_delete\datablob.csv"
$outputfile = "C:\test_delete\test5.csv"
New-Item -Path $outputfile
Get-Content $textfile | ForEach-Object -Parallel {
Select-String -Raw -Path $using:loglocation -Pattern $_ |
Out-File -append $using:outputfile
}
Error Out-File: Line | 3 | Out-File -append $using:outputfile | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | The process cannot access the file 'C:\test_delete\test5.csv' because it is being used by another process.
In the ForEach-Object help, Example 11, it talks about thread safe usage and the example declares the variable = the thread safe syntax. That makes sense (kinda). Except in this case, I am wanting to eventually have a paramenter declared for $outfile which will need to be called in a tread safe manner. I am getting ahead of myself but just can't figure out how to make that thread safe call to $outfile.
Sorry for be verbose, this is my first post and just want to relay I have spent some time trying to get this. Thanks in advance!