can someone help me with a powershell script that fetch reports from identity

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

can someone help me with a powershell script that fetch reports from identity

L1 Bithead

we want a powershell script that fetch reports from identity for audit purpose

any documents, script with readme

 

1 accepted solution

Accepted Solutions

L3 Networker

Likewise, use it at your own discretion and risk.

 

Below is an example script to hit the RedRock endpoint and run a custom SQL query. The setup assumes you have created an OAuth Client app with the appropriate scope, and an OAuth confidential client (service account with the appropriate roles\permissions to access the data you are looking for and entitlement to the OAuth client app).

 

Custom OAuth2 Client | CyberArk Docs

Add service users | CyberArk Docs

 

After the above is stood up should just need to populate the TenantUrl, AppId, ClientId values to be able to generate the bearer token then you can customize the sql string to contain the report info you are interested in gathering (in this example it grabs info on the roles defined in the tenant). You may need to tweak the scope parameter if you use an alternate value in your client app.

$TenantUrl = "" # or https://acme.my.idaptive.app $AppId = "" # from the OAuth2 Client app $ClientId = "" # e.g., apiuser@yourtenant $ClientSecret= Read-Host -AsSecureString "Service user password" | ` ForEach-Object { [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($_)) }   $TokenUrl = "$TenantUrl/oauth2/token/$AppId"   $body = @{ grant_type = 'client_credentials' client_id = $ClientId client_secret = $ClientSecret scope = 'reporting' # scope created with REST Regex 'Redrock/query' }   $tokenResponse = Invoke-RestMethod -Method POST -Uri $TokenUrl ` -ContentType 'application/x-www-form-urlencoded' -Body $body   $headers = @{ Authorization = "Bearer $($tokenResponse.access_token)" }   $headers | Format-List   #--------------------------------------------------------------------------------------   function Invoke-CyberArkIdentityQuery { param( [Parameter(Mandatory)] [string]$TenantUrl, [Parameter(Mandatory)] [hashtable]$Headers, [Parameter(Mandatory)] [string]$Sql, [int]$PageNumber = 1, [int]$PageSize = 1000, [int]$Limit = 1000, [int]$Caching = -1, # live data, write to cache [bool]$Ascending = $true )   $uri = "$TenantUrl/Redrock/query" $body = @{ Script = $Sql args = @{ PageNumber = $PageNumber PageSize = $PageSize Limit = $Limit Caching = $Caching direction = $Ascending SortBy = $SortBy } } | ConvertTo-Json -Depth 5   Invoke-RestMethod -Method POST -Uri $uri -Headers $Headers ` -ContentType 'application/json' -Body $body }   # Example: list user IDs and usernames, sorted case-insensitive #$sql = "Select ID, Username from User ORDER BY Username COLLATE NOCASE" #$sql = "Select * from Role WHERE RoleType = 'PrincipalList' ORDER BY Name COLLATE NOCASE" $sql = "Select * from Role ORDER BY Name COLLATE NOCASE" $result = Invoke-CyberArkIdentityQuery -TenantUrl $TenantUrl -Headers $headers -Sql $sql   $result.Result.Columns | ft $result.Result.Results | ForEach-Object { $_.Row } | Select-Object ID, Name, ReadOnly, IsHidden, RoleType | Out-GridView

 

View solution in original post

9 REPLIES 9

L3 Networker

Not an expert, but you can hit the RedRock\Query endpoint to generate reports. I've only done this for some specific custom use cases, not to export output from the stock reports. Will share the OAuth client account you create to hit the endpoint will probably wind up needing the Read Only System Administration right as there is the right to access the reports but then you also need access to the underlying data that the report queries. This will materialize in the request returning success but no results when you know there should be results.

 

How To Use Developer Tools To Determine API Calls

L4 Transporter

Hello @mohd azeem khan​ 

 

There is a KB article explaining steps to generate Reports using REST API and PowerShell scripts.

 

Privilege Cloud Shared Services - Automate with REST API & PowerShell

 

I hope this will help you!

L4 Transporter

Hello @mohd azeem khan​ 

 

There is a KB article explaining steps to generate Reports using REST API and PowerShell scripts.

 

Privilege Cloud Shared Services - Automate with REST API & PowerShell

 

I hope this will help you!

L1 Bithead

@Rohini Arora​ 

 

in this above article report fetching is not present

help me on this

 

L4 Transporter

Hello @mohd azeem khan​ 

 

For Reports

  • Identity Administration > Core Services > Reports > click ‘OAuth Events report’ > Set report filters (time range) > OK
    • Report is displayed > AppId ‘_widgetOAuthApp’ corresponds to Shared Services API tokens

 

I hope this helps you!

L4 Transporter

Hello @mohd azeem khan​ 

 

For Reports

  • Identity Administration > Core Services > Reports > click ‘OAuth Events report’ > Set report filters (time range) > OK
    • Report is displayed > AppId ‘_widgetOAuthApp’ corresponds to Shared Services API tokens

 

I hope this helps you!

L4 Transporter

Hi @mohd azeem khan​ 

Several community members have shared answers to your question, could you let us know if any of these resolved your issue?

If so, please mark the appropriate answer as “Select as Best” to help other members find the solution easily.

If the issue is still unresolved, I recommend raising a ticket with our support team for further assistance.

Thank you,

Shaked

L4 Transporter

CyberArk does not provide any officially documented or supported PowerShell scripts for generating reports. While I’m not a PowerShell expert, there is a script you might be able to adapt using RedRock queries:

https://github.com/jeffrechten/epv-api-scripts/blob/master/Identity%20Role%20Management/Role-Management.ps1

Please note: This script is not supported by CyberArk. Use it at your own discretion and risk.

L3 Networker

Likewise, use it at your own discretion and risk.

 

Below is an example script to hit the RedRock endpoint and run a custom SQL query. The setup assumes you have created an OAuth Client app with the appropriate scope, and an OAuth confidential client (service account with the appropriate roles\permissions to access the data you are looking for and entitlement to the OAuth client app).

 

Custom OAuth2 Client | CyberArk Docs

Add service users | CyberArk Docs

 

After the above is stood up should just need to populate the TenantUrl, AppId, ClientId values to be able to generate the bearer token then you can customize the sql string to contain the report info you are interested in gathering (in this example it grabs info on the roles defined in the tenant). You may need to tweak the scope parameter if you use an alternate value in your client app.

$TenantUrl = "" # or https://acme.my.idaptive.app $AppId = "" # from the OAuth2 Client app $ClientId = "" # e.g., apiuser@yourtenant $ClientSecret= Read-Host -AsSecureString "Service user password" | ` ForEach-Object { [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($_)) }   $TokenUrl = "$TenantUrl/oauth2/token/$AppId"   $body = @{ grant_type = 'client_credentials' client_id = $ClientId client_secret = $ClientSecret scope = 'reporting' # scope created with REST Regex 'Redrock/query' }   $tokenResponse = Invoke-RestMethod -Method POST -Uri $TokenUrl ` -ContentType 'application/x-www-form-urlencoded' -Body $body   $headers = @{ Authorization = "Bearer $($tokenResponse.access_token)" }   $headers | Format-List   #--------------------------------------------------------------------------------------   function Invoke-CyberArkIdentityQuery { param( [Parameter(Mandatory)] [string]$TenantUrl, [Parameter(Mandatory)] [hashtable]$Headers, [Parameter(Mandatory)] [string]$Sql, [int]$PageNumber = 1, [int]$PageSize = 1000, [int]$Limit = 1000, [int]$Caching = -1, # live data, write to cache [bool]$Ascending = $true )   $uri = "$TenantUrl/Redrock/query" $body = @{ Script = $Sql args = @{ PageNumber = $PageNumber PageSize = $PageSize Limit = $Limit Caching = $Caching direction = $Ascending SortBy = $SortBy } } | ConvertTo-Json -Depth 5   Invoke-RestMethod -Method POST -Uri $uri -Headers $Headers ` -ContentType 'application/json' -Body $body }   # Example: list user IDs and usernames, sorted case-insensitive #$sql = "Select ID, Username from User ORDER BY Username COLLATE NOCASE" #$sql = "Select * from Role WHERE RoleType = 'PrincipalList' ORDER BY Name COLLATE NOCASE" $sql = "Select * from Role ORDER BY Name COLLATE NOCASE" $result = Invoke-CyberArkIdentityQuery -TenantUrl $TenantUrl -Headers $headers -Sql $sql   $result.Result.Columns | ft $result.Result.Results | ForEach-Object { $_.Row } | Select-Object ID, Name, ReadOnly, IsHidden, RoleType | Out-GridView

 

  • 1 accepted solution
  • 231 Views
  • 9 replies
  • 0 Likes
Like what you see?

Show your appreciation!

Click Like if a post is helpful to you or if you just want to show your support.

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!