Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Render RDL through powershell

Status
Not open for further replies.

brage98

IS-IT--Management
Sep 8, 2010
27
CA
I have the following code that properly renders an RDL to a file.
I then email the file as an attachment through an email.

The Question
Users want to be able to see the content of the attachment in the body of the email as well. How would I go about rendering the output file (which is usually in .pdf) right in the body of the email?

Code:
##################################################################
[void][system.Reflection.Assembly]::LoadWithPartialName("System")
[void][system.Reflection.Assembly]::LoadWithPartialName("System.Net")
[void][system.Reflection.Assembly]::LoadWithPartialName("System.Net.WebRequest")
[void][system.Reflection.Assembly]::LoadWithPartialName("System.Net.HttpWebResponse")
[void][system.Reflection.Assembly]::LoadWithPartialName("System.IO")
##################################################################
function Render-Report([string]$reportServerName, [string]$pathToRDL, [string]$reportName, [string]$outputFormat, [string]$reportParams)
{
    #validate the inputs:
    $reportFileExt = ".pdf"
    if ($outputFormat -AND $outputFormat.Trim().ToUpper() -eq "EXCEL")   { $reportFileExt = "xls" }
    elseif ($outputFormat -AND $outputFormat.Trim().ToUpper() -eq "PDF"){ $reportFileExt = "pdf" }
    else {Throw "Currently only 'EXCEL' AND 'PDF' are supported"}

    #Generate report file and folder structure:
    $reportFolder = "../Reports"
    if (-NOT $(Test-Path $reportFolder)) { New-Item $reportFolder -type directory }
    $reportFilePathnName = "$reportFolder/$reportName" + "_" + (Get-Date -format "yyyyMMdd_HHmmss") + ".$reportFileExt"

    #Parameterized URL to the report (including the parameters etc):
    $url = Create-ReportURL $reportServerName $pathToRDL $reportName $outputFormat $reportParams

    log-info "Report url: $url"

    #Call report-server and get the report data:
    $req = [System.Net.HttpWebRequest] ([System.Net.WebRequest]::Create($url)) 
    $req.UseDefaultCredentials = $true;
    $req.Timeout = 1000 * 60 * 15; #timeout 15 minutes 
    $req.Method = "GET";
    $resp = [System.Net.HttpWebResponse]($req.GetResponse());
    $respStream = $($resp.GetResponseStream());

    #render the report
    $fs = New-Object System.IO.FileStream($reportFilePathnName, [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write)
    $byteCount = 1;
    $bytes = new-object byte[] 256
    while ($byteCount -gt 0)
    {
        $byteCount = $respStream.Read($bytes, 0, 256);
        $fs.Write($bytes, 0, $byteCount);
    }
    $fs.Flush();
    $fs.Close();

    return $reportFilePathnName
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top