# ChkQuotes.ps1 - Check source code for unbalanced quotes. # Copyright (c) 2024 - Richard L. Mueller # Version 1.0 - May 1, 2024 # --------------------------------------------------------------------- # Instances of unbalanced quotes can be difficult to find. This script # flags all lines with an odd number of quote characters. A few quoted # strings might continue on the next line, and both lines will be # flagged. But these cases are easily recognized as valid. This script # can be run on any source code to find unbalanced quotes, including # PowerShell, VBScript, Visual Basic, HTML, and PHP. # --------------------------------------------------------------------- # # You have a royalty-free right to use, modify, reproduce, and # distribute this script file in any way you find useful, provided that # you agree that the copyright owner above has no warranty, obligations, # or liability for such use. $Version = "Version 1.0 - May 1, 2024" $Copyright = "Copyright (c) 2024 - Richard L. Mueller" $ScriptName = "ChkQuotes.ps1" # Retrieve the filename if provided on the command line. $Cnt = $Args.Count Do { If ($Cnt -eq 1) { $FileName = $Args[0] } ElseIf ($Cnt -eq 0) { # No valid filename provided. # Request the name of the source code file to be analyzed. $FileName = Read-Host "Enter the file name with the source code, such as Example.ps1" } ElseIf ($Cnt -gt 1) { Write-Host -ForegroundColor Red "Too many parameters. Script aborted." Exit } If (($Cnt -eq 0) -Or ($Cnt -eq 1)) { # Make sure the file exists. If (Test-Path -Path $FileName) {$Err = $False} Else { Write-Host -ForegroundColor Red "The file $FileName cannot be found" Write-Host -ForegroundColor Red "If the file is not in the current directory," Write-Host -ForegroundColor Red "include the path." $Cnt = 0 $Err = $True } } } Until ($Err -eq $False) # Find the file extension and remove. $Parts = $FileName.Split(".") $File = $Parts[0] # Check for more than one decimal point. If ($Parts.Count -gt 2) { Write-Host -ForegroundColor Red "Too many decimal points in the filename." Write-Host -ForegroundColor Red "Script aborted." Exit } # Check if the extension is "rpt". If (($Parts.Count -gt 1) -And ($Parts[1] -eq "rpt")) { Write-Host -ForegroundColor Green "The file extension is `"rpt`"." Write-Host -ForegroundColor Green "The report file would replace this file." Write-Host -ForegroundColor Green "Rename the source code file with a different" Write-Host -ForegroundColor Green "extension and run this script again." Write-Host -ForegroundColor Green "Script aborted." Exit } # Setup the report file. $ReportFile = $File + ".rpt" # Test for existence of the report file. If (Test-Path -Path $ReportFile) { Do { $Err = $False Write-Host "The report file $ReportFile already exists." -foregroundcolor red -backgroundcolor black [String]$Ans = Read-Host "Enter ""Y"" to delete the file and have it recreated. Enter ""N"" to append the report to the file." If ($Ans -eq "Y") {Remove-Item $ReportFile} ElseIf ($Ans -eq "N") {Write-Host "The new report will be appended to the file $ReportFile"} Else {$Err = $True} } Until ($Err -eq $False) } # Boolean to determine if any unbalanced quotes were detected. # $UnBalQ is $False if there are no unbalanced quotes. # $UnBalQ is $True if any unbalanced quotes found. $UnBalQ = $False # Abort the script if the file cannot be created and written to. Add-Content -Path $ReportFile -Value "------------------------------------------------" -ErrorAction Stop Add-Content -Path $ReportFile -Value "PowerShell script: $ScriptName" Add-Content -Path $ReportFile -Value "Check source code for unbalanced quotes" Add-Content -Path $ReportFile -Value "$Version" Add-Content -Path $ReportFile -Value "$Copyright" Add-Content -Path $ReportFile -Value "Check file $FileName for unbalanced quotes" Add-Content -Path $ReportFile -Value $("Started: " + (Get-Date).ToString()) Add-Content -Path $ReportFile -Value "Report file: $ReportFile" Add-Content -Path $ReportFile -Value "------------------------------------------------" # Keep track of line numbers. $Num = 0 # Read the file. $Code = Get-Content -Path $FileName ForEach ($Line In $Code) { $Num = $Num + 1 # Break up the line, delimited by the quote character, into an array of strings. $Quotes = $Line.Split("`"") # Count the number of substrings in the array. [Int]$Count = $Quotes.Count # Determine if $Count is even, in which case the number of quote characters is odd. [Int]$k = $Count / 2 $k = 2 * $k If ($Count -eq $k) { # $Count is even, so the number of quote characters is odd (unbalanced). Write-Host -ForegroundColor Red "Line $Num has unbalanced quotes" Add-Content -Path $ReportFile -Value "Line $Num has unbalanced quotes" Write-Host -ForegroundColor Red " " $Line Add-Content -Path $ReportFile -Value " $Line" $UnBalQ = $True } } If ($UnBalQ -eq $False) { Write-Host -ForegroundColor Green "No unbalanced quote characters found." Add-Content -Path $ReportFile -Value "No unbalanced quote characters found." } Write-Host -ForegroundColor Green "See the report in file $ReportFile"