【问题标题】:Results output to txt file by group结果按组输出到txt文件
【发布时间】:2018-07-16 18:57:02
【问题描述】:

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

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 中的输出为:

Width  : 1920
Height : 1080

Width  : 2560
Height : 1440





VideoOutputTechnology : DISPLAYPORT_EXTERNAL

VideoOutputTechnology : DVI





Manufacturer : ACI             
Model        : ASUS PB287Q  

Manufacturer : SAM             
Model        : SMS27A850  

我的问题是:如何按每个监视器对输出结果进行分组。 例如:

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

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

提前感谢大家的回复。

【问题讨论】:

    标签: powershell


    【解决方案1】:

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

    编辑:合并 mklement0 的提示
    EDIT2:添加属性 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
    

    【讨论】:

      猜你喜欢
      • 2016-05-06
      • 1970-01-01
      • 1970-01-01
      • 2018-11-27
      • 2020-06-15
      • 2016-07-02
      • 1970-01-01
      • 2020-03-29
      • 2014-09-03
      相关资源
      最近更新 更多