分开监视器显示信息输出

时间:2019-04-10 14:29:33

标签: powershell multiple-monitors get-wmiobject

这是显示与我的计算机连接的显示器的信息的代码,该代码可以正常工作。

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

1 个答案:

答案 0 :(得分: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)}}

}