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.
I followed your suggestion to remove "Save Data with Report" and saved the report.
Unfortunately, even with this change the report still executes successfully for me with an incorrect password.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Robert,
I just noticed your SQL connection has IntegratedSecurity as true. If that's the case, the password you pass to the report will be ignored.
If your SQL Server were not using Integrated Security then you would have to pass the Username and Password.
https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/authentication-in-sql-server
Let me know if this makes sense.
Thanks,
Brian
| User | Count |
|---|---|
| 8 | |
| 5 | |
| 5 | |
| 4 | |
| 4 | |
| 3 | |
| 2 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.