带有变量的字符串包括命名空间和对象信息?

Strings with variables include namespace and object information?

# Locate Local HDD drive letter (100GB+)
#(Get-WmiObject -Query "SELECT * FROM Win32_LogicalDisk WHERE DriveType LIKE 3" | Sort-Object -Descending Size)[0] | Format-List *
$oLocalLargestHDD = (Get-WmiObject -Query "SELECT * FROM Win32_LogicalDisk WHERE DriveType LIKE 3" | Sort-Object -Descending Size)[0]
If ($oLocalLargestHDD.Size -gt 100000000)
{
    $sMsg = "HDD ( $oLocalLargestHDD.Name ) is found!"
    Write-Host $sMsg
} else {
    Write-Host "HDD is NOT found!"
}

输出:

HDD ( \BRIANG\root\cimv2:Win32_LogicalDisk.DeviceID="C:".Name ) is found!

而不是

HDD ( C: ) is found!

你没有正确地进行变量扩展。你需要有一个subexpression。你很接近。

$sMsg = "HDD ($($oLocalLargestHDD.Name)) is found!"

或者您可以使用 format operator

$sMsg = "HDD ({0}) is found!" -f $oLocalLargestHDD.Name

您看到的是 $oLocalLargestHDD 对象的字符串表示形式。使用上述任何一种方法都可以使用 属性。