- Access exclusive content
- Connect with peers
- Share your expertise
- Find support resources
Our community just got stronger! Explore new identity security resources, connect with experts, and discover more ways to learn and collaborate.
07-30-2018 11:20 PM
Hello All we are trying to on board safe via REST API however while executing the Powershell script I encounter the below error . I have made the changes in web.config file changing the binding to "https".
Please advise
Enter user id:: admin1
StatusCode:
StatusDescription:
Response: The underlying connection was closed: An unexpected error occurred on a receive.
07-31-2018 12:58 AM
Does the user that runs the PS script trust the PVWAs SSL cert?
Are you definitely using the right logon URL?
Does the error occur when the script log on or when it actually performs a Safe operation?
07-31-2018 01:25 AM
Hello BernhardB,
Thanks for your prompt response I have been running with my domain ID ,
Can you guide how I could create trust for domain ID on PVWA's SSL cert ?
Yes my colleague is able to logon perfectly from desktop and I have access to client network via Jump box so encounter error.
Attached is the script for your reference and we are using CyberArk authentication
function PASREST-Logon {
if (-not ([System.Management.Automation.PSTypeName]'ServerCertificateValidationCallback').Type)
{
$certCallback = @"
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public class ServerCertificateValidationCallback
{
public static void Ignore()
{
if(ServicePointManager.ServerCertificateValidationCallback ==null)
{
ServicePointManager.ServerCertificateValidationCallback +=
delegate
(
Object obj,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors errors
)
{
return true;
};
}
}
}
"@
Add-Type $certCallback
}
[ServerCertificateValidationCallback]::Ignore()
# Declaration
$webServicesLogon = "$PVWA_URL/PasswordVault/WebServices/auth/Cyberark/CyberArkAuthenticationService.svc/Logon"
# Authentication
$uid=Read-Host -Prompt "Enter user id:"
$Pwd = Read-Host -Prompt "Enter password" -AsSecureString
$pwd1=[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($Pwd))
$bodyParams = @{username = "$uid"; password = "$pwd1"} | ConvertTo-JSON
#write-host $bodyParams
# Execution
try {
$logonResult = Invoke-RestMethod -Uri $webServicesLogon -Method POST -ContentType "application/json" -Body $bodyParams -ErrorVariable logonResultErr
Return $logonResult.CyberArkLogonResult
}
catch {
Write-Host "StatusCode: " $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription: " $_.Exception.Response.StatusDescription
Write-Host "Response: " $_.Exception.Message
Return $false
}
}
function PASREST-Logoff ([string]$Authorization) {
# Declaration
$webServicesLogoff = "$PVWA_URL/PasswordVault/WebServices/auth/Cyberark/CyberArkAuthenticationService.svc/Logoff"
# Authorization
$headerParams = @{}
$headerParams.Add("Authorization",$Authorization)
# Execution
try {
$logoffResult = Invoke-RestMethod -Uri $webServicesLogoff -Method POST -ContentType "application/json" -Header $headerParams -ErrorVariable logoffResultErr
Return $true
}
catch {
Write-Host "StatusCode: " $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription: " $_.Exception.Response.StatusDescription
Write-Host "Response: " $_.Exception.Message
Return $false
}
}
function PASREST-ADDSAFE ([string]$Authorization) {
# Declaration
$webServicesGA = "$PVWA_URL/PasswordVault/WebServices/PIMServices.svc/SAFES"
# Authorization
$headerParams = @{}
$headerParams.Add("Authorization",$sessionID)
$bodyParams = @{safe = @{SafeName = "TESTSafe3"; Description = "Testsafe for testing"; OLACenabled = "true"; ManagingCPM = "PasswordManager"; NumberOfDaysRetention = "7"}} | ConvertTo-JSON
write-host $bodyparams
# Execution
try {
$getAccountResult = Invoke-RestMethod -Uri $webServicesGA -Method POST -ContentType "application/json" -Headers $headerParams -Body $bodyParams -ErrorVariable getAccountResultErr
return $addsafeResult
}
catch {
Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
Write-Host "Response:" $_.Exception.Message
return $false
}
}
# Global Declaration
$PVWA_URL = "https://10.116.81.38"
$Keywords = "TestAccount"
$Safe = "TestSafe"
# Execute Logon
$sessionID = PASREST-Logon
# Error Handling for Logon
if ($sessionID -eq $false) {Write-Host "[ERROR] There was an error logging into the Vault." -ForegroundColor Red; break}
else {Write-Host "[INFO] Logon completed successfully." -ForegroundColor DarkYellow}
# Execute Addsafe
$addsafeResult = PASREST-Addsafe -Authorization $sessionID
if ($addsafeResult -eq $false) {Write-Host "[ERROR] There was an error adding the safe to the Vault."-ForegroundColor Red}
else {$addsafeResult.safes | Format-Table -Property SafeName}
# Execute Logoff
$logoffResult = PASREST-Logoff -Authorization $sessionID
if ($logoffResult -eq $true) {Write-Host "[INFO] Logoff completed successfully." -ForegroundColor DarkYellow}
else {Write-Host "[ERROR] Logoff was not completed successfully. Please logout manually using Authorization token:" $sessionID -ForegroundColor Red}
07-31-2018 03:32 AM
The script itself is fine. I ran it on my lab environment (only changed the PVWA URL) and it created the Safe successfully.
You can run "certmgr.msc" on your Windows client to add the CA that created the PVWA SSL cert, as a trusted CA.
07-31-2018 04:35 AM
08-06-2018 10:10 PM
Try this in your code
try
{
Write-Host "Adding TrustAllCertsPolicy type." -ForegroundColor Yellow
Add-Type -TypeDefinition @"
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;
}
}
"@
Write-Host "TrustAllCertsPolicy type added." -ForegroundColor Yellow
}
catch
{
Write-Host $_ -ForegroundColor "Yellow"
}
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
08-09-2018 09:20 AM
By default PowerShell uses TLS 1.0 for cmdlets like Invoke-RestMethod and Invoke-WebRequest but your CyberArk environment is most likely configured to require TLS 1.2, which is why you are getting the error you referenced. You can force PowerShell to use TLS 1.2 instead by putting this at the beginning of your script:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
08-10-2018 12:22 AM
12-15-2020 10:19 AM
Hi Vidit,
can you please advise what was the issue with the script execution as even i am facing this error while trying to execute from my system
Click Accept as Solution to acknowledge that the answer to your question has been provided.
The button appears next to the replies on topics you’ve started. The member who gave the solution and all future visitors to this topic will appreciate it!
These simple actions take just seconds of your time, but go a long way in showing appreciation for community members and the LIVEcommunity as a whole!
The LIVEcommunity thanks you for your participation!

