Skip to content

Instantly share code, notes, and snippets.

@newyear2006
Last active January 14, 2018 14:06
Show Gist options
  • Select an option

  • Save newyear2006/f40696264fc63f23d8b1e133364c3da0 to your computer and use it in GitHub Desktop.

Select an option

Save newyear2006/f40696264fc63f23d8b1e133364c3da0 to your computer and use it in GitHub Desktop.

Revisions

  1. newyear2006 revised this gist Jan 14, 2018. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion HyperVVMScreenCopy.PS1
    Original file line number Diff line number Diff line change
    @@ -43,7 +43,8 @@ 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.Height = $img.Size.Height;
    $form.AutoScroll = $true;
    $pictureBox = new-object Windows.Forms.PictureBox
    $pictureBox.Width = $img.Size.Width;
    $pictureBox.Height = $img.Size.Height;
  2. newyear2006 created this gist Jan 10, 2018.
    70 changes: 70 additions & 0 deletions HyperVVMScreenCopy.PS1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    #
    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;
    $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)

    }