Important Update: Community URLs redirect issues are partially resolved. Learn More. .

cancel
Showing results for 
Search instead for 
Did you mean: 

Question on retrieving report contents via API - PowerShell

AlexanderHatfie
Contributor II

I'm trying to output the contents of a report via a couple scripts I found in other discussion boards that I put together. I keep receiving various errors on everything that I try, so looking to see if someone can help me out. The version below is outputting "Unable to validate session." that implies I'm not authenticating. My $session_token returns correctly though. Any advice?

 

Apologies for poor formatting:

 

$base_url = "https://fakeurl.com/RSAarcher"
$instance_name = "fakeinstance"
$user_domain = ""
$username = "fakeuser"
$password = "fakepass"

try
{
$api_url = $base_url + "/ws/general.asmx"
if ($user_domain -eq "") {
$ws = New-WebServiceProxy -Uri $api_url -Class General -Namespace webservice -ErrorAction Stop
$session_token = $ws.CreateUserSessionFromInstance($username, $instance_name, $password)
}
else {
$ws = New-WebServiceProxy -Uri $api_url -Class General -Namespace webservice -ErrorAction Stop
$session_token = $ws.CreateDomainUserSessionFromInstance($username, $instance_name, $password, $user_domain)
}
$session_token
}
catch {
$session_token = $null
$_.Exception|Format-List -Force
exit
}


function Error($exception) {
$exception | Format-List -Force
#exit
}

function SearchRecordsByReport($session_token, $reportGUID) {
$api_url = $base_url + "/ws/search.asmx"
$xml_file = New-Object System.Xml.XmlDocument
try {
$ws = New-WebServiceProxy -Uri $api_url -Class Search -Namespace webservice
$page = 1
Do {
[xml] $xdoc = $ws.SearchRecordsByReport($session_token, $reportGUID, $page)
if ($page -eq 1) {
$xml_file = $xdoc
}
else {
ForEach ($record in $xdoc.SelectNodes("/Records/Record")) {
$records = $xml_file.SelectSingleNode("/Records")
$records.AppendChild($xml_file.ImportNode($record, $true)) | Out-Null
}
}
$page++
} While ($xdoc.Records.Record.Count -gt 0)
}
catch {
Error -exception $_.Exception
}
$xml_file.InnerXml
return $xml_file.InnerXml
}

I then run the following:

$guid = "1234567890"

$testing = SearchRecordsByReport($session_token, $guid)

7 REPLIES 7

Ilya_Khen
Champion III

Alexander Hatfield,

 

If you get token, means you was authenticated. Wonder if could be related to the possible defect Bodie Minster talked about:

https://community.rsa.com/thread/201577

Ilya,

 

I don't think this is similar. The issue they are having deals with updating content that they are able to pull, and I am not able to pull data at all. I think I'm stuck on the step before theirs.

Reason, I thought about similar, that if you got proper session_token and passed it, you hardly could be having invalid token. UNLESS, you are doing 2nd auth request or in meantime someone else using the same credentials. I am assuming also that code is correct, I did not check it, and WS node is using proper authentication

 

Are these working fine for you?

https://community.rsa.com/docs/DOC-83451

https://community.rsa.com/message/905900

PaulDouglas
Archer Employee
Archer Employee

Looking at your code, I feel like you're calling SearchRecordsByReport which requires $session_token as input without having a session_token set outside of the try section.

I would suggest wrapping the sessiontoken generation into a login function that returns sessiontoken into a variable and either call it before you call the SearchRecordsByReport function, or call it within the function and remove the $session_token variable from the SearchRecordsByReport function.

From a security point of view, I would also suggest you create a logout function to make sure the sessiontoken doesn't get reused.

$session_token is indeed being saved to a variable this way. I am able to manually call it and see the token outside of the try section.

Well, this just became very interesting.

I copied your code, and I'm running on my system, and there seems to be some parameter binding issue on calling your search function. The report GUID is being appended to the session token, and reportGUID is then empty.

I made some slight changes to function name and casting the variables, just for testing, but here is a quick shot of what I'm seeing

pastedImage_1.png

When I remove the function, and just have it as one continuous script, it works perfectly.

PaulDouglas
Archer Employee
Archer Employee

Okay, I've made some changes and this is now working in my system.

You'll notice that I've changed the format of the parameter declaration in the search function, as well as the way to invoke the function. I've copied the entire script for ease of viewing.

$base_url = "https://fakeserver/"
$instance_name = "instancename"
$user_domain = ""
$username = "fakeusername"
$password = "fakepassword"
$sessiontoken = ""
$reportguid = "1234567890"

try
{
$api_url = $base_url + "/ws/general.asmx"
if ($user_domain -eq "") {
$ws = New-WebServiceProxy -Uri $api_url -Class General -Namespace webservice -ErrorAction Stop
$sessiontoken = $ws.CreateUserSessionFromInstance($username, $instance_name, $password)
}
else {
$ws = New-WebServiceProxy -Uri $api_url -Class General -Namespace webservice -ErrorAction Stop
$sessiontoken = $ws.CreateDomainUserSessionFromInstance($username, $instance_name, $password, $user_domain)
}
$sessiontoken
}
catch {
$sessiontoken = $null
$_.Exception|Format-List -Force
exit
}

function Error($exception) {
$exception | Format-List -Force
#exit
}

function SearchByReport() {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$session_token,

[Parameter(Mandatory)]
[string]$reportGUID
)

$api_url = $base_url + "/ws/search.asmx"
$xml_file = New-Object System.Xml.XmlDocument
try {
$ws = New-WebServiceProxy -Uri $api_url -Class Search -Namespace webservice
$page = 1
Do {
[xml] $xdoc = $ws.SearchRecordsByReport($session_token, $reportGUID, $page)
if ($page -eq 1) {
$xml_file = $xdoc
}
else {
ForEach ($record in $xdoc.SelectNodes("/Records/Record")) {
$records = $xml_file.SelectSingleNode("/Records")
$records.AppendChild($xml_file.ImportNode($record, $true)) | Out-Null
}
}
$page++
} While ($xdoc.Records.Record.Count -gt 0)
}
catch {
Error -exception $_.Exception
}
$xml_file.InnerXml
return $xml_file.InnerXml
}

$testing = SearchByReport -session_token $sessiontoken -reportGUID $reportguid