Skip to content

Instantly share code, notes, and snippets.

@newyear2006
Last active January 14, 2018 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save newyear2006/f40696264fc63f23d8b1e133364c3da0 to your computer and use it in GitHub Desktop.
Save newyear2006/f40696264fc63f23d8b1e133364c3da0 to your computer and use it in GitHub Desktop.
Zum Speichern und Anzeigen des aktuellen Bildschirm einer virtuellen Maschine
#
Add-Type -AssemblyName "System.Drawing"
Add-Type -AssemblyName "System.Windows.Forms"
function Get-VMScreenBMP {
param
(
$VMName,
$index=0
)
$Filter="ElementName='$($VMName)'"
$VMCS = Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_ComputerSystem –Filter $Filter
# Get the resolution of the screen at the moment
$video = $VMCS.GetRelated("Msvm_VideoHead")
If ($video.Count -gt 0) {
If ($index -gt $video.Count) {
$index = $video.Count
}
$x = $video.CurrentHorizontalResolution[$index]
$y = $video.CurrentVerticalResolution[$index]
$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)
$BitMap
} else {
Write-Error "No Video Device found, VM not running?"
}
}
Function Show-Screen([System.Drawing.Image]$img, $VMName) {
$form = new-object Windows.Forms.Form
$form.Text = "Screen from $VMName"
$form.Width = $img.Size.Width;
$form.Height = $img.Size.Height;
$form.AutoScroll = $true;
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Width = $img.Size.Width;
$pictureBox.Height = $img.Size.Height;
$pictureBox.Image = $img;
$form.controls.add($pictureBox)
$form.Add_Shown( { $form.Activate() } )
$form.ShowDialog()
}
Function Show-VMScreen ($vmName, $index=0) {
$bmp=Get-VMScreenBMP $vmName $index
Show-Screen $bmp $VmName
}
Function Save-VMScreen ($vmName, $index=0, $filename=($ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath(".\$($VMName)Screen.BMP"))) {
$bmp=Get-VMScreenBMP $vmName $index
$bmp.Save($filename)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment