Last active
May 26, 2024 16:09
-
-
Save BenjaminArmstrong/4e2c0df7ab62b909945b8d277f3e4b1a to your computer and use it in GitHub Desktop.
PowerShell script that will read a virtual machine screen to you
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$VMName = "New Virtual Machine" | |
# Rectangle format == top left x, top left y, width, height | |
# $cropRectangle = New-Object System.Drawing.Rectangle 35,485,218,147 | |
$cropRectangle = $null | |
$speakItToMe = $true | |
# Load System.Drawing | |
Add-Type -AssemblyName "System.Drawing" | |
# Load Tesseract - using code from https://github.com/jourdant/powershell-paperless | |
ipmo "C:\Users\benja\Documents\powershell-paperless-master\tesseractlib.psm1" | |
$VMMS = Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_VirtualSystemManagementService | |
$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,Format32bppArgb | |
$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 | |
} | |
$VMBitmap = getVMScreenBMP $VMCS $xResolution $yResolution | |
if ($cropRectangle -ne $null) { | |
$VMBitmap = $VMBitmap.Clone($cropRectangle, $VMBitmap.PixelFormat) | |
} | |
# Scaling the bitmap up makes OCR more reliable | |
$scaledVMBitmap = New-Object System.Drawing.Bitmap -Args $VMBitmap, ($VMBitmap.Width*2), ($VMBitmap.Height*2) | |
$text = (Get-TessTextFromImage -Image $scaledVMBitmap).Text | |
if ($speakItToMe) { | |
$cleanText = ($text.Replace("`n"," ")).replace(" ", "`n") | |
Add-Type -AssemblyName System.speech | |
$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer | |
$speak.SelectVoiceByHints('Female') | |
$speak.Speak($cleanText) } | |
else { | |
write-host $text | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment