我对 Powershell 还不熟悉,在扩展双引号字符串中的对象属性值时遇到了问题。有人能给我解释一下吗:我希望你写“$(var.attr)”,但这会失败,下面是一个描述问题的示例:
PS C:\WINDOWS\system32> $c = Get-Volume -DriveLetter 'C'
PS C:\WINDOWS\system32> $c.Size
126696288256
PS C:\WINDOWS\system32> "$c.size"
MSFT_Volume (ObjectId = "{1}\\KEPPLAPTOP3\root/Microsoft/Windows...).size
PS C:\WINDOWS\system32> "$(c.size)"
c.size : The term 'c.size' 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:4
+ "$(c.size)"
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (c.size:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
如您所见,当不在双引号字符串中时,我可以轻松显示 $c 对象的 Size 属性,当我尝试在不使用括号的情况下进行扩展时,它只会扩展对象,但是当我在括号中包含 object.attr 时,它无法识别表达式。我在这里做错了什么?