ConfigMgr Log Sanitizer Powershell Script

Do you ever need to email or post ConfigMgr log files or snippets online for blogs or support? I know I do. I wanted to have a way to consistently clean log files quickly, so I wrote a small PowerShell script to help. It’s nothing special - the hardest part is figuring out which regular expressions work best for each data type you want to replace. I ended up with 4 regular expressions that work well for the smsts.log. Here are the 4 types and examples of log file entries before and after each regex:

Server Name

1
2
3
$regex.Add("ASD\w*-\w+","XXX-ServerName")
Before: Successfully connected to MP ASD-CM01
After: Successfully connected to MP XXX-ServerName

SCCM Site Code

1
2
3
$regex.Add("(CM1)","XXX")
Before: Action command line: smsswd.exe /run:CM100123 ServiceUI.exe -process:TSProgressUI.exe C:\Windows\System32\cmd.exe
After: Action command line: smsswd.exe /run:XXX00123 ServiceUI.exe -process:TSProgressUI.exe C:\Windows\System32\cmd.exe

URLs with domain names

1
2
3
$regex.Add("(CM1)","XXX")
Before: Action command line: smsswd.exe /run:CM100123 ServiceUI.exe -process:TSProgressUI.exe C:\Windows\System32\cmd.exe
After: Action command line: smsswd.exe /run:XXX00123 ServiceUI.exe -process:TSProgressUI.exe C:\Windows\System32\cmd.exe

Specific words unique to your company

1
2
3
$regex.Add("(SecretProductName|SecretProductName2)","XXXXXXXXX")
Before: Parsing step node: 1.0 Launch SecretProductName1 UDI Wizard
Parsing step node: 1.0 Launch XXXXXXXXX UDI Wizard
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#Be sure to test your regular expressions.  There are several online test tools available.
#I used this one and it worked very well http://regexstorm.net/tester
#Regex Reference
#https://www.zerrouki.com/powershell-cheatsheet-regular-expressions/

##############################################################################################################################################
#Modify these entries to match your site's needs. So far, these 4 are all I need, but there may be others.
#Format is (RegexValue, ReplacementValue)
$regex = @{}
$regex.Add("ASD\w*-\w+","XXX-ServerName") #Replace server names following format <CompanyPrefix>*-*
$regex.Add("(CM1)","XXX") #Replace all instances of your configmgr site prefix, mostly seein in package names
$regex.Add("(\.\w?\w?\.|\.)asquaredozen.com",".XXXXXXX.com") #Replace all domain urls matching .*.<mydomain.com> or .<mydomain.com>
$regex.Add("(SecretProductName|SecretProductName2)","XXXXXXXXX") #Replace any other exact match words - like internal product names and such.
##############################################################################################################################################

#Location of this script.
set-location $PSScriptRoot 

#Path to the log files.  Default location is the same folder as this script.
$logfiles = Get-ChildItem "$PSScriptRoot\*.log" 

Write-Host
forEach ($log in $logfiles){
    Write-Host -f green "Parsing $($log)"
    $Content = (Get-Content $log)
    foreach ($rex in $regex.keys)
    {
        $Content = $Content -ireplace "$($rex)","$($regex[$rex])" #Use ireplace for case insensetive replace
    }
     #Files are saved with "_parsed appended to the end to preserve your originals.
     $parsedLogName = $log.Name.Split('.')[0] + "_parsed." + $log.Name.Split('.')[-1]
     $content | Set-Content $parsedLogName
     
     Write-Host -f green "Parsing Completed for $($log)."
}

I’ve added the file to my GitHub repository (first one yay!!) here: https://github.com/AdamGrossTX/PowershellScripts/blob/master/PSLogSanitizer.ps1.

Let me know if you have any suggestions for improvements.