function ReplaceHtmlCharacterCodes {
param(
[string] $pattern
)
# Import the System.Web assembly to use the WebUtility class
Add-Type -AssemblyName System.Web
# Decode the captured text
$decodedText = [System.Web.HttpUtility]::HtmlDecode($pattern)
return $decodedText
}
$record = $record -replace 'amp;', '&'
$decodedrecord = $record | ForEach-Object {
if ($_ -match $pattern) {
$_ -replace $pattern, {
ReplaceHtmlCharacterCodes ($pattern) # Pass the pattern
}
} else {
$_
}
}
is giving me output like:
Loneliness is the most dangerous and least talked-about epidemic
ReplaceHtmlCharacterCodes ($pattern) # Pass the pattern
life-giving friendships? In Made for People, bestselling author and founder of The Common Rule Justin Whitmel Earley explains why we were made for friendships and how we can cultivate them in a technology-driven, post-pandemic world.
I want to escape these html characters but what I am getting is:
`ReplaceHtmlCharacterCodes ($pattern) # Pass the pattern`
The syntax
... -replace <pattern>,{ <substitution routine> }was not introduced until PowerShell 6.1. In previous versions, the scriptblock will simply be coerced to a string and used as a regular match substitution string, which is why you see your source code in the resulting string.To use a match evaluator in Windows PowerShell, use
[regex]::Replace()directly instead: