0

这是显示有关连接到我的计算机的显示器信息的代码,它工作正常。

Get-WmiObject -Namespace root\wmi -Class WmiMonitorBasicDisplayParams |
select @{ N="Computer"; E={$_.__SERVER} }, InstanceName,
@{ N="Horizonal"; E={[System.Math]::Round(($_.MaxHorizontalImageSize/2.54), 2)} },
@{ N="Vertical"; E={[System.Math]::Round(($_.MaxVerticalImageSize/2.54), 2)} },
@{N="Size"; E={[System.Math]::Round(([System.Math]::Sqrt([System.Math]::Pow($_.MaxHorizontalImageSize, 2) + [System.Math]::Pow($_.MaxVerticalImageSize, 2))/2.54),2)} },
@{N="Ratio";E={[System.Math]::Round(($_.MaxHorizontalImageSize)/($_.MaxVerticalImageSize),2)} }

我需要的是根据连接的监视器数量将输出分离为监视器 1、监视器 2 和监视器 3。当前代码将输出以下内容:

Computer     : HOME-HP
InstanceName : DISPLAY\HWP3183\4&1badc1af&0&UID200195_0
Horizonal    : 18.9
Vertical     : 10.63
Size         : 21.68
Ratio        : 1.78

Computer     : HOME-HP
InstanceName : DISPLAY\HPN3394\4&1badc1af&0&UID224795_0
Horizonal    : 20.87
Vertical     : 11.81
Size         : 23.98
Ratio        : 1.77

但我想要的是下面

Computer     : HOME-HP
Monitor 1 InstanceName : DISPLAY\HWP3183\4&1badc1af&0&UID200195_0
Monitor 1 Horizonal    : 18.9
Monitor 1 Vertical     : 10.63
Monitor 1 Size         : 21.68
Monitor 1 Ratio        : 1.78

Computer     : HOME-HP
Monitor 2 InstanceName : DISPLAY\HPN3394\4&1badc1af&0&UID224795_0
Monitor 2 Horizonal    : 20.87
Monitor 2 Vertical     : 11.81
Monitor 2 Size         : 23.98
Monitor 2 Ratio        : 1.77
4

1 回答 1

0

简单的解决方案是遍历数组并格式化输出:

$Monitors = Get-WmiObject -Namespace root\wmi -Class WmiMonitorBasicDisplayParams

$Count = 0
ForEach($Monitor in $Monitors){
    $Count += 1
    $Monitor | select @{ N="Computer"; E={$_.__SERVER} }, `
    @{N="Monitor $Count InstanceName"; E={$_.InstanceName}},`
    @{N="Monitor $Count Horizonal"; E={[System.Math]::Round(($_.MaxHorizontalImageSize/2.54), 2)} }, `
    @{N="Monitor $Count Vertical"; E={[System.Math]::Round(($_.MaxVerticalImageSize/2.54), 2)} }, 
    @{N="Monitor $Count Size"; E={[System.Math]::Round(([System.Math]::Sqrt([System.Math]::Pow($_.MaxHorizontalImageSize, 2) + [System.Math]::Pow($_.MaxVerticalImageSize, 2))/2.54),2)} },`
    @{N="Monitor $Count Ratio";E={[System.Math]::Round(($_.MaxHorizontalImageSize)/($_.MaxVerticalImageSize),2)} }

}

编辑:

要添加制造周和序列号,您必须在 for 循环中加入信息:

$Monitors = Get-WmiObject -Namespace root\wmi -Class WmiMonitorBasicDisplayParams

$MonitorIDs = Get-WmiObject -Namespace root\wmi -Class WmiMonitorID

$Count = 0
ForEach($Monitor in $Monitors){
    $Count += 1

    $MonitorId = ($MonitorIDs | ?{$_.InstanceName -eq $Monitor.InstanceName})

    $Monitor | select @{ N="Computer"; E={$_.__SERVER} }, `
    @{N="Monitor $Count InstanceName"; E={$_.InstanceName}},`
    @{N="Monitor $Count Horizonal"; E={[System.Math]::Round(($_.MaxHorizontalImageSize/2.54), 2)} }, `
    @{N="Monitor $Count Vertical"; E={[System.Math]::Round(($_.MaxVerticalImageSize/2.54), 2)} }, 
    @{N="Monitor $Count Size"; E={[System.Math]::Round(([System.Math]::Sqrt([System.Math]::Pow($_.MaxHorizontalImageSize, 2) + [System.Math]::Pow($_.MaxVerticalImageSize, 2))/2.54),2)} },`
    @{N="Monitor $Count Ratio";E={[System.Math]::Round(($_.MaxHorizontalImageSize)/($_.MaxVerticalImageSize),2)} },`
    @{N="Monitor $Count WeekOfManufacture"; E={$MonitorId.WeekOfManufacture}},`
    @{N="Monitor $Count SerialNumberID"; E={$MonitorId.SerialNumberID}},`
    @{N="Monitor $Count SerialNumber"; E={[System.Text.Encoding]::ASCII.GetString($MonitorId.SerialNumberID)}}

}
于 2019-04-10T14:56:41.990 回答