【问题标题】:Get Screen resolution using WMI/powershell in Windows 7在 Windows 7 中使用 WMI/powershell 获取屏幕分辨率
【发布时间】:2011-12-19 13:01:13
【问题描述】:

我正在使用以下脚本通过 WMI 在 Windows 中获取屏幕分辨率。该脚本在计算机处于横向模式时工作正常,但在纵向模式下返回不正确的值。在 XP 中可以正常工作,但在 Vista 中没有尝试过。谁能确认这是 Windows 7 WMI 中的错误。

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_DesktopMonitor",,48) 
For Each objItem in colItems 
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "Win32_DesktopMonitor instance"
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "ScreenHeight: " & objItem.ScreenHeight
    Wscript.Echo "ScreenWidth: " & objItem.ScreenWidth
Next

【问题讨论】:

    标签: windows-7 powershell wmi


    【解决方案1】:

    这是一个基于 Shays 的答案,它仅根据 OP 的示例格式化每个屏幕的结果。

    用于格式化结果的 PowerShell 代码:[System.Windows.Forms.Screen]::AllScreens

    Add-Type -AssemblyName System.Windows.Forms
    $screen_cnt  = [System.Windows.Forms.Screen]::AllScreens.Count
    $col_screens = [system.windows.forms.screen]::AllScreens
    
    $info_screens = ($col_screens | ForEach-Object {
    if ("$($_.Primary)" -eq "True") {$monitor_type = "Primary Monitor    "} else {$monitor_type = "Secondary Monitor  "}
    if ("$($_.Bounds.Width)" -gt "$($_.Bounds.Height)") {$monitor_orientation = "Landscape"} else {$monitor_orientation = "Portrait"}
    $monitor_type + "(Bounds)                          " + "$($_.Bounds)"
    $monitor_type + "(Primary)                         " + "$($_.Primary)"
    $monitor_type + "(Device Name)                     " + "$($_.DeviceName)"
    $monitor_type + "(Bounds Width x Bounds Height)    " + "$($_.Bounds.Width) x $($_.Bounds.Height) ($monitor_orientation)"
    $monitor_type + "(Bits Per Pixel)                  " + "$($_.BitsPerPixel)"
    $monitor_type + "(Working Area)                    " + "$($_.WorkingArea)"
    }
    )
    
    Write-Host "TOTAL SCREEN COUNT: $screen_cnt"
    $info_screens
    

    横向模式下辅助监视器的输出。 1920 x 1200

    # TOTAL SCREEN COUNT: 2
    # Primary Monitor    (Bounds)                          {X=0,Y=0,Width=2560,Height=1600}
    # Primary Monitor    (Primary)                         True
    # Primary Monitor    (Device Name)                     \\.\DISPLAY1
    # Primary Monitor    (Bounds Width x Bounds Height)    2560 x 1600 (Landscape)
    # Primary Monitor    (Bits Per Pixel)                  32
    # Primary Monitor    (Working Area)                    {X=0,Y=0,Width=2560,Height=1560}
    # Secondary Monitor  (Bounds)                          {X=2560,Y=0,Width=1920,Height=1200}
    # Secondary Monitor  (Primary)                         False
    # Secondary Monitor  (Device Name)                     \\.\DISPLAY2
    # Secondary Monitor  (Bounds Width x Bounds Height)    1920 x 1200 (Landscape)
    # Secondary Monitor  (Bits Per Pixel)                  32
    # Secondary Monitor  (Working Area)                    {X=2560,Y=0,Width=1920,Height=1160}
    

    纵向模式下辅助监视器的输出。 1200 x 1920

    # TOTAL SCREEN COUNT: 2
    # Primary Monitor    (Bounds)                          {X=0,Y=0,Width=2560,Height=1600}
    # Primary Monitor    (Primary)                         True
    # Primary Monitor    (Device Name)                     \\.\DISPLAY1
    # Primary Monitor    (Bounds Width x Bounds Height)    2560 x 1600 (Landscape)
    # Primary Monitor    (Bits Per Pixel)                  32
    # Primary Monitor    (Working Area)                    {X=0,Y=0,Width=2560,Height=1560}
    # Secondary Monitor  (Bounds)                          {X=2560,Y=0,Width=1200,Height=1920}
    # Secondary Monitor  (Primary)                         False
    # Secondary Monitor  (Device Name)                     \\.\DISPLAY2
    # Secondary Monitor  (Bounds Width x Bounds Height)    1200 x 1920 (Portrait)
    # Secondary Monitor  (Bits Per Pixel)                  32
    # Secondary Monitor  (Working Area)                    {X=2560,Y=0,Width=1200,Height=1880}
    

    【讨论】:

      【解决方案2】:

      @Shay Levy 上面的回答准确地报告了启动 powershell 会话时处于活动状态的宽度/高度。如果您在 PS 启动后旋转显示器,它会继续报告原来的、现在不正确的值。

      SystemInformation 类提供了另一种获取方向的方法,它会在当前 PS 会话中发生变化,即使在会话启动后旋转显示也是如此。

      Add-Type -AssemblyName System.Windows.Forms
      [System.Windows.Forms.SystemInformation]::ScreenOrientation
      Angle0
      
      [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize
      IsEmpty                            Width                           Height
      -------                            -----                           ------
      False                              1680                             1050
      

      旋转监视器,然后...

      [System.Windows.Forms.SystemInformation]::ScreenOrientation
      Angle90
      
      [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize
      IsEmpty                            Width                           Height
      -------                            -----                           ------
      False                              1050                             1680
      

      https://msdn.microsoft.com/en-us/library/system.windows.forms.systeminformation(v=vs.110).aspx

      【讨论】:

        【解决方案3】:

        与其他答案相同,但对于普通 cmd:

        wmic path Win32_VideoController get VideoModeDescription

        【讨论】:

          【解决方案4】:

          您可以从Win32_VideoController WMI 类中获取它。 VideoModeDescription 属性包括屏幕分辨率和颜色深度。

          (Get-WmiObject -Class Win32_VideoController).VideoModeDescription;
          

          结果

          1600 x 900 x 4294967296 colors
          

          【讨论】:

            【解决方案5】:

            作为记录,PowerShell代码是:

            Get-WmiObject -Class Win32_DesktopMonitor | Select-Object ScreenWidth,ScreenHeight
            

            我在横向或纵向模式下得到相同的值。

            更新:

            在多显示器环境中,您可以通过以下方式获取所有显示器的信息:

            PS> Add-Type -AssemblyName System.Windows.Forms
            PS> [System.Windows.Forms.Screen]::AllScreens
            
            
            BitsPerPixel : 32
            Bounds       : {X=0,Y=0,Width=1280,Height=800}
            DeviceName   : \\.\DISPLAY1
            Primary      : True
            WorkingArea  : {X=0,Y=0,Width=1280,Height=770}
            
            BitsPerPixel : 32
            Bounds       : {X=1280,Y=0,Width=1920,Height=1200}
            DeviceName   : \\.\DISPLAY2
            Primary      : False
            WorkingArea  : {X=1280,Y=0,Width=1920,Height=1170}
            

            【讨论】:

            • 这似乎只能找到第一台显示器。有没有办法获得所有显示器的分辨率?
            • 这里的第二种方法准确报告了启动powershell会话时处于活动状态的宽度高度。如果您在 PS 启动后旋转显示器,它会继续报告原来的、现在不正确的值。请参阅下面的答案,了解即使在显示器旋转后也可以在同一 PS 会话中使用的另一种方法。
            • 非常好的解决方案,关于更新(WMI 解决方案对我不起作用,Win 10)。不幸的是,您必须将绑定对象的宽度和高度乘以 dpi 比例因子(在 Windows 设置 > 系统 > 显示中进行配置),例如。 G。 1.25 为 125%。
            • ScreenWidthScreenHeight 中的第一个方法返回空。第二种方法完善
            • 你好@ShayLevy,是否有可能获得对所有这些监视器的引用并将一些应用程序窗口从一个移动到另一个?
            【解决方案6】:

            您可以使用此命令获得所有可用的分辨率:

            $Query = "SELECT * FROM CIM_VideoControllerResolution"
            $res = Get-WMIObject -query $Query | Select Caption
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2010-12-03
              • 1970-01-01
              • 2014-07-31
              • 1970-01-01
              相关资源
              最近更新 更多