Jump to content

Parsing $reportID to $reportFileLink to download a report via the API


Recommended Posts

I have two separate scripts:

# Define output stream type
    [OutputType([object])]

    # Define runbook input params
    Param
    (
        # API Params
        #[Parameter (Mandatory= $true)]
        [string] $reportId = "842",
        [string] $outputFormat,
        [string] $runtimeParameter,
        [string] $comment, 
        [string] $publish
        )
 
    # Add XMLMC params
    Add-HB-Param "reportId" $reportId $false
    Add-HB-Param "outputFormat" $outputFormat $false
    Add-HB-Param "runtimeParameter" $runtimeParameter $false
    Add-HB-Param "comment" $comment $false
    Add-HB-Param "publish" $publish $false
   
   # Invoke XMLMC call, output returned as PSObject
   $xmlmcOutput = Invoke-HB-XMLMC "reporting" "reportRun"
   $exceptionName = "" 
   $exceptionSummary = ""
   
    # Read output status
    if($xmlmcOutput.status -eq "ok") {
        if($xmlmcOutput.params.runId -and $xmlmcOutput.params.runId -ne ""){
            $runId = $xmlmcOutput.params.runId
            }
        if($xmlmcOutput.params.exceptionName -and $xmlmcOutput.params.exceptionName -ne ""){
            $exceptionName = $xmlmcOutput.params.exceptionName
            $exceptionSummary = $xmlmcOutput.params.exceptionDescription
        }
    }

    # Build resultObject to write to output
    $resultObject = New-Object PSObject -Property @{
        Status = $xmlmcOutput.status
        Error = $xmlmcOutput.error
        ExceptionName = $exceptionName
        ExceptionSummary = $exceptionSummary
        runId = $runId
    }

    if($resultObject.Status -ne "ok" -or $exceptionName -ne ""){
        Write-Error $resultObject
    } else {
        Write-Output $runId
       }

This generates a run ID for a report which downloads successfully if I manually enter the run ID into:


# Define output stream type
    [OutputType([object])]

    # Define runbook input params
    Param
    (
        # API Params
        #[Parameter (Mandatory= $true)]
        [string] $runId = "45234"
        )
# Add XMLMC params
    Add-HB-Param "runId" $runId $false
    
   # Invoke XMLMC call, output returned as PSObject
   $xmlmcOutput = Invoke-HB-XMLMC "reporting" "reportRunGetStatus"
   $exceptionName = "" 
   $exceptionSummary = ""
   
    # Read output status
    if($xmlmcOutput.status -eq "ok") {
        if($xmlmcOutput.params.reportRun -and $xmlmcOutput.params.reportRun -ne ""){
            $reportRun = $xmlmcOutput.params.reportRun
            }
            if($xmlmcOutput.params.files -and $xmlmcOutput.params.files -ne ""){
            $files = $xmlmcOutput.params.files
            }
            if($xmlmcOutput.params.delivery -and $xmlmcOutput.params.delivery -ne ""){
            $delivery = $xmlmcOutput.params.delivery
            }
        if($xmlmcOutput.params.exceptionName -and $xmlmcOutput.params.exceptionName -ne ""){
            $exceptionName = $xmlmcOutput.params.exceptionName
            $exceptionSummary = $xmlmcOutput.params.exceptionDescription
        }
    }

    # Build resultObject to write to output
    $resultObject = New-Object PSObject -Property @{
        Status = $xmlmcOutput.status
        Error = $xmlmcOutput.error
        ExceptionName = $exceptionName
        ExceptionSummary = $exceptionSummary
        reportRun = $reportRun
        files = $files
        delivery = $delivery
    }

    if($resultObject.Status -ne "ok" -or $exceptionName -ne ""){
        Write-Error $resultObject
    } else {
    $reportFileLink = $files | Select -ExpandProperty name
    $reportID = "123"
    $HornbillKey = "xxxxxxx"
    [string] $url = "https://mdh-p01-api.hornbill.com/iposervicedesk/dav/" + "reports/" + $reportID + "/" + $reportFileLink
    $headers = @{}
    if ($HornbillKey -ne $null) {
        $headers["Authorization"] = "ESP-APIKEY $HornbillKey"
    }
    try {
        $result = Invoke-WebRequest -Uri $url -Method GET -OutFile "./$reportFileLink" -Headers $headers
        } 
    Catch {
        Write-Error $_.Exception.Message
    }
    }

What I want to do is parse the run ID that is output from the first piece of code so that the report downloads without having to manually specify the run ID to the second piece of code.

. "./script1.ps1" - runId $runId

Doesn't have the desired effect. 

Link to comment
Share on other sites

Hi @Rob Gething,

You might be more successful on a powershell script forum for this one.

A Google Search on "powershell pass variable to another script" got me this promising solution (amongst others): https://stackoverflow.com/questions/26731933/how-do-i-pass-arguments-or-variables-from-one-powershell-script-to-another

Link to comment
Share on other sites

Since there’s just the one parameter for the second script, PowerShell will intelligently match the output of one script to the input of the next, when piped.

‘.\GetRunID.ps1’ -ReportID xxx | ‘.\DownloadReport.ps1’ returns:

 

@{delivery=; ExceptionName=; files=;

Error=Failed to retrieve report info for Id: 45234; reportRun=; Status=fail; ExceptionSummary=}

If I run the scripts individually I get this from GetRunID:

Status           : ok

Error            :

ExceptionName    :

runId            : 51542

ExceptionSummary :

.\reportRunGetStatus.ps1 -runId 51542 has actually successfully downloaded the file but that’s because I ran GetRunID first, got the reportID and then ran .\reportRunGetStatus.ps1 -runId 51542.

I can’t get 51542 to parse to the other script through a pipe and download the file.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...