Last active
January 14, 2018 14:06
-
-
Save newyear2006/f40696264fc63f23d8b1e133364c3da0 to your computer and use it in GitHub Desktop.
Zum Speichern und Anzeigen des aktuellen Bildschirm einer virtuellen Maschine
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
# | |
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