与Powershell IDE相比,Powershell.exe中的结果不同

问题描述 投票:1回答:1

知道为什么会这样吗?我正在尝试构建一个包含我想要的所有信息的字符串(最终将在GUI中输出,在DataGridView单元格中),在Powershell.exe中,mediatype和healthstatus显示为代码(3,4,0) )而不是IDE中显示的字符串(HDD,SSD,OK):

两者中的代码完全相同:

$DisquesPhysiques = invoke-command -ComputerName XXXX -ScriptBlock {get-physicaldisk}

foreach ($disque in $DisquesPhysiques) {
$infodisque = $($disque.FriendlyName) + " - " + $($disque.SerialNumber)+ " - "  + $($disque.MediaType)+ " - "  + $($disque.HealthStatus)+ " - "  + $([math]::Round($disque.Size/1gb))  + " GB"
$infodisque
}

enter image description here

WEIRDEST部分有时它不会显示代码,它会显示实际的单词(SDD,HDD,Healthy)。这是非常有问题的,因为我不能一直依赖它是一个数字或一个词!

powershell ide difference
1个回答
0
投票

这很奇怪,所以我最终从CIM(WMI)获取信息并在媒体类型上执行IF(4是SDD):

$DisquesPhysiques = Get-CimInstance -ComputerName $poste MSFT_PhysicalDisk -Namespace Root\Microsoft\Windows\Storage

                foreach ($disque in $DisquesPhysiques)
                {
                    if ($disque.MediaType -eq 4) { $type = "SSD" }
                    else { $type = "HDD" }
                    $infodisque = $($disque.FriendlyName) + " - " + $($disque.SerialNumber) + " - " + $type + " - " + $([math]::Round($disque.Size/1gb)) + " GB"
                    $tableInfoPoste.Rows.add("Disque physique", $infodisque)
                }
© www.soinside.com 2019 - 2024. All rights reserved.