Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 7 在Windows 7中使用WMI/powershell获取屏幕分辨率_Windows 7_Powershell_Wmi - Fatal编程技术网

Windows 7 在Windows 7中使用WMI/powershell获取屏幕分辨率

Windows 7 在Windows 7中使用WMI/powershell获取屏幕分辨率,windows-7,powershell,wmi,Windows 7,Powershell,Wmi,我正在使用以下脚本在Windows中使用WMI获得屏幕分辨率。当计算机处于横向模式时,脚本工作正常,但在纵向模式下返回不正确的值。在XP中正常工作,未在Vista中尝试。有人能确认这是Windows7WMI中的错误吗 strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") Set colItems = objWMIService.ExecQuery

我正在使用以下脚本在Windows中使用WMI获得屏幕分辨率。当计算机处于横向模式时,脚本工作正常,但在纵向模式下返回不正确的值。在XP中正常工作,未在Vista中尝试。有人能确认这是Windows7WMI中的错误吗

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

请注意,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}

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

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

您可以从
Win32\u VideoController
WMI类中获取此信息。
VideoModeDescription
属性包括屏幕分辨率和颜色深度

(Get-WmiObject -Class Win32_VideoController).VideoModeDescription;
结果
与其他答案相同,但是对于普通cmd:


wmic path Win32\u VideoController get VideoModeDescription

@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

以下是基于Shays的答案,它根据OPs的示例格式化每个屏幕的结果

用于格式化以下结果的PowerShell代码:[System.Windows.Forms.Screen]::AllScreens 横向模式下辅助监视器的输出。1920 x 1200 纵向模式下辅助监视器的输出。1200 x 1920
这似乎只找到了第一个监视器。是否有办法获得所有监视器的分辨率?这里的第二种方法准确报告启动powershell会话时处于活动状态的宽度高度。如果在PS启动后旋转监视器,它将继续报告原始的、现在不正确的值。请参阅下面的答案,了解在同一PS会话中工作的另一种方法,即使在监视器旋转后也是如此。非常好的解决方案,关于更新(WMI解决方案不适用于我,Win 10)。不幸的是,您必须将绑定对象的宽度和高度乘以dpi比例因子(在Windows设置>系统>显示中进行配置),例如。G第一个方法在
ScreenWidth
ScreenHeight
中返回空值。第二种方法是完美的
[System.Windows.Forms.SystemInformation]::ScreenOrientation
Angle90

[System.Windows.Forms.SystemInformation]::PrimaryMonitorSize
IsEmpty                            Width                           Height
-------                            -----                           ------
False                              1050                             1680
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
# 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}
# 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}