Results output to txt file by group

2

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

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

Thanks in advance for everyone's reply.

powershell
asked on Stack Overflow Jul 16, 2018 by SS-WQY

1 Answer

2

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.

EDIT: incorporated mklement0's hint
EDIT2: added properties 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

Sample output:

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
answered on Stack Overflow Jul 16, 2018 by LotPings • edited Jul 16, 2018 by LotPings

User contributions licensed under CC BY-SA 3.0