programing

Windows 7에서 WMI/powershell을 사용하여 화면 해상도 가져오기

jooyons 2023. 10. 22. 20:02
반응형

Windows 7에서 WMI/powershell을 사용하여 화면 해상도 가져오기

저는 WMI를 이용하여 윈도우에서 화면 해상도를 얻기 위해 다음 스크립트를 사용하고 있습니다.컴퓨터가 가로 모드일 때는 스크립트가 정상적으로 작동하지만 세로 모드일 때는 잘못된 값을 반환합니다.XP에서는 제대로 작동하고 Vista에서는 시도하지 않았습니다.윈도우 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

기록상 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}

이걸 가지고 갈 수 있어요.Win32_VideoControllerWMI 클래스. 그VideoModeDescription속성은 화면 해상도와 색 깊이를 포함합니다.

(Get-WmiObject -Class Win32_VideoController).VideoModeDescription;

결과

1600 x 900 x 4294967296 colors

그러나 일반 cmd에 대해서는 다른 답변과 동일합니다.

wmic path Win32_VideoController get VideoModeDescription

@Shay Levy의 위 답변은 파워셸 세션이 시작될 때 활성화된 Width/Height를 정확하게 보고합니다.PS를 실행한 후 모니터를 회전하면 원래 값이 잘못되었음을 계속 보고합니다.

System Information 클래스는 방향 설정을 위한 또 다른 방법을 제공하며, 세션 시작 후 디스플레이가 회전하더라도 현재 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

다음은 OP의 예에 따라 각 화면에 대한 결과를 포맷하는 Shays만을 기반으로 한 답변입니다.

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}

다음 명령으로 사용 가능한 모든 해상도를 얻을 수 있습니다.

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

간단히 말하면 첫 화면(여러 개일 경우)의 너비와 높이를 별도로 표시합니다.

$height = (((Get-WmiObject -Class Win32_VideoController).VideoModeDescription  -split '\n')[0]  -split ' ')[2]
$width = (((Get-WmiObject -Class Win32_VideoController).VideoModeDescription  -split '\n')[0]  -split ' ')[0]

언급URL : https://stackoverflow.com/questions/7967699/get-screen-resolution-using-wmi-powershell-in-windows-7

반응형