WilliamLam.com

  • About
    • About
    • Privacy
  • VMware Cloud
  • Tanzu
    • Application Modernization
    • Tanzu services
    • Tanzu Community Edition
    • Tanzu Kubernetes Grid
    • vSphere with Tanzu
  • Home Lab
  • Nested Virtualization
  • Apple
You are here: Home / Automation / Automating Virtual Machine screenshots in vSphere

Automating Virtual Machine screenshots in vSphere

01.18.2023 by William Lam // 3 Comments

I was recently reminded of an old article that I wrote back in 2013 demonstrating how to capture Virtual Machine screenshots using a couple of options that are available within vSphere. While the blog post is more than 10 years old, the guidance and options are still applicable for all recent vSphere releases.

I highly recommend folks give the original article a read, especially for the full background but I did want to provide some updated information for those interested in automating the capture of VM screenshots using both the vSphere API as well as the HTTP Handler methods.

Using vSphere API

While the original article included a vSphere SDK for Perl sample script, I figured most folks would prefer seeing a PowerCLI example using the vSphere API 🙂 Below is a functional example that uses the CreateSCreenshot_Task() API and waits for the task to complete, which then returns the location path for where the screenshot (PNG) file is stored and can be downloaded from.

$vm = Get-VM vcsa.primp-industries.local
$task = $vm.ExtensionData.CreateScreenshot_Task()
$task1 = Get-Task -Id ("Task-$($task.value)")
$task1 | Wait-Task


To download the screenshot, we will use both the New-PSDrive to create virtual drive to vSphere Datas tore and then use Copy-DatastoreItem to transfer the screenshot to our local desktop. The commands below creates the logical drive mapping to the inventory hierarchy of the vSphere Datastore of where the screenshot is stored. In my example, the parent vSphere Datacenter for the vSphere Datastore is called Primp-Datacenter and vSphere Datastore name is sm-vsanDatastore.

New-PSDrive -PSProvider VimDatastore -Name sm-vsanDatastore -Root vmstore:Primp-Datacenter\sm-vsanDatastore
Copy-DatastoreItem 'sm-vsanDatastore:\98f77763-01c1-f5d5-cbd5-ac1f6b1a4efa/vcsa.primp-industries.local-2.png' 'vcsa.primp-industries.local-2.png'

The benefit of using the vSphere API is that this method works with both vCenter Server as well as a standalone ESXi host, so the same code can be used in either environments in case you need to support both. To be able to use this vSphere API method, ensure the user performing the operation has the VirtualMachine.Interact.CreateScreenshot privilege.

Using HTTP Handler

Alternatively to the vSphere API, you can also leverage the HTTP handler method which now seems to only work when using a standalone ESXi host, at least as of vSphere 8. I was not able to connect to vCenter Server and quickly glancing at some of the rhttpproxy and vpxd logs, I noticed that this method may no longer be implemented.

For standalone ESXi host, you will perform an authenticated POST operation to a specific URL that includes the VM Managed Object Reference (MoRef) ID. The MoRef ID will differ from the vCenter MoRef ID even for the exact same VM as an ESXi host will generate a local MoRef ID compared to the one generated by vCenter Server. You can retrieve the desired MoRef ID by simply navigating to the desired VM using the ESXi Embedded Host Client and looking at your browser URL https://esxi.primp-industries.local/ui/#/host/vms/4435 where 4435 is the MoRef ID value or you can also use the vSphere API connected to an ESXi host to retrieve the list of MoRef IDs.

If you wish to automate the HTTP Handler method instead of opening up a browser, then you will need to perform an authenticated POST operation to the /screen endpoint as shown in the examples below.

cURL example:

curl -k -d 'user=root&password=VMware1!' -X POST 'https://esxi.primp-industries.local/screen?id=4435' -o screenshot.png

PowerShell example:

$webrequestParams = @{
    Uri = 'https://esxi.primp-industries.local/screen?id=4435'
    Method = 'POST'
    Body = @{
        user = 'root'
        password = 'VMware1!'
    }
    OutFile = "screenshot.png"
}
Invoke-WebRequest @webrequestParams -SkipCertificateCheck


To be able to use this HTTP handler method, ensure the user performing the operation has the VirtualMachine.Interact.CreateScreenshot privilege.

More from my site

  • Quick Tip - Inventory core count for vSphere+, vSAN+ & VCF+ Cloud Service
  • vSphere Session Timeout Overview
  • A first look at the new vSphere+ & vSAN+ Cloud Service
  • Application Transformer for VMware Tanzu is more than just an App Modernization Engine
  • VMware Event Broker Appliance (VEBA) v0.6.1

Categories // Automation, vSphere 7.0, vSphere 8.0 Tags // screenshot, vSphere

Comments

  1. Raoul Schaffner says

    01/18/2023 at 11:00 am

    thank you! is there a method to wake the screen in case its blacked out by the guest os?

    Reply
    • William Lam says

      01/18/2023 at 11:02 am

      Yes, you can use https://williamlam.com/2017/09/automating-vm-keystrokes-using-the-vsphere-api-powercli.html

      Reply
  2. Raoul Schaffner says

    01/18/2023 at 11:56 am

    great! this will be my next pet project. 🙂 thanks again, r.

    Reply

Thanks for the comment! Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Search

Author

William Lam is a Senior Staff Solution Architect working in the VMware Cloud team within the Cloud Infrastructure Business Group (CIBG) at VMware. He focuses on Cloud Native technologies, Automation, Integration and Operation for the VMware Cloud based Software Defined Datacenters (SDDC)

Connect

  • Email
  • GitHub
  • LinkedIn
  • RSS
  • Twitter
  • Vimeo

Recent

  • Blocking vSphere HTML5 VM Console and allowing only Standalone VM Remote Console (VMRC)? 02/08/2023
  • Quick Tip - Inventory core count for vSphere+, vSAN+ & VCF+ Cloud Service 02/07/2023
  • How to automate adding a license into vCenter Server with custom label?  02/06/2023
  • Automated ESXi Installation with a USB Network Adapter using Kickstart 02/01/2023
  • How to bootstrap ESXi compute only node and connect to vSAN HCI Mesh? 01/31/2023

Advertisment

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

Copyright WilliamLam.com © 2023

 

Loading Comments...