Created
September 7, 2023 16:29
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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