在 Windows 7 中使用 WMI/powershell 获取屏幕分辨率
我正在使用以下脚本通过 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
I am using the following script to get screen resolution in Windows using WMI. The script works fine when the computer is in landscape mode but returns incorrect values when in portrait mode. Works properly in XP and did not try in Vista. Can anyone confirm this is bug in 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
根据记录,PowerShell 代码为:
我在横向或纵向模式下得到相同的值。
更新:
在多显示器环境中,您可以通过以下方式获取所有显示器的信息:
For the record, the PowerShell code is:
I get the same values in Landscape or in Portrait mode.
UPDATE:
In a multi monitor environment you can get the info for all monitors with:
您可以从
Win32_VideoController
WMI 类中获取它。VideoModeDescription
属性包括屏幕分辨率和颜色深度。结果
You can grab this from the
Win32_VideoController
WMI class. TheVideoModeDescription
property includes the screen resolution and the color depth.Result
与其他答案相同,但对于普通 cmd:
wmic path Win32_VideoController get VideoModeDescription
Same as the other answers, however for the plain cmd:
wmic path Win32_VideoController get VideoModeDescription
这是基于 Shays 的答案,仅根据 OP 的示例格式化每个屏幕的结果。
用于格式化结果的 PowerShell 代码:
[System.Windows.Forms.Screen]::AllScreens
横向模式下辅助显示器的输出。 1920 x 1200
纵向模式下辅助显示器的 输出。 1200×1920
Here's an answer based on Shays only it formats the results for each screen as per the OPs' example.
PowerShell Code to format the results of:
[System.Windows.Forms.Screen]::AllScreens
Output for the secondary monitor in landscape mode. 1920 x 1200
Output for the secondary monitor in portrait mode. 1200 x 1920
@Shay Levy 的上述答案准确地报告了 powershell 会话启动时处于活动状态的宽度/高度。如果您在 PS 启动后旋转显示器,它会继续报告原来的、现在不正确的值。
SystemInformation 类提供了另一种获取方向的方法,即使在会话启动后显示旋转,它也会在当前 PS 会话中发生变化。
旋转显示器,然后...
https://msdn.microsoft.com/en-us/library/system.windows.forms.systeminformation(v=vs.110).aspx
@Shay Levy's answer above accurately reports the Width/Height that was active when the powershell session was launched. If you rotate monitor after PS launch, it continues to report the original, now incorrect values.
The SystemInformation class provides another way to get orientation, and it changes in the current PS session even if the display is rotated after the session launch.
Rotate monitor, then...
https://msdn.microsoft.com/en-us/library/system.windows.forms.systeminformation(v=vs.110).aspx
您可以使用以下命令获取所有可用的分辨率:
You can get all available resolution with this command:
简而言之,这将分别为您提供第一个屏幕(如果有很多)的宽度和高度
For Short, this gets you the first screen (if you have many) width and height separately