VMWare VSphere Take a screen shot of a VM console from PowerShell

$StorageLocation = "C:\temp\SMA"
$FileName = [System.IO.Path]::GetRandomFileName()
$FileName = "$FileName.jpg"
#VCenter Credentials
$Server = Server
$Username = UserName
$Password = Password
    Try {
            Add-PSSnapin VMware.VimAutomation.Core
            write-verbose "Connecting to vcenter"
            if ($DefaultVIServers.Count -lt 1) {
                $VISession =  Connect-VIServer -Server $Server -User $Username -Password $Password
            }
            $VCSECPwd = ConvertTo-SecureString -AsPlainText -String $using:Password -Force
            $VCCred = $cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $using:Username, $VCSECPwd
            $VM = Get-VM -Name $Using:ServerName
            $MoRef = $VM.ExtensionData.MoRef.Value
            Invoke-WebRequest "https://$using:Server/screen?id=$MoRef" -outFile "$StorageLocation\$FileName" -credential $VCCred

    } Catch {
        $ExceptionMessage = $Error[0].Exception
        write-verbose "Exception - $ExceptionMessage"
        $Exception = $true
    } Finally {
        # Create Return Object
        
        $ReturnObject = [PSCustomObject]@{
            Ret = "$StorageLocation\$FileName"
            Exception = $Exception
            ExceptionMessage = $ExceptionMessage
        }
        # Return Object
        Disconnect-VIServer -server $Server -force -confirm:$false
        $ReturnObject

Jeremy Tirrell

Read more posts by this author.