Robert Gething
Robert Gething

Reputation: 1

Parse the output from one powershell script as the input of another

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.

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.

Upvotes: 0

Views: 801

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174445

Change the param block in the second script to accept input via the pipeline:

Param(
    # API Params
    [Parameter(Mandatory=$true, ValueFromPipeline = $true)]
    [string] $runId = "45234"
)

Now you can compose a single pipeline from both scripts:

path\to\GetRunID.ps1 -ReportId 123 | path\to\script.ps1

Upvotes: 1

Related Questions