Skip to content

Instantly share code, notes, and snippets.

@BenjaminArmstrong
Last active June 18, 2024 02:35
Show Gist options
  • Save BenjaminArmstrong/e6e4c771b5553b6c15788a3406feb934 to your computer and use it in GitHub Desktop.
Save BenjaminArmstrong/e6e4c771b5553b6c15788a3406feb934 to your computer and use it in GitHub Desktop.
Sample PowerShell script that shows you how to create a .BMP file of the display of a Hyper-V virtual machine.
$VMName = "VM 1"
$BMPName = "C:\Users\benja\Desktop\test.bmp"
Add-Type -AssemblyName "System.Drawing"
$VMCS = Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_ComputerSystem -Filter "ElementName='$($VMName)'"
# Get the resolution of the screen at the moment
$video = $VMCS.GetRelated("Msvm_VideoHead")
$xResolution = $video.CurrentHorizontalResolution[0]
$yResolution = $video.CurrentVerticalResolution[0]
function getVMScreenBMP {
param
(
$VM,
$x,
$y
)
$VMMS = Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_VirtualSystemManagementService
# Get screenshot
$image = $VMMS.GetVirtualSystemThumbnailImage($VMCS, $x, $y).ImageData
# Transform into bitmap
$BitMap = New-Object System.Drawing.Bitmap -Args $x,$y,Format16bppRgb565
$Rect = New-Object System.Drawing.Rectangle 0,0,$x,$y
$BmpData = $BitMap.LockBits($Rect,"ReadWrite","Format16bppRgb565")
[System.Runtime.InteropServices.Marshal]::Copy($Image, 0, $BmpData.Scan0, $BmpData.Stride*$BmpData.Height)
$BitMap.UnlockBits($BmpData)
return $BitMap
}
(getVMScreenBMP $VMCS $xResolution $yResolution).Save($BMPName)
@citelao
Copy link

citelao commented May 3, 2024

I can confirm this works on Windows PowerShell, but not modern PowerShell (powershell.exe, not pwsh.exe), at least on my machine. This is awesome!

EDIT (18 June 2024): The code as-is only works in Windows PowerShell, but that's because there are newer APIs for using WMI/CIM in modern PowerShell.

For my purposes, I used runspaces to continue using the old APIs, but you can see a modern implementation in strawgate's HyperV-OCR project (a friend found that for me).

Runspace alternative:

$s = New-PSSession -UseWindowsPowerShell
Invoke-Command -Session $s -ScriptBlock {
  # Call above code, basically.
  #
  # If you want to reference external variables:
  # $MyVar becomes $using:MyVar
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment