您好,我遇到了一些powershell的问题,我用它来检测屏幕分辨率,然后用宽度除以高度。
$width = (Get-WmiObject -Class Win32_VideoController).CurrentHorizontalResolution
$height = (Get-WmiObject -Class Win32_VideoController).CurrentVerticalResolution
$result = $width / $height
echo $width
echo $height
echo $result基本上,问题出在上面的第三行。我在PS控制台中得到了一个与此相关的错误。
Method invocation failed because [System.Object[]] does not contain a method named 'op_Division'.我有点明白这是怎么回事。变量$width和$height的类型不正确,例如decimal或double。问题是我不知道如何将这种类型赋给这些变量。我已经尝试了以下几种方法。
[double]$width = (Get-WmiObject -Class Win32_VideoController).CurrentHorizontalResolution
[double]$height = (Get-WmiObject -Class Win32_VideoController).CurrentVerticalResolution
[double]$result = $width / $height
echo $width
echo $height
echo $result问题是我在PS控制台中得到了这个错误。
Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Double".所以基本上它不能将$width和$height变量转换成我需要的类型,来除以产生的数字,它看起来是System.Object[]类型的。
有谁能给我指个方向吗?我是powershell的新手,所以我一直在学习,我认为我从中学到的任何东西在我成为一名流利的powersheller的过程中都将是非常有用的。任何帮助也是非常感谢的。
PS -这似乎在运行powershell 4.0的Windows 7上工作,但我现在使用的是我的家用PC,它是运行powershell 4.0的Windows 8.1
发布于 2014-07-18 16:40:55
如下所示:
$width = ((Get-WmiObject -Class Win32_VideoController).CurrentHorizontalResolution)[0]
$height = ((Get-WmiObject -Class Win32_VideoController).CurrentVerticalResolution)[0]
$result = $width / $height
echo $width
echo $height
echo $resultIMHO (Get-WmiObject -Class Win32_VideoController).CurrentHorizontalResolution返回具有两个或更多监视器的解决方案的数组。如果有多个监视器,则需要循环阵列。
发布于 2014-07-18 22:02:13
这有一个问题。在某些情况下,Get-WmiObject -Class Win32_VideoController返回未设置分辨率值的对象。
例如,在我的系统上:
PS C:\Windows\system32> Get-WmiObject -Class Win32_VideoController | select-object Caption,CurrentHorizontalResolution,CurrentVerticalResolution
Caption CurrentHorizontalResolution CurrentVerticalResolution
------- --------------------------- -------------------------
NVIDIA GeForce GT 730M
Intel(R) HD Graphics Family 1280 1024 获得你想要的结果的更可靠的方法:
PS C:\Windows\system32> Add-Type -AssemblyName System.Windows.Forms
PS C:\Windows\system32> [System.Windows.Forms.Screen]::AllScreens |
select @{n='Width';e={$_.WorkingArea.Width}}, @{n='Height';e={$_.WorkingArea.Height}},
@{n='Ratio';e={$_.WorkingArea.Width / $_.WorkingArea.Height }} | ft -AutoSize
Width Height Ratio
----- ------ -----
1218 1024 1.189453125
1600 900 1.77777777777778或者:
PS C:\Windows\system32> [System.Windows.Forms.Screen]::AllScreens |
select @{n='Width';e={$_.Bounds.Width}}, @{n='Height';e={$_.Bounds.Height}},
@{n='Ratio';e={$_.Bounds.Width / $_.Bounds.Height }} | ft -AutoSize
Width Height Ratio
----- ------ -----
1280 1024 1.25
1600 900 1.77777777777778根据您想要的是屏幕的工作区(可能不包括开始栏)还是显示器的完整尺寸(包括它)。
编辑:实际上,你可能想要的是:
PS C:\> $size = ([System.Windows.Forms.Screen]::AllScreens |? Primary).Bounds.Size
PS C:\> $result = $size.Width/$size.Height
PS C:\> $result
1.25这就给出了主显示器的比例。
https://stackoverflow.com/questions/24820645
复制相似问题