powershell - powershell中的op_Division和数据类型

标签 powershell types division type-conversion

嗨,我遇到了一些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的类型不正确,例如小数或 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上运行,但是我现在使用的是运行Powershell 4.0的Windows 8.1的家用PC

最佳答案

像这样做:

$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返回带有两个或更多监视器的解决方案数组。
如果有多个监视器,则需要循环阵列。

关于powershell - powershell中的op_Division和数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24820645/

相关文章:

powershell - 从外部 URI 获取 JSON 对象中的值

c - 为什么输出是这样的?

powershell - PowerShell 批量文件重命名中的奇怪行为

powershell - 从Powershell函数内部的外部命令流输出

powershell - Powershell 是一种足够成熟的技术供企业使用吗?

arrays - DirectCast(或替代方案)将类型数组中的值作为第二个参数?

typescript - TypeScript 输入问题

c++ - 此指针类型转换符号的含义

assembly - 有符号整数相除后结果错误

python - 带有传统除号 (÷) 的 eval() 问题