on ‎2019 Jul 25 8:57 AM
I have a powershell script that executes a Crystal Report (there is no subreport) which takes three parameters, retrieves it's data from SQL Server, and writes/saves the report to a network share.
I am able to execute the powershell script successfully.
Another user is not able to execute the script successfully.
This user:
1. Is able to UNC to the network share, and
2. Is able to read and write to the network share
3. Is able to successfully write the error to the same SQL Server in which the report gets its data (See Try...Catch statement in the code below).
The error this user is getting, is the following: Exception calling "ExportToDisk" with "2" argument(s): "Failed to open the connection. [Name of Report] {16D21D5B-3494-5B3E-B3D9-3DFF7D467GDB}.rpt"
The following is the powershell code as pertains to the ExportToDisk error:
$BeginDate = Read-Host 'Enter begin date using format MM/DD/YYYY '
$date = $BeginDate
$EndDate = $BeginDate
$BeginDate = $BeginDate + " 02:00:00"
$EndDate = $EndDate + " 01:59:59"
$EndDate = ([datetime]$EndDate).AddDays(1) #Get datetime stamp to append to file name, in the form of YYYYMMDD_YYYYMMDD_HHMMSS
$CurrentDate = get-date
$CurrentDate = $CurrentDate.ToString("yyyyMMdd")
$date = $date.Replace("/", "")
$time = get-date -Format HH:mm:ss
$time = $time.Replace(":", "")
$datetime = $date + "_" + $CurrentDate + "_" + $time #YYYYMMDD_YYYYMMDD_HHMMSS
[string] $UserName = $env:UserName
$Property = "SiteName"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=ServerName;Database=Reporting;Integrated Security=True" $SqlConnection.Open()
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.Connection = $SqlConnection
[reflection.assembly]::LoadWithPartialName('CrystalDecisions.Shared') | Out-Null [reflection.assembly]::LoadWithPartialName('CrystalDecisions.CrystalReports.Engine') | Out-Null
$report = New-Object CrystalDecisions.CrystalReports.Engine.ReportDocument
[string] $LocalReportPath = "C:\Users\$UserName\Documents\CrystalReports\[Name of Report].rpt"
$ReportPath = "S:\Reporting\SiteName\" #This is for saving to the SQL table for historical or error occurrence. $FileLoc = "\\ServerName\Reporting\SiteName\" #This is path for saving to the network server, which equates to the S drive on ServerName.
$ReportNameLive = "TableGames_XX_" + $datetime + ".pdf"
$FileName = $FileLoc + $ReportNameLive
$ReportFullPath = $ReportPath + $ReportNameLive
$report.SetParameterValue("@Property", $Property)
$report.SetParameterValue("@BeginDate", $BeginDate)
$report.SetParameterValue("@EndDate", $EndDate)
$SqlCmd.CommandText = "INSERT dbo.ReportTracker ( UserName, ReportNameLive, [FileName], ReportFullPath, BeginDateParam, EndDateParam ) VALUES ('$UserName', '$ReportNameLive', '$FileName', '$ReportFullPath', '$BeginDate', '$EndDate' )"
try {
$report.ExportToDisk("PortableDocFormat", $FileName)
$SqlCmd.ExecuteNonQuery() | Out-Null
Write-Host "Successfully created" $ReportNameLive -ForegroundColor Green }
catch {
$ErrorMessage = "[XX] " + $_.Exception.Message
Write-Host -ForegroundColor Red $ErrorMessage
$SqlCmd.CommandText = "INSERT dbo.ReportErrorTracker ( UserName, ReportNameLive, [FileName], ReportFullPath, BeginDateParam, EndDateParam, ErrorMessage ) VALUES ('$UserName', '$ReportNameLive', '$FileName', '$ReportFullPath', '$BeginDate', '$EndDate', '$ErrorMessage')"
$SqlCmd.ExecuteNonQuery() | Out-Null
$SqlConnection.Close()
$report.close()
exit 1 }
Request clarification before answering.
If you're using an ODBC connection, does this other user have the same connection configured as you do? By this I mean not only does the connection exist with the same name, but is it the same "bit-ness" (32 or 64) as what is configured on your computer?
Also, the error says that only two parameters are being provided to the program, but you indicate that it needs three. I would verify that the other user is running it correctly.
-Dell
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Dell. I will check the ODBC "bit-ness". As per your statement "the error says that only two parameters are being provided to the program, but you indicate that it needs three"
I may not have clarified, the stored procedure behind the report needs three parameters, but the error is on ExportToDisk, which ExportToDisk requires two parameters. Both the stored procedure and ExportToDisk, when performing a Write-Host prior to the calls, both show the parameters are valid and not missing. Meaning, it is not a parameter issue on the ExportToDisk execution. Therefore, I will look into the ODBC "bit-ness".
Thank you.
| User | Count |
|---|---|
| 7 | |
| 6 | |
| 5 | |
| 4 | |
| 3 | |
| 3 | |
| 3 | |
| 3 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.