传递给 Powershell 中的函数时如何在参数中添加引号
How to add quotes in an argument when passing to a function in Powershell
我需要在参数中添加 '"$third'" 这些引号以获得正确的结果。
下面是我的代码
$first = Function-xyz $second $third
在这里,如果我们可以添加 '"$third"' 并传递参数,它将对我有用,但可以通过以下方法做到这一点
$first = Function-xyz $second '"$third"'
有了这个,它只是将 third 本身作为字符串传递,而不读取它的值。
欢迎。
我会这样做:
$third = "Some Text"
Return '"{0}"' -f $third
在函数结束时。这将扩展 Third: "Some text"
的值
如果你这样做:
$Third = '$Second'
Return '"{0}"' -f $third
结果为:“$Second”
如果你这样做:
$Second = 2
$Third = "$Second"
Return '"{0}"' -f $third
结果为:2
我需要在参数中添加 '"$third'" 这些引号以获得正确的结果。 下面是我的代码
$first = Function-xyz $second $third
在这里,如果我们可以添加 '"$third"' 并传递参数,它将对我有用,但可以通过以下方法做到这一点
$first = Function-xyz $second '"$third"'
有了这个,它只是将 third 本身作为字符串传递,而不读取它的值。
欢迎。
我会这样做:
$third = "Some Text"
Return '"{0}"' -f $third
在函数结束时。这将扩展 Third: "Some text"
的值如果你这样做:
$Third = '$Second'
Return '"{0}"' -f $third
结果为:“$Second”
如果你这样做:
$Second = 2
$Third = "$Second"
Return '"{0}"' -f $third
结果为:2