我试图在Powershell中替换部分字符串.但是,替换字符串不是硬编码的,它是根据函数计算的:
$text = "the image is -12345-"
$text = $text -replace "-(\d*)-", 'This is the image: $1'
Write-Host $text
Run Code Online (Sandbox Code Playgroud)
这给了我正确的结果:"这是图像:12345"
现在,我想要包含base64编码的图像.我可以从id读取图像.我希望以下内容可行,但它不会:
function Get-Base64($path)
{
[convert]::ToBase64String((get-content $path -encoding byte))
}
$text -replace "-(\d*)-", "This is the image: $(Get-Base64 '$1')"
Run Code Online (Sandbox Code Playgroud)
它不起作用的原因是因为它首先将$1(字符串,而不是值$1)传递给函数,执行它然后才进行替换.我想做的是