PowerShell错误:无法将System.Object转换为System.Int32

时间:2017-03-13 19:38:17

标签: .net powershell

我正在构建一个PS脚本,它会截取屏幕截图并将其保存在文件中,以便稍后比较其哈希值。我使用了我在post中找到的功能,但我一直在使用#34; Type"当我用变量替换值时出错。这就是我到目前为止所做的:

[void][Reflection.Assembly]::LoadWithPartialName("System.Drawing")

function screenshot([Drawing.Rectangle]$bounds, $path) {

   $bmp = New-Object Drawing.Bitmap $bounds.width, $bounds.height
   $graphics = [Drawing.Graphics]::FromImage($bmp)
   $graphics.CopyFromScreen($bounds.Location, [Drawing.Point]::Empty, $bounds.size)
   $bmp.Save($path)
   $graphics.Dispose()
   $bmp.Dispose()
}

$computer = $env:COMPUTERNAME
$namespace = "ROOT\cimv2"
$classname = "Win32_VideoController"

$WMIos = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computer
$ResolutionX = Get-WmiObject -Class $classname -ComputerName $computer -Namespace $namespace | Select-Object -Property CurrentHorizontalResolution
$ResolutionY = Get-WmiObject -Class $classname -ComputerName $computer -Namespace $namespace | Select-Object -Property CurrentVerticalResolution
$ResIntX = $ResolutionX.CurrentHorizontalResolution
$ResIntY = $ResolutionY.CurrentVerticalResolution

$Cpath = "C:\Users\kiosk\Documents\FullScreenCapture.png"
$bounds2 = [Drawing.Rectangle]::FromLTRB(0, 0, $ResIntX, $ResIntY) #Getting screen resolution from WMI


screenshot $bounds2 $Cpath

这是我得到的错误:

Cannot convert argument "right", with value: "System.Object[]", for "FromLTRB" to type "System.Int32": "Cannot
 convert the "System.Object[]" value of type "System.Object[]" to type "System.Int32"."
At line:25 char:1
+ $bounds2 = [Drawing.Rectangle]::FromLTRB(0, 0, $ResIntX, $ResIntY) #G ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

Exception calling "CopyFromScreen" with "3" argument(s): "The handle is invalid"
At line:8 char:4
+    $graphics.CopyFromScreen($bounds.Location, [Drawing.Point]::Empty, ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : Win32Exception

收到错误后,我检查$ ResIntX | get-member我得到的类型是" TypeName:System.UInt32"

如果我尝试将其值转换为[int32],我仍会得到相同的错误

我做错了什么?

0 个答案:

没有答案