- Access exclusive content
- Connect with peers
- Share your expertise
- Find support resources
08-23-2023 09:04 AM
Ok, so I am trying to follow this doc.
https://docs.cyberark.com/EPM/Latest/en/Content/WebServices/SAMLAuthentication.htm
Import-Module PS-SAML-Interactive
$LoginURL = "Okta embedded app link"
$Value = New-SAMLInteractive -LoginIDP $LoginURL -Verbose
$Header = @{
'Content-Type' = 'application/x-www-form-urlencoded';
}
$Body = @{
'Key' = 'SAMLResponse';
'Value' = "$Value";
}
$JsonBody = ConvertTo-JSON $Body
Invoke-RestMethod -Uri "https://login.epm.cyberark.com/SAML/Logon" -Method Post -Headers $Header -Body $JsonBodyThis is the code I am trying to use but it just keeps failing and I have no idea why. I found PS-SAML-Interactive module from a post in the community for a different cyberark product but it doesn't seem to work. I even tried manually grabbing the token from SAML tracer and that doesn't work either.
Does anyone have a working PowerShell script for SAML auth?
08-24-2023 06:29 AM
function New-SAMLInteractive{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string] $LoginIDP
)
Begin{
$RegEx = '(?i)name="SAMLResponse"(?: type="hidden")? value=\"(.*?)\"(?:.*)?\/>'
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Web
}
Process{
# create window for embedded browser
$form = New-Object Windows.Forms.Form
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
$form.Width = 640
$form.Height = 700
$form.showIcon = $false
$form.TopMost = $true
$web = New-Object Windows.Forms.WebBrowser
$web.Size = $form.ClientSize
$web.Anchor = "Left,Top,Right,Bottom"
$web.ScriptErrorsSuppressed = $true
$form.Controls.Add($web)
$web.Navigate($LoginIDP)
$web.add_Navigating({
if ($web.DocumentText -match "SAMLResponse"){
$_.cancel = $true
if ($web.DocumentText -match $RegEx){
$form.Close()
$Script:SAMLResponse = $(($Matches[1] -replace '+', '+') -replace '=', '=')
}
}
})
# show browser window, waits for window to close
if([system.windows.forms.application]::run($form) -ne "OK") {
if ($null -ne $Script:SAMLResponse){
Write-Output $Script:SAMLResponse
$form.Close()
Remove-Variable -Name SAMLResponse -Scope Script -ErrorAction SilentlyContinue
}
Else{
throw "SAMLResponse not matched"
}
}
}
End{
$form.Dispose()
}
}
#Logon
$SAMlResponse=New-SAMLInteractive -LoginIDP 'IDP_URL_HERE'
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$url = "https://PVWA.COM/PasswordVault/api/auth/saml/logon"
$body = @{
concurrentSession='true'
apiUse='true'
SAMLResponse=$SAMlResponse
}
$token = Invoke-RestMethod -Uri $url -Method Post -body $body -ContentType 'application/x-www-form-urlencoded'
$headers.Add("Authorization", "$token")
#Logon EndCredits for the original PS Script: https://github.com/allynl93/getSAMLResponse-Interactive
08-24-2023 06:29 AM
function New-SAMLInteractive{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string] $LoginIDP
)
Begin{
$RegEx = '(?i)name="SAMLResponse"(?: type="hidden")? value=\"(.*?)\"(?:.*)?\/>'
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Web
}
Process{
# create window for embedded browser
$form = New-Object Windows.Forms.Form
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
$form.Width = 640
$form.Height = 700
$form.showIcon = $false
$form.TopMost = $true
$web = New-Object Windows.Forms.WebBrowser
$web.Size = $form.ClientSize
$web.Anchor = "Left,Top,Right,Bottom"
$web.ScriptErrorsSuppressed = $true
$form.Controls.Add($web)
$web.Navigate($LoginIDP)
$web.add_Navigating({
if ($web.DocumentText -match "SAMLResponse"){
$_.cancel = $true
if ($web.DocumentText -match $RegEx){
$form.Close()
$Script:SAMLResponse = $(($Matches[1] -replace '+', '+') -replace '=', '=')
}
}
})
# show browser window, waits for window to close
if([system.windows.forms.application]::run($form) -ne "OK") {
if ($null -ne $Script:SAMLResponse){
Write-Output $Script:SAMLResponse
$form.Close()
Remove-Variable -Name SAMLResponse -Scope Script -ErrorAction SilentlyContinue
}
Else{
throw "SAMLResponse not matched"
}
}
}
End{
$form.Dispose()
}
}
#Logon
$SAMlResponse=New-SAMLInteractive -LoginIDP 'IDP_URL_HERE'
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$url = "https://PVWA.COM/PasswordVault/api/auth/saml/logon"
$body = @{
concurrentSession='true'
apiUse='true'
SAMLResponse=$SAMlResponse
}
$token = Invoke-RestMethod -Uri $url -Method Post -body $body -ContentType 'application/x-www-form-urlencoded'
$headers.Add("Authorization", "$token")
#Logon EndCredits for the original PS Script: https://github.com/allynl93/getSAMLResponse-Interactive
08-24-2023 06:51 AM
Your $body parameter is what fixed it for me. I just replaced that and removed the convert json and it is now working.
You're awesome thank you very much for the help!!!
08-28-2023 03:09 AM
Hi @Derek Hartman
Can you please share your powershell code(after masking the environment URLs) using which you are able to authenticate to CyberArk via SAML? I am unable to do it getting error "Sorry, but we’re having trouble signing you in.
AADSTS750054: SAMLRequest or SAMLResponse must be present as query string parameters in HTTP request for SAML Redirect binding"😞
08-28-2023 07:00 AM
@Sabyasachi Patnaik
Sure, here is my code.
Import-Module PS-SAML-Interactive
$LoginURL = "Okta embedded app link"
$Value = New-SAMLInteractive -LoginIDP $LoginURL -Verbose
$Header = @{
'Content-Type' = 'application/x-www-form-urlencoded';
}
$Body = @{
concurrentSession='true'
apiUse='true'
SAMLResponse=$Value
}
Invoke-RestMethod -Uri "https://login.epm.cyberark.com/SAML/Logon" -Method Post -Headers $Header -Body $Body
08-31-2023 08:07 AM
Thank you @Derek Hartman
Let me validate.
09-08-2026 09:08 AM
In your case, you did for IDP initiated not for SP Initiated right?
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!

