PowerCLI Script to Download VM Screenshots from Datastore

When vSphere HA VM Monitoring resets a VM after a VMware Tools heartbeat failure, it can save a screenshot of the VM console just before the reset. The screenshot is stored alongside the VM’s configuration files on the datastore. You can find its path in the vCenter reset event. Broadcom KB 337335 shows an example.

vCenter event for a vSphere HA reset after a VMware Tools heartbeat failure, with the saved screenshot path.
The vSphere HA reset event shows where the VM console screenshot was saved.

Why I wrote the script

I usually download these files by browsing to the VM’s directory in the datastore browser. On a large cluster, that gets tedious, especially when there are hundreds of vSAN directories to sift through. I wrote a small PowerCLI script to do the browsing and downloading for me.


Get the script

The script finds the VM’s datastore directory and downloads files with the extension you specify. Use PNG for screenshots or LOG for files such as vmware.log. I tested it on vSphere 6.7 Update 3 with vSAN and VMFS 6 datastores.

Download Get-VMFiles.ps1 · View on GitHub

<#
.SYNOPSIS
    Downloads virtual machine screenshots, logs, or other files from the datastore to a local machine.

.DESCRIPTION
    This function connects to a vCenter server, locates the specified virtual machine, 
    and downloads files of the specified type from the VM's directory on the datastore 
    to a specified local directory.

.PARAMETER vCenter
    The URL or IP address of the vCenter server.

.PARAMETER VMName
    The name of the virtual machine from which files are to be downloaded.

.PARAMETER ItemType
    The file extension (e.g., LOG, PNG) of the files to be downloaded.

.PARAMETER LocalPath
    The local directory path where the files will be saved.

.PARAMETER Credential
    The credentials to connect to the vCenter server. Use `Get-Credential` to supply the credentials.

.EXAMPLE
    Get-VMFiles -vCenter "vc.virtuallywired.io" -VMName "win2016" -ItemType "PNG" -LocalPath "C:\VM_LOGS" -Credential (Get-Credential)

.NOTES
    Version: 1.1
    Tested on vSphere 6.7 Update 3, vSAN 6.7 Update 3, and VMFS 6.
    Author: Nicholas Mangraviti #VirtuallyWired
    Blog URL: virtuallywired.io
#>
function Get-VMFiles {
    param (
        [Parameter(Mandatory = $true)]
        [string]$vCenter,

        [Parameter(Mandatory = $true)]
        [string]$VMName,

        [Parameter(Mandatory = $true)]
        [string]$ItemType,

        [Parameter(Mandatory = $true)]
        [string]$LocalPath,

        [Parameter(Mandatory = $true)]
        [PSCredential]$Credential
    )

    try {
        # Connect to vCenter
        Write-Host "Connecting to vCenter: $vCenter" -ForegroundColor Cyan
        $VC = Connect-VIServer -Server $vCenter -Credential $Credential

        # Get the VM object
        Write-Host "Locating VM: $VMName" -ForegroundColor Cyan
        $VM = Get-VM -Name $VMName

        # Create target local directory
        $Target = New-Item -ItemType Directory -Force -Path "$LocalPath\$($VM.Name)"

        # Extract VM path details
        $vmpath = [regex]::Matches(([regex]::Replace(($VM.ExtensionData.Config.Files.LogDirectory), "\s", "")), '\[(.+)\](.+)\/').Groups.Value
        $Datastore = $VM | Get-Datastore -Name $($vmpath[1])

        # Map datastore as a PSDrive
        if ((Get-PSDrive).Name -eq "DSdrive") {
            Remove-PSDrive -Name DSdrive -Confirm:$false -Force | Out-Null
        }
        New-PSDrive -Location $Datastore -Name DSdrive -PSProvider VimDatastore -Root "\" | Out-Null

        # Change location to VM's datastore directory
        Set-Location "DSdrive:\$($vmpath[2])\"

        # Download files
        if (Get-ChildItem "*.$ItemType") {
            Write-Host "'$ItemType' file(s) found, preparing to download..." -ForegroundColor Green
            Copy-DatastoreItem -Item "*.$ItemType" -Destination $Target
            Write-Host "File(s) downloaded successfully to $($Target.FullName)" -ForegroundColor Green
        } else {
            Write-Host "No '$ItemType' file(s) found." -ForegroundColor Red
        }

        # Reset location
        Set-Location C:

        # Cleanup PSDrive
        if (Get-PSDrive -Name DSdrive) {
            Remove-PSDrive -Name DSdrive -Confirm:$false -Force | Out-Null
        }
    } catch {
        Write-Host "An error occurred: $_" -ForegroundColor Red
    } finally {
        # Disconnect from vCenter
        Write-Host "Disconnecting from vCenter..." -ForegroundColor Cyan
        Disconnect-VIServer -Server $VC -Force -Confirm:$false -ErrorAction SilentlyContinue | Out-Null
    }
}
Show full script

I hope you found this useful. If you have any feedback, feel free to get in touch. 🙂

Leave a Reply