question

EricPettinger-9636 avatar image
0 Votes"
EricPettinger-9636 asked IanXue-MSFT answered

Powershell Script outputting to file and then parsing the newly created logfile

I have written a simple PS script which uses "Select-String" to search a file structure for content using the input supplied by the end user.

The behavior I am struggling with is that when the search is completed, and the results are written to the chosen log file, it includes hundreds of lines of log showing that "Select-String" has performed the search, found results, logged it to a file and THEN it searches the newly created log file as well resulting in hundreds of lines of results which make no sense.


Please let me know why this is occurring and how I can get around it.


Here is the full script:

cls
Write-Host ""
Write-Host " When entering File Paths, be sure to include the FULL path"
Write-Host ""
Write-Host " For example: C:\users\MyUsername\TempFolder "
Write-Host " NO trailing Slash is required"
Write-Host ""
$FileLocation = Read-Host -Prompt ' Please provide the FULL path to the desired file(s) for your search'
Write-Host ""
Write-Host ""
Write-Host " Please input the desired file Search Term for this search"
Write-Host " For example: Successful installation"
Write-Host "" $SearchTerm1 = Read-Host -Prompt ' Input your Search Term'
Write-Host ""
Write-Host "" $OutPutFile = Read-Host -Prompt ' What name would you prefer for your Search Log? '
Write-Host ""
Write-Host ""
Write-Host " You chose '$FileLocation' as your search path for this run"
Write-Host ""
Write-Host " and you chose to search for '$SearchTerm1' as the content you are seeking'"
Write-Host ""
Write-Host " Performing your search now, please standby"
Write-Host ""
Write-Host " Your SearchResults log will open upon completion regardless of the findings"
pushd $FileLocation

Select-String -Path $FileLocation*.* -Pattern $SearchTerm1 |Out-File -FilePath $FileLocation\$OutputFile

popd

windows-serverwindows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

Don't place the output file in the same directory.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered

Hi,

You can place the log files in another path or use the "-exclude" parameter of the Select-String cmdlet to specify the log files that should be excluded.

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.