首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >powershell中的op_Division和数据类型

powershell中的op_Division和数据类型
EN

Stack Overflow用户
提问于 2014-07-18 16:36:13
回答 2查看 12.3K关注 0票数 1

您好,我遇到了一些powershell的问题,我用它来检测屏幕分辨率,然后用宽度除以高度。

代码语言:javascript
复制
$width = (Get-WmiObject -Class Win32_VideoController).CurrentHorizontalResolution
$height = (Get-WmiObject -Class Win32_VideoController).CurrentVerticalResolution
$result = $width / $height
echo $width
echo $height
echo $result

基本上,问题出在上面的第三行。我在PS控制台中得到了一个与此相关的错误。

代码语言:javascript
复制
Method invocation failed because [System.Object[]] does not contain a method named 'op_Division'.

我有点明白这是怎么回事。变量$width和$height的类型不正确,例如decimal或double。问题是我不知道如何将这种类型赋给这些变量。我已经尝试了以下几种方法。

代码语言:javascript
复制
[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控制台中得到了这个错误。

代码语言:javascript
复制
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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-07-18 16:40:55

如下所示:

代码语言:javascript
复制
$width = ((Get-WmiObject -Class Win32_VideoController).CurrentHorizontalResolution)[0]
$height = ((Get-WmiObject -Class Win32_VideoController).CurrentVerticalResolution)[0]
$result = $width / $height
echo $width
echo $height
echo $result

IMHO (Get-WmiObject -Class Win32_VideoController).CurrentHorizontalResolution返回具有两个或更多监视器的解决方案的数组。如果有多个监视器,则需要循环阵列。

票数 3
EN

Stack Overflow用户

发布于 2014-07-18 22:02:13

这有一个问题。在某些情况下,Get-WmiObject -Class Win32_VideoController返回未设置分辨率值的对象。

例如,在我的系统上:

代码语言:javascript
复制
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                                                   

获得你想要的结果的更可靠的方法:

代码语言:javascript
复制
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

或者:

代码语言:javascript
复制
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

根据您想要的是屏幕的工作区(可能不包括开始栏)还是显示器的完整尺寸(包括它)。

编辑:实际上,你可能想要的是:

代码语言:javascript
复制
PS C:\> $size = ([System.Windows.Forms.Screen]::AllScreens |? Primary).Bounds.Size

PS C:\> $result = $size.Width/$size.Height

PS C:\> $result
1.25

这就给出了主显示器的比例。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24820645

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档