0

I'm building a PS script that would take a screenshot and save it on file in order to compare its Hash value later. I'm using the function I found in this post, but I keep getting "Type" errors when I substitute values with variables. This is what I have so far:

[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

This is the errors I get:

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

After getting the error and I check $ResIntX | get-member I get that the type is "TypeName: System.UInt32"

If I try to cast its value to [int32] I still get same error

What am I doing wrong?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • `Select-Object -Property CurrentHorizontalResolution` -> `Select-Object -ExpandProperty CurrentHorizontalResolution`. same for the other one as well. Right now you have an object with a single property. `-Expand` just returns the property _value_. `(Get-WmiObject -Class $classname -ComputerName $computer -Namespace $namespace).CurrentHorizontalResolution` should also work. – Matt Mar 13 '17 at 19:41
  • what's the value getting? – Jeb50 Mar 13 '17 at 19:46
  • According to your error message the variable contains an *array*, whereas the method you're invoking expects a *single value*. Try `$ResIntX = $ResolutionX | Select-Object -Expand CurrentHorizontalResolution -First 1`. – Ansgar Wiechers Mar 13 '17 at 19:47

0 Answers0