如何解析 Powershell 脚本块中的变量 [英] How to resolve variables in a Powershell script block

查看:25
本文介绍了如何解析 Powershell 脚本块中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于我有:

$a = "world"
$b = { write-host "hello $a" }

如何获取脚本块的解析文本,应该是包含 write-host 的 entre 字符串:

How do I get the resolved text of the script block, which should be the entre string including write-host:

write-host "hello world"

更新:补充说明

如果你只是打印 $b 你得到的是变量而不是解析值

If you just print $b you get the variable and not the resolved value

write-host "hello $a"

如果您使用 & 执行脚本块$b 你得到的是打印值,而不是脚本块的内容:

If you execute the script block with & $b you get the printed value, not the contents of the script block:

hello world

这个问题正在寻找一个字符串,其中包含带有评估变量的脚本块的内容,即:

This question is seeking a string containing the contents of the script block with the evaluated variables, which is:

write-host "hello world"

推荐答案

与原始问题一样,如果您的整个脚本块内容不是字符串(但您希望它是)并且您需要在脚本块中进行变量替换,您可以使用以下内容:

As in the original question, if your entire scriptblock contents is not a string (but you want it to be) and you need variable substitution within the scriptblock, you can use the following:

$ExecutionContext.InvokeCommand.ExpandString($b)

在当前执行上下文中调用 .InvokeCommand.ExpandString($b) 将使用当前作用域中的变量进行替换.

Calling .InvokeCommand.ExpandString($b) on the current execution context will use the variables in the current scope for substitution.

以下是创建脚本块并检索其内容的一种方法:

The following is one way to create a scripblock and retrieve its contents:

$a = "world"
$b = [ScriptBlock]::create("write-host hello $a")
$b

write-host hello world

您也可以使用脚本块符号 {} 来完成同样的事情,但您需要使用 & 调用运算符:

You can use your scriptblock notation {} as well to accomplish the same thing, but you need to use the & call operator:

$a = "world"
$b = {"write-host hello $a"}
& $b

write-host hello world

使用上述方法的一个特点是,如果您随时更改 $a 的值,然后再次调用脚本块,输出将像这样更新:

A feature to using the method above is that if you change the value of $a at any time and then call the scriptblock again, the output will be updated like so:

$a = "world"
$b = {"write-host hello $a"}
& $b
write-host hello world
$a = "hi"
& $b
write-host hello hi

GetNewClosure() 方法可用于创建上述脚本块的克隆,以获取脚本块当前评估的理论快照.它将不受代码之后 $a 值的更改的影响:

The GetNewClosure() method can be used to create a clone of the scriptblock above to take a theoretical snapshot of the scriptblock's current evaluation. It will be immune to the changing of the $a value later the code:

$b = {"write-host hello $a"}.GetNewClosure()
& $b
write-host hello world
$a = "new world"
& $b
write-host hello world

{} 表示法表示您可能已经知道的脚本块对象.这可以传递到 Invoke-Command 中,它会打开其他选项.您还可以在脚本块内创建可以稍后传入的参数.请参阅 about_Script_Blocks 了解更多信息.

The {} notation denotes a scriptblock object as you probably already know. That can be passed into Invoke-Command, which opens up other options. You can also create parameters inside of the scriptblock that can be passed in later. See about_Script_Blocks for more information.

这篇关于如何解析 Powershell 脚本块中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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