page hit counter
Welcome to MSDN Blogs Sign in | Join | Help

Taylor Brown's Blog

Test Lead for Windows Core OS Division on the Hyper-V Team.

Syndication

News

Welcome to the professional blog of Taylor Brown.  I am a test lead on the core virtualization team (Hyper-V) at Microsoft.

This blog will contain information about virtualization, Microsoft, Hyper-V, operating systems, testing, and whatever else I end up talking about...

 

Standard Microsoft Disclaimer:
"This posting is provided "AS IS" with no warranties, and confers no rights. You assume all risk for your use."

-Taylor Brown


Hyper-V WMI: Creating a Thumbnail Image

Hyper-V has a WMI API that will allow you to create a thumbnail image of any running or paused virtual machine.  You can create any sized thumbnail you want (640x480, 800x600, 1024x768 etc…).  Creating the image is pretty easy, you just call GetVirtualSystemThumbnailImage passing a reference to an Msvm_VirutalSystemSettingData instance and the size of the image you want…  However getting something useful from the returned data is a bit tricky…  You get an array of unit8’s that represent pixels, the API doesn’t have much choice and luckily Powershell makes this not an impossible feat.  All you have to do is create a new bitmap object and read (marshal) the pixel data into the object…  So here’s a script for you!

Windows Server 2008 - x64_running Windows Server 2008 - x64_saved
Running Virtual Machine Saved Virtual Machine

 

Powershell Script:

[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

$HyperVParent = "localhost"
$HyperVGuest = "Windows Server 2008 - x64"
$ImagePath = "c:\Temp"
$xRes = 640
$yRes = 480


$VMManagementService = Get-WmiObject -class "Msvm_VirtualSystemManagementService" -namespace "root\virtualization" -ComputerName $HyperVParent
$Vm = Get-WmiObject -Namespace "root\virtualization" -ComputerName $HyperVParent -Query "Select * From Msvm_ComputerSystem Where ElementName='$HyperVGuest'"
$VMSettingData = Get-WmiObject -Namespace "root\virtualization" -Query "Associators of {$Vm} Where ResultClass=Msvm_VirtualSystemSettingData AssocClass=Msvm_SettingsDefineState" -ComputerName $HyperVParent
$RawImageData = $VMManagementService.GetVirtualSystemThumbnailImage($VMSettingData, "$xRes", "$yRes") #| ProcessWMIJob $VMManagementService.PSBase.ClassPath "GetVirtualSystemThumbnailImage"

$VMThumbnail = new-object System.Drawing.Bitmap($xRes, $yRes, [System.Drawing.Imaging.PixelFormat]::Format16bppRgb565)

$rectangle = new-object System.Drawing.Rectangle(0,0,$xRes,$yRes)
[
System.Drawing.Imaging.BitmapData] $VMThumbnailBitmapData = $VMThumbnail.LockBits($rectangle, [System.Drawing.Imaging.ImageLockMode]::WriteOnly, [System.Drawing.Imaging.PixelFormat]::Format16bppRgb565)
[
System.Runtime.InteropServices.marshal]::Copy($RawImageData.ImageData, 0, $VMThumbnailBitmapData.Scan0, $xRes*$yRes*2)
$VMThumbnail.UnlockBits($VMThumbnailBitmapData);

$VMThumbnail
$VMThumbnail.Save("$ImagePath\$HyperVGuest.jpg")

Taylor Brown
Hyper-V Integration Test Lead
http://blogs.msdn.com/taylorb

clip_image001

Published Tuesday, July 29, 2008 6:00 AM by taylorb

Filed under: , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# a-foton » Hyper-V WMI: Creating a Thumbnail Image @ Tuesday, July 29, 2008 9:19 AM

PingBack from http://blog.a-foton.ru/2008/07/hyper-v-wmi-creating-a-thumbnail-image/

a-foton » Hyper-V WMI: Creating a Thumbnail Image

Leave a Comment

(required) 
required 
(required) 
Page view tracker