我查看了下一个WinRM和PowerShell 3的最新版本,并查看了最新的更改列表,并看到了一些我以前从未见过的东西。
例子是:
$server = "msp42"
$status = "online"
"$server: $status"其结果是:
online
好吧,我以前从未遇到过这种情况,也不知道为什么冒号会引起问题。文档中建议的解决方案是放置一个空格(这很愚蠢,因为随后您更改了输出):
"$server : $status"另一个建议是使用这种格式(对我来说是新的!):
"${server}: $status"最后的建议是表达一种我一直都熟悉和使用的表达方式:
"$($server): $status"所以,我向你们PowerShell大师提出的问题是:
,
${variable}语法吗?${variable}语法是${variable}语法吗?它是严格处理冒号还是它有一些整洁的特性?发布于 2011-12-05 13:46:32
冒号是变量名的有效字符,例如在$Env:PATH中等等。
您也可以使用以下选项
$server`: $status或者,在某些情况下,格式字符串更具可读性:
'{0}: {1}' -f $server, $status回到结肠。对于与PSDrive上的项相对应的变量名称,有一种特殊情况:
$Env:Foo # equivalent to the contents of Env:\Foo
$Function:C: # equivalent to the contents of Function:\C:
${C:\autoexec.bat} # ... you get the picture语法${}的存在是为了能够指定变量名,否则将使用为语法的其他部分保留的字符。在标识符前面,您可以看到它与C#的@类似(但功能更强大)。请参见上面在变量名中使用\的地方,因为$Drive:Item只适用于驱动器上的当前容器(或者是Env、Alias或Function等非层次结构容器的根)。
另一个变量名通常为无效语法的示例:
PS> $+
The term '$+' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ $+
+ ~~
+ CategoryInfo : ObjectNotFound: ($+:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
PS> ${+} = 5
PS> ${+}
5
PS> Get-Variable +
Name Value
---- -----
+ 5https://stackoverflow.com/questions/8386219
复制相似问题