结果按组输出到txt文件

问题描述:

我目前正在研究 Powershell 并正在编写一个脚本,该脚本从 Windows 系统中获取多显示器显示配置.感谢其他人的帮助,我终于制定了一个完整的脚本来显示每台显示器的分辨率、视频输出类别、制造商和型号.这是完整的脚本:

I'm currently studying Powershell and working on a script that grabs multi-monitors Display configuration from windows system. Thanks to the help from other guys, I finally work out a full script to show each monitor's resolution, Video output category, Manufacturer, and Model. Here is the full script:

add-type -assemblyName system.windows.forms 
[system.windows.forms.screen]::AllScreens.Bounds | format-list Width,Height | 
out-file -append system1.txt

enum VideoOutputTechnology {
UNINITIALIZED                    = -2
UNKNOW                           = -1
VGA                              = 0
S_VIDEO                          = 1
COMPOSITE_VIDEO                  = 2
COMPONENT_VIDEO                  = 3
DVI                              = 4
HDMI                             = 5
LVDS_OR_MIPI_DSI                 = 6
D_JPN                            = 8
SDI                              = 9
DISPLAYPORT_EXTERNAL             = 10
DISPLAYPORT_EMBEDDED             = 11
UDI_EXTERNAL                     = 12
UDI_EMBEDDED                     = 13
DONGLE_CABLE_THAT_SUPPORTS_SDTV  = 14
MIRACAST_CONNECTED_SESSION       = 15
INTERNAL_CONNECTION              = 0x80000000
}
Get-WmiObject WmiMonitorconnectionparams -Namespace root\wmi | 
Format-List @{ 
n='VideoOutputTechnology'
e={ [VideoOutputTechnology] $_.VideoOutputTechnology } 
} >> system1.txt

Get-WmiObject WmiMonitorID -Namespace root\wmi | ForEach-Object {
 [PSCustomObject]@{
     Manufacturer = ($_.ManufacturerName | ForEach {[char]$_}) -join ""
     Model        = ($_.UserFriendlyName | ForEach {[char]$_}) -join ""
 }
} | Format-List | Out-File -append system1.txt

system1.txt 中的输出为:

The output in system1.txt is:

Width  : 1920
Height : 1080

Width  : 2560
Height : 1440





VideoOutputTechnology : DISPLAYPORT_EXTERNAL

VideoOutputTechnology : DVI





Manufacturer : ACI             
Model        : ASUS PB287Q  

Manufacturer : SAM             
Model        : SMS27A850  

我的问题是:如何按每个监视器制作输出结果组.例如:

My question is: How could I make the output results group by each monitor. For example:

Manufacturer: ACI             
Model: ASUS PB287Q  
Width: 1920
Height: 1080
VideoOutputTechnology: DISPLAYPORT_EXTERNAL

Manufacturer: SAM             
Model: SMS27A850    
Width: 2560
Height: 1440
VideoOutputTechnology: DVI

先谢谢大家的回复.

将部分结果放入变量中,并在一个 [PSCustomObject]
(我不太确定屏幕顺序是否与 MonitorID 顺序匹配)
WmiMonitorIDWmiMonitorconnectionparams 共有的属性 InstanceName 用于同步设置.

Put the partial results into variables and assemble all in one [PSCustomObject]
(I'm not quite sure if the order of the screens will match the MonitorID order)
The property InstanceName which is common to both WmiMonitorID and WmiMonitorconnectionparams is used to sync the settings.

合并 mklement0的提示
添加属性 ProductCodeID、SerialNumberID、Manufactured(year,week)

## Q:\Test\2018\07\16\SO_51368476.ps1
## 

enum VideoOutputTechnology {
UNINITIALIZED                    = -2
UNKNOW                           = -1
VGA                              = 0
S_VIDEO                          = 1
COMPOSITE_VIDEO                  = 2
COMPONENT_VIDEO                  = 3
DVI                              = 4
HDMI                             = 5
LVDS_OR_MIPI_DSI                 = 6
D_JPN                            = 8
SDI                              = 9
DISPLAYPORT_EXTERNAL             = 10
DISPLAYPORT_EMBEDDED             = 11
UDI_EXTERNAL                     = 12
UDI_EMBEDDED                     = 13
DONGLE_CABLE_THAT_SUPPORTS_SDTV  = 14
MIRACAST_CONNECTED_SESSION       = 15
INTERNAL_CONNECTION              = 0x80000000
}

Add-Type -AssemblyName system.windows.forms
$Screens = [system.windows.forms.screen]::AllScreens.Bounds

$MIDs = Get-WmiObject WmiMonitorID -Namespace root\wmi

$i=0
$AllMonInfo = ForEach ($MID in $MIDs){
    $MCP = (Get-WmiObject WmiMonitorconnectionparams -Namespace root\wmi |
        Where-Object InstanceName -eq $MID.InstanceName)
    [PSCustomObject]@{
        Manufacturer = (-join [char[]] $MID.ManufacturerName)
        Model        = (-join [char[]] $MID.UserFriendlyName)
        ProductCodeID= (-join [char[]] $MID.ProductCodeID)
        SerialNumberID=(-join [char[]] $MID.SerialNumberID)
        Manufactured = ("{0}W{1}" -f $MID.YearOfManufacture,$Mid.WeekOfManufacture.ToString('00'))
        Width        = $Screens[$i].Width
        Height       = $Screens[$i].Height
        VideoOutput  = ([VideoOutputTechnology]$MCP.VideoOutputTechnology)
    }
    $i++
} 

$AllMonInfo | Format-List | Out-File '.\system1.txt' -Append -Encoding ascii

示例输出:

Get-Content '.\system1.txt'

Manufacturer   : SAM
Model          : SyncMaster
ProductCodeID  : 03E7
SerialNumberID : H9XQxxxxxx
Manufactured   : 2008W40
Width          : 1920
Height         : 1200
VideoOutput    : DVI

Manufacturer   : SAM
Model          : SyncMaster
ProductCodeID  : 0425
SerialNumberID : H1AKxxxxxx
Manufactured   : 2008W10
Width          : 1920
Height         : 1200
VideoOutput    : DVI