如何延迟PowerShell字符串中变量的扩展? [英] How do I delay expansion of variables in PowerShell strings?

查看:72
本文介绍了如何延迟PowerShell字符串中变量的扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论您要调用什么名称,我都在尝试找出一种方法来获取现有字符串的内容并将其评估为双引号字符串.例如,如果我创建以下字符串:

Whatever you want to call it, I'm trying to figure out a way to take the contents of an existing string and evaluate them as a double-quoted string. For example, if I create the following strings:

$string = 'The $animal says "meow"'
$animal = 'cat'

然后,Write-Host $string将产生The $animal says "meow".我如何重新评估$ string以输出(或分配给新变量)The cat says "meow"?

Then, Write-Host $string would produce The $animal says "meow". How can I have $string re-evaluated, to output (or assign to a new variable) The cat says "meow"?

多么令人讨厌...注释的局限性使得很难(如果可能的话)在反引号中包含代码.这是我对以下zdan的最后两个评论的完整版本:

How annoying...the limitations on comments makes it very difficult (if it's even possible) to include code with backticks. Here's an unmangled version of the last two comments I made in response to zdan below:

实际上,在考虑之后,我意识到期望The $animal says "meow"在不转义双引号的情况下进行插值是不合理的,因为如果以双引号开头的字符串开头,则如果双精度字符串会导致评估失败引号没有逃脱.所以我想答案是,这是一个两步过程:

Actually, after thinking about it, I realized that it's not reasonable to expect The $animal says "meow" to be interpolated without escaping the double quotes, because if it were a double-quoted string to begin with, the evaluation would break if the double quotes weren't escaped. So I suppose the answer would be that it's a two step process:

$newstring = $string -replace '"', '`"'
iex "`"$string`""

关于后代的最后一条评论:我尝试了将所有内容统一显示的方法,一旦将其输入到iex中,几乎所有您认为可行的方法都会中断,但是这一方法有效:

One final comment for posterity: I experimented with ways of getting that all on one line, and almost anything that you'd think works breaks once you feed it to iex, but this one works:

iex ('"' + ($string -replace '"', '`"') + '"')

推荐答案

您可以使用Invoke-Expression来重新解析您的字符串-像这样:

You could use Invoke-Expression to have your string reparsed - something like this:

$string = 'The $animal says `"meow`"'
$animal = 'cat'
Invoke-Expression "Write-Host `"$string`""

请注意,必须如何在字符串中转义双引号(使用反引号),以免使解析器混乱.这包括原始字符串中的所有双引号.

Note how you have to escape the double quotes (using a backtick) inside your string to avoid confusing the parser. This includes any double quotes in the original string.

还请注意,第一个命令应该是一个命令,如果您需要使用结果字符串,只需使用write-output传递输出并将其分配给稍后可以使用的变量:

Also note that the first command should be a command, if you need to use the resulting string, just pipe the output using write-output and assign that to a variable you can use later:

$result = Invoke-Expression "write-output `"$string`""

如您的注释中所述,如果您无法修改字符串的创建以转义双引号,则您必须自己执行此操作.您也可以将其包装在一个函数中,以使其看起来更清晰:

As noted in your comments, if you can't modify the creation of the string to escape the double quotes, you will have to do this yourself. You can also wrap this in a function to make it look a little clearer:

function Invoke-String($str) { 
    $escapedString =  $str -replace '"', '`"'
    Invoke-Expression "Write-Output `"$escapedString`""
}

所以现在看起来像这样:

So now it would look like this:

# ~> $string = 'The $animal says "meow"'
# ~> $animal = 'cat'
# ~> Invoke-String $string
The cat says "meow"

这篇关于如何延迟PowerShell字符串中变量的扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆