需求:编写PowerShell脚本清理邮件签名文件中的无效地址行
I’ve got you covered here. Since you can’t access the original signature generator’s source code, this PowerShell script will automatically remove those annoying leftover comma separators from your signature files every night. Here’s how to implement it:
Step 1: The PowerShell Script
This script scans your deployment folder, processes each signature file, and removes any lines that contain the invalid City/State comma separator (we’re targeting lines that are just a comma with optional whitespace—this is the common leftover when no location is assigned). It also creates a backup of each file before modifying it, just in case you need to revert:
# Set the path to your signature deployment folder $signatureFolder = "C:\Path\To\Your\Deployment\Signatures" # Define the invalid separator pattern (adjust if your leftover is slightly different) $invalidPattern = '^\s*,\s*$' # Get all signature files (adjust extensions if you use .txt or other formats) $signatureFiles = Get-ChildItem -Path $signatureFolder -Filter "*.htm" -Recurse foreach ($file in $signatureFiles) { # Create a backup of the original file $backupPath = "$($file.FullName).bak" Copy-Item -Path $file.FullName -Destination $backupPath -Force # Read the file content, filter out invalid lines, and write back $cleanContent = Get-Content -Path $file.FullName -Raw | ForEach-Object { $_ -split "`n" | Where-Object { $_ -notmatch $invalidPattern } } -join "`n" Set-Content -Path $file.FullName -Value $cleanContent -Encoding UTF8 Write-Host "Cleaned: $($file.FullName) | Backup saved to: $backupPath" }
Quick adjustments for your setup:
- Update
$signatureFolderto match your actual deployment folder path. - If your signatures use a different file extension (like
.txt), change the-Filterparameter to match. - Tweak
$invalidPatternif your leftover separator looks different—for example, use',\s*'to target any line containing a comma followed by whitespace, not just lines that are only the separator.
Step 2: Schedule the Script to Run Nightly
To make this run automatically every night, set up a Windows Task Scheduler task:
- Open Task Scheduler and click Create Basic Task.
- Name it something like "Clean Signature Files" and add a brief description.
- Choose Daily as the trigger, then set the time you want it to run (e.g., 2:00 AM when traffic is low).
- For the action, select Start a program.
- In "Program/script", enter
powershell.exe. - In "Add arguments", input
-ExecutionPolicy Bypass -File "C:\Path\To\Your\Script\CleanSignatures.ps1"(replace with your script’s full file path). - Finish the wizard, and ensure the task runs with an account that has read/write permissions to the signature deployment folder.
This setup will keep your signature files clean overnight, so users get valid, separator-free signatures when they sync.
内容的提问来源于stack exchange,提问作者user3195770

