Invoke-VMScript: An error occurred while sending the request

Posted by

After upgrading the PowerShell module to the latest version now available (11.3.0.13964823), my invoke-vmscript goes wrong. The following exception occurs:

Invoke-VMScript : Invoke-VMScript         An error occurred while sending the request.
    + CategoryInfo          : NotSpecified: (:) [Invoke-VMScript], ViError
    + FullyQualifiedErrorId : Client20_VmGuestServiceImpl_DownloadFileFromGuest_DownloadError,VMware.VimAutomation.ViCore.Cmdlets.Commands.InvokeVmScript

Finally after a couple hours of troubleshooting, parameter checking and reinstalls we found the issue. The issue is caused by a untrusted certificate of the VMWare VCenter server.

To solve this the “recommended” way: Check and fix your certificates on your system and VCenter server.
The dirty “not recommended” way: Add the following code to your script.

add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
  public bool CheckValidationResult(
  ServicePoint srvPoint, X509Certificate certificate,
  WebRequest request, int certificateProblem) {
  return true;
  }
}
"@

[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

3 comments

  1. I just started having this error after installing PowerShell 7. I tried uninstalling PowerShell 7 but the error persisted. What is weird is that I get the error but every Invoke-VMScript worked flawlessly other than puking red all over the screen.

    Thanks so much for posting this! I was about to go down the rabbit hole.

Leave a Reply

Your email address will not be published. Required fields are marked *