functions/Test-TokenTransform.ps1
<#
.SYNOPSIS Test the tokens in the files can be applied .DESCRIPTION Test the tokens in the files can be applied and error on any missing matches .PARAMETER ConfigObject This is a hashtable of all the tokens to look for and the replacement .PARAMETER TargetFolder Location of what to test .PARAMETER FileFilter Which files to affect (defaults to *.config) .EXAMPLE Test-TokenTransform -ConfigObject @{"Test" = "Test";} -TargetFolder = "C:\Temp" .NOTES General notes #> function Test-TokenTransform { [CmdletBinding()] param( $ConfigObject, [Parameter(Mandatory)] $TargetFolder, $FileFilter = "*.config" ) begin { $tokenSearchPattern = "__(\w+)__" $configXmlObject = ConvertTo-Xml $ConfigObject -Depth 10 $missingToken = 0 if(!(Test-Path($TargetFolder))) { Write-Error "ERROR: Target could not be found"; } $files = Get-Files -TargetFolder $TargetFolder -fileFilter $FileFilter if (!($files)) { Write-Error "ERROR: No files given to test"; } } process { foreach ($file in $files) { $lineMatches = Select-string -Path $file -Pattern $tokenSearchPattern -AllMatches foreach ($lineMatch in $lineMatches) { foreach ($match in $lineMatch.Matches) { $token = $match.Groups[1].Value $tokenCount = $configXmlObject.SelectNodes("//Property[text() = '{0}']" -f $token).Count if ($tokenCount -eq 0) { $missingToken++ Write-Error "ERROR: Token '$token' in file '$file' has no occurences in ConfigObject provided" } } } } } end { $($missingToken -eq 0) } } |