Skip to content

Instantly share code, notes, and snippets.

@infamousjoeg
Created September 7, 2023 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save infamousjoeg/7a14401908e6e6539a075bfa376e02db to your computer and use it in GitHub Desktop.
Save infamousjoeg/7a14401908e6e6539a075bfa376e02db to your computer and use it in GitHub Desktop.
Search CyberArk for Port Number and Update to New Port Number in PowerShell
# Global Variables
$baseUrl = "https://cyberark.joegarcia.dev" # CHANGE ME
$authType = "ldap" # CHANGE ME
$portToSearchFor = 3306 # CHANGE ME
$newPortValue = 3307 # CHANGE ME
# Logon Variables
$credentials = Get-Credential
$logonRequestUri = "${baseUrl}/PasswordVault/api/auth/${authType}/logon"
$logoffRequestUri = "${baseUrl}/PasswordVault/api/auth/logoff"
$authBody = @"
{
`"username`": `"$($credentials.UserName)`",
`"password`": `"$($credentials.GetNetworkCredential().Password)`",
`"concurrentSession`": `"true`"
}
"@
# Accounts Variables
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$accountsUrl = "${baseUrl}/PasswordVault/api/Accounts"
# Authenticate to CyberArk
$response = Invoke-RestMethod -Uri $logonRequestUri -Method POST -Body $authBody -ContentType "application/json" -Verbose
if ($response -ne "") {
# Store token in headers for future requests
$headers.Add("Authorization", $response.Trim("`""))
# Search for accounts with portToSearchFor
$searchResponse = Invoke-RestMethod -Uri "${accountsUrl}?search=${portToSearchFor}&searchType=startswith" -Headers $headers -ContentType "application/json" -Verbose
# Check if there are any accounts to update
if ($searchResponse.count -gt 0) {
foreach ($account in $searchResponse.value) {
$updateURL = $accountsURL + "/" + $account.id
$updateBody = @"
[{
`"op`": `"replace`",
`"path`": `"/platformAccountProperties/Port`",
`"value`": `"$newPortValue`"
}]
"@
# Update the port for each account
Invoke-RestMethod -Uri $updateURL -Method PATCH -Headers $headers -Body $updateBody -ContentType "application/json" -Verbose -ErrorAction Stop
}
Write-Host "Successfully updated all accounts' port to $newPortValue."
} else {
Write-Host "No accounts found with port $portToSearchFor."
}
} else {
Write-Error "Authentication failed. Please check your credentials."
}
# Logoff
Invoke-RestMethod -Uri "$logoffRequestUri" -Method POST -Headers $headers -ErrorAction SilentlyContinue | Out-Null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment