Hello,
When running the Invoke-VMScript commandlet I receive an error similar to the following:
Invoke-VMScript : Invoke-VMScript An error occurred while sending the request.
At line:1 char:1
+ Invoke-VMScript -ScriptText "get-service" -VM $VM -GuestUser Administ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-VMScript], ViError
+ FullyQualifiedErrorId : Client20_VmGuestServiceImpl_DownloadFileFromGuest_DownloadError,VMware.VimAutomation.ViC
ore.Cmdlets.Commands.InvokeVmScript
My code is as follows:
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;
}
}
"@
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
Invoke-VMScript -vm "test centos7" -scripttext "ls" -guestuser root -guestpassword #####
I tried the resolution outlined in the following thread with no success:
I'm running PowerCLI 12.0 on vSphere 6.5.0.30000 with VMware Tools version 10346.
Is my code sound or is there something that I missed?
Thanks in advance.
Is there perhaps a proxy between your station and the VM?
Also, did you use the -NoIPinCert switch or not?
Update: I just noticed you stated there is no proxy involved.
Update2: could it be that port 902 between your station and the ESXi node on which the target VM is running, is not open?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The error seems to occur when the Invoke-VMScript downloads the file with the output from the target VM.
As the user in the thread, you pointed to also experienced, you might want to try my Invoke-VMScriptPlus with the Verbose switch.
That should give you more information on what goes wrong.
And while you are at, try the NoIPinCert switch as well.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hello,
I tried your script and received the errors below. I configured an IP on the VM and put hosts file entries on the host running the script. I did some research on the error and found references to proxy configurations which I am not using.
VERBOSE: 4/6/2020 2:06:40 PM Get-View Finished execution
VERBOSE: 4/6/2020 2:06:40 PM Get-View Finished execution
VERBOSE: 4/6/2020 2:06:40 PM Get-View Finished execution
VERBOSE: 4/6/2020 2:06:40 PM Get-View Finished execution
VERBOSE: Created temp folder in guest OS C:\Users\ADMINI~1\AppData\Local\Temp\salman_2020vmware26
VERBOSE: Created temp script file in guest OS
C:\Users\ADMINI~1\AppData\Local\Temp\salman_2020vmware26\salman_2020vmware219.ps1
VERBOSE: Created temp output file in guest OS
C:\Users\ADMINI~1\AppData\Local\Temp\salman_2020vmware26\salman_2020_outputvmware233
VERBOSE: Copying Data to C:\Users\ADMINI~1\AppData\Local\Temp\salman_2020vmware26\salman_2020vmware219.ps1
Invoke-WebRequest : Unable to connect to the remote server
At C:\Users\salman\Documents\test.ps1:261 char:15
+ ... copyResult = Invoke-WebRequest -Uri $fileInfo -Method Put -Body $Data
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : System.Net.WebException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
ScripText copy failed!Status
At C:\Users\salman\Documents\test.ps1:264 char:1
+ Throw "ScripText copy failed!`rStatus $($copyResult.StatusCode)`r$(($ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (ScripText copy failed!Status :String) [], RuntimeException
+ FullyQualifiedErrorId : ScripText copy failed!Status
Is there perhaps a proxy between your station and the VM?
Also, did you use the -NoIPinCert switch or not?
Update: I just noticed you stated there is no proxy involved.
Update2: could it be that port 902 between your station and the ESXi node on which the target VM is running, is not open?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Looks like port 902 wasn't opened from the host I was running the script from to the ESXi hosts. That what was causing the issue.
Thanks a lot for your help.
We have two vCenters BLR and NJDC. When I have executed the below script ,it is working in NJDC VC and not working in BLR VC.Please find the reference screenshots.
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
$VMName = 'XXXXXX'
$vCenterIP = 'https://XXXXXXXX/sdk'
$vCenterUser= 'XXXXXX'
$vCenterPassword = 'XXXXXXX'
$desktopGuestUser = 'XXXXXXX'
$desktopGuestPassword = 'XXXXXXXX'
$APIUserName = 'XXXXXXX'
$APIPassword ='XXXXXXX'
$DomainPassword = 'XXXXXXX'
$vCenterIP=$vCenterIP.split('/')[2];
$Password=ConvertTo-SecureString -string $vCenterPassword -AsPlainText -Force
$ViServerCred=New-Object System.Management.Automation.PSCredential($vCenterUser,$Password)
Connect-VIServer -Server $vCenterIP -Protocol https -Credential $ViServerCred -Force
$VMPassword=ConvertTo-SecureString -string $desktopGuestPassword -AsPlainText -Force
$DOMPassword=ConvertTo-SecureString -string $DomainPassword -AsPlainText -Force
$VMCred=New-Object System.Management.Automation.PSCredential($desktopGuestUser.ToLower(),$VMPassword)
$Result = Invoke-VMScript -ScriptText "hostname" -VM $VMName -GuestCredential $VMCred -ScriptType Bash -ToolsWaitSecs 30 -Verbose
$Result
Can you help with this? We have already allowed port 902.when run the below script for one of VCSA.it is not showing output.
Try the same with my Invoke-VMScriptPlus function and use the Verbose switch.
It might provide more feedback.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference