擴展雙引號 Powershell 字串中的物件屬性

擴展雙引號 Powershell 字串中的物件屬性

我對 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時,它無法辨識該表達。

相關內容