Learn how to use CyberArk REST API with PowerShell to automate account onboarding, safe creation, and password management. Step-by-step guide with script.
A REST API (Representational State Transfer – Application Programming Interface) allows applications to communicate over HTTPS.
π In simple terms:
It acts as a bridge between two systems to exchange data securely.
Imagine:
You want to update your status on Facebook automatically
Instead of manual login, you use a script
π That script connects using APIs provided by the application
Applications like:
AWS
Azure etc.
π Provide APIs for automation
Similarly, CyberArk provides APIs for:
β Account onboarding
β Safe creation
β Password rotation
β User management
β Reporting etc.
π You can view all APIs using Swagger:
https://PVWA/PasswordVault/Swagger
π This provides:
API list
Request format
Response structure
β Automate repetitive tasks
β Reduce manual effort
β Improve accuracy
β Enable bulk operations
β Integrate with external tools
π Onboard 1000 accounts automatically
π Instead of manual onboarding
π PowerShell is a scripting language used to:
β Automate OS-level tasks
β Call APIs
β Integrate systems
π Onboard 1000 accounts or more into CyberArk
Create a CSV file:
userName,address,safeName,platformID,secret
admin1,server1,Windows-Safe,WinDomain,Pass@123
admin2,server2,Unix-Safe,UnixSSH,Pass@456
π Save at:
C:\Users\Administrator\Desktop\Accounts.csv
https://PVWAURL/PasswordVault/API/auth/Cyberark/Logon
https://PVWAURL/PasswordVault/API/Accounts
https://PVWAURL/PasswordVault/API/auth/Logoff
You can use:
β CyberArk Authentication
β LDAP Authentication
β SAML Authentication
π Just update API path accordingly
β οΈ Never hardcode password
β Use secure input method
π Run PowerShell ISE as Administrator
π Provide password securely
π Script will:
Login
Read CSV
Onboard accounts
Logoff
# ==============================
# CyberArk Account Onboarding Script
# ==============================
# Variables
$PVWAURL = "https://PVWAURL/PasswordVault"
$AuthType = "Cyberark"
$Username = "Administrator"
$CSVPath = "C:\Users\Administrator\Desktop\Accounts.csv"
# Disable SSL warnings (if needed)
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
# ==============================
# Get Secure Password
# ==============================
$SecurePassword = Read-Host "Enter CyberArk Password" -AsSecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword)
$PlainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
# ==============================
# Login API
# ==============================
$LogonURL = "$PVWAURL/API/auth/$AuthType/Logon"
try {
Write-Host "Logging into CyberArk..."
$Body = @{
username = $Username
password = $PlainPassword
} | ConvertTo-Json
$Token = Invoke-RestMethod -Uri $LogonURL -Method POST -Body $Body -ContentType "application/json"
Write-Host "Login Successful!" -ForegroundColor Green
}
catch {
Write-Host "Login Failed: $_" -ForegroundColor Red
exit
}
# ==============================
# Read CSV
# ==============================
try {
$Accounts = Import-Csv -Path $CSVPath
}
catch {
Write-Host "Failed to read CSV file: $_" -ForegroundColor Red
exit
}
# ==============================
# Onboard Accounts
# ==============================
$AddAccountURL = "$PVWAURL/API/Accounts"
foreach ($Account in $Accounts) {
try {
Write-Host "Onboarding account: $($Account.userName)..."
$AccountBody = @{
name = $Account.userName
address = $Account.address
userName = $Account.userName
platformId = $Account.platformID
safeName = $Account.safeName
secret = $Account.secret
} | ConvertTo-Json -Depth 3
Invoke-RestMethod -Uri $AddAccountURL `
-Method POST `
-Headers @{ Authorization = $Token } `
-Body $AccountBody `
-ContentType "application/json"
Write-Host "Successfully onboarded: $($Account.userName)" -ForegroundColor Green
}
catch {
Write-Host "Failed to onboard $($Account.userName): $_" -ForegroundColor Red
}
}
# ==============================
# Logoff
# ==============================
$LogoffURL = "$PVWAURL/API/auth/Logoff"
try {
Invoke-RestMethod -Uri $LogoffURL `
-Method POST `
-Headers @{ Authorization = $Token }
Write-Host "Logged off successfully!" -ForegroundColor Green
}
catch {
Write-Host "Logoff Failed: $_" -ForegroundColor Red
}
π Uses API to authenticate
π Returns session token
π Reads account details
π Loop through each account
π Calls API to create account
π Ends session securely
β Never hardcode passwords
β Use secure string input
β Use try-catch for error handling
β Validate CSV before execution
β Test in lower environment first
π Wrong credentials / Auth type
π Wrong endpoint / URL
π Missing Safe / Platform
π Certificate validation problem
CyberArk REST API is a game-changer for automation
π Manual work → Automated workflows
π Time-consuming → Efficient
π‘ If you master this → You move from Admin → Automation Engineer
Your email address will not be published. Required fields are marked*
Copyright 2022 SecApps Learning. All Right Reserved
Comments ()