哈希值不在 powershell 中注入其他变量的值

Hash value not injecting other variable's value in powershell

我在 powershell 中有一个如下所示的哈希表

 $children = @{'key'='value\$child\Parameters'}
 

现在考虑以下内容:

$child = "hey"
$parent = "$child ho"

Write-Host $parent

这会打印

hey ho

所以基本上字符串 $parent 是可以使用字符串 $child 定义的。 当从哈希表的值中期望相同的行为时,这不会发生。例如:

$child = "hey"
$children = @{'key'='value\$child\Parameters'}

$children.GetEnumerator() | ForEach-Object {
$param = $_.Value
Write-Host $param
} 

这会打印

 value\$child\Parameters 

而不是使用 $child 和打印 value\hey\Parameters 我认为这可能是因为类型,所以我尝试使用 | Out-String|% ToString$_.Value 转换为字符串,但它仍然不起作用。 有什么方法可以利用 $_.Value 并仍然注入 $child 值?

抱歉,我的词汇比 powershell 更 java。

这里的问题是引号。如果将单引号更改为双引号,您可以看到插值将按预期工作。

$child = "hey"
$children = @{"key"="value\$child\Parameters"}

这里是 documentation 中解释这种行为的相关部分。

Single-quoted strings

A string enclosed in single quotation marks is a verbatim string. The string is passed to the command exactly as you type it. No substitution is performed.

Double-quoted strings

A string enclosed in double quotation marks is an expandable string. Variable names preceded by a dollar sign ($) are replaced with the variable's value before the string is passed to the command for processing.