Hi everyone. I'm more used to building data pipelines, but my team needed a way to onboard service accounts into BeyondTrust in bulk from a CSV. The API is great, but doing it manually for hundreds of accounts... 😅
I built a PowerShell wrapper script to handle the authentication and batch creation. It's my first time working with the BeyondTrust API directly. Maybe it's useful for someone else?
The core part that handles the API call for creating a Managed Account looks like this:
```powershell
function New-BulkManagedAccount {
param(
[string]$ApiUrl,
[string]$ApiKey,
[string]$CsvPath
)
$accounts = Import-Csv -Path $CsvPath
$headers = @{
"Authorization" = "Bearer $ApiKey"
}
foreach ($account in $accounts) {
$body = @{
SystemId = $account.SystemId
AccountName = $account.AccountName
DomainName = $account.DomainName
# ... other required properties
} | ConvertTo-Json
try {
Invoke-RestMethod -Uri "$ApiUrl/ManagedAccounts" -Method Post -Headers $headers -Body $body -ContentType "application/json"
Write-Host "Account $($account.AccountName) created successfully."
}
catch {
Write-Error "Failed to create $($account.AccountName): $($_.Exception.Message)"
}
}
}
```
Things I learned:
* The API expects the `SystemId` (the asset ID) upfront. I had to pre-create those.
* Rate limiting is importantβI added a `Start-Sleep -Seconds 1` after every 10 requests.
* Outputting results to a log file is crucial for retries.
I'm wondering about error handling. Should I build in a retry logic with backoff? Also, has anyone integrated something like this into an orchestration tool (like Airflow) for scheduled onboarding?