Using Powershell to parse config.txt and large log files
Created: 2024-10-16 15:15:00Modified: 2026-02-12 12:35:05
Tags: Troubleshooting
Sometimes logs are so large they are not easily opened in text editors. Or, you may wish to parse specific data from the log (such as errors).
The findstr command is useful for simple searches.
The Powershell Get-Content combined with Select-String allow you to output lines just prior to or just following the search string.
Sample log data: (Line numbers added for clarity)
Add Object | cn=Mini.pearl@acme.com,OU=SyncGAL,DC=Acme,DC=com
Add Object | cn=marnie.pearl@acme.com,OU=SyncGAL,DC=Acme,DC=com
Database Error | 19
Database Message | UNIQUE constraint failed:
object.dstdn
Source DN | CN=Patrick Burns,OU=Users,DC=Corp,DC=com
Dest DN | cn=patrick.burns@acme.com,OU=SyncGAL,DC=Acme,DC=com
db srcdn | CN=patrick.burns@acme.com,OU=Users,DC=Corp,DC=com
Add Object | cn=kuld.Nancy@acme.com,OU=SyncGAL,DC=Acme,DC=com
Add Object | cn=Joe.Smith@acme.com,OU=SyncGAL,DC=Acme,DC=com
The data you want to parse out from the log is “UNIQUE constraint”. And you want the one preceding line as well as the three lines after.
Example:
To search for and output the error data, do the following:
- Open Powershell
- Execute:
Get-Content 2024-10-11-11-46-38-sim | Select-String -Pattern "UNIQUE constraint" -Context 1,3 >errordata.txt
Returned data
Database Error | 19
Database Message | UNIQUE constraint failed: object.dstdn
Source DN | CN=Patrick Burns,OU=Users,DC=Corp,DC=com
Dest DN | cn=patrick.burns@acme.com,OU=SyncGAL,DC=Acme,DC=com
db srcdn | CN=patrick.burns@acme.com,OU=Users,DC=Corp,DC=com
Find more tips on using get-Content here