此脚本在PowerShell中运行良好。但是,当我使用任务计划程序运行它时,它会失败并带有
您无法在PowerShell脚本上调用空值表达式上的方法。
$ErrorActionPreference = "SilentlyContinue"
$global:reports
$global:strdate
$global:ReportName
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=DWPROD;Database=DP_DW;Integrated Security=True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = " Select
email = 'user@example.com'
,username='example\user'
,password='password'
,ReportName = 'LaborAnalysis Kronos Daily'
,ReportFileName = 'LaborAnalysisDaily-'
,ReportDate = CONVERT(varchar,getdate()-2,101)
,Sharepoint = 'http://intranet.shccs.com/shcsites/toolbox/D Labor/Daily Reports/'"
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$reportinfo = $DataSet.Tables[0]
$SqlConnection.Close()
#ARRAY TO SAVE Report Criteria
$a = 0
foreach ($reportparam in $reportinfo.Rows)
{
$ReportName = $reportparam['ReportName']
$ReportFileName = $reportparam['ReportFileName']
$ReportDate = $reportparam['ReportDate']
$Sharepoint = $reportparam['Sharepoint']
#Write-Host $ReportName + $ReportDate
Write-Host $ReportDate
#Write-Host $Sharepoint
#Write-Host (Get-date).AddDays(-1)
#Write-Host $(get-date -f yyyy-MM-dd)
#--------------------------------------------------------------
# report parameters
#--------------------------------------------------------------
$inputParams = @{
"prmRptDate" = '11/16/2016'
}
#--------------------------------------------------------------
# add assembly
#--------------------------------------------------------------
Add-Type -AssemblyName "Microsoft.ReportViewer.WinForms, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
#Add-Type -AssemblyName -Path 'C:\Scripts\itextsharp.dll'
#[System.Reflection.Assembly]::LoadFrom('\\itextsharp.dll') | Out-Null
#--------------------------------------------------------------
# display calculated month end date
#--------------------------------------------------------------
$startTime = Get-Date
Write-Host ("=" * 80)
Write-Host "Generating Reports"
Write-Host "Start Time: $startTime"
Write-Host ("=" * 80)
#--------------------------------------------------------------
# create timestamped folder
# where we will save our report
#--------------------------------------------------------------
$dt = Get-Date -Format "yyyy-MMM-dd hhmmtt"
$rptdt = Get-Date -Format "yyyy-mm-dd"
$baseFolder = "C:Scripts"
Remove-Item "C:\Scripts\Labor\*"
$rv = New-Object Microsoft.Reporting.WinForms.ReportViewer
#--------------------------------------------------------------
# report Server Properties
#--------------------------------------------------------------
$strdate = $ReportDate -replace "/", "-"
$reports = $ReportFileName + $strdate + "pdf";
$rv.ServerReport.ReportServerUrl = "http://dpweb01.shccs.com/ReportServer"
$rv.ServerReport.ReportPath = "/Labor/"+ $ReportName
$rv.ProcessingMode = "Remote"
#Write-Host $ReportName
#--------------------------------------------------------------
# set up report parameters
#--------------------------------------------------------------
$params = $null
#create an array based on how many incoming parameters
$params = New-Object 'Microsoft.Reporting.WinForms.ReportParameter[]' $inputParams.Count
$d = 0
foreach ($p in $inputParams.GetEnumerator())
{
$params[$d] = New-Object Microsoft.Reporting.WinForms.ReportParameter($p.Name, $p.Value, $false)
$d++
}
# set the parameters
$rv.ServerReport.SetParameters($params)
$rv.ShowParameterPrompts = $false
$rv.RefreshReport()
$rv.ServerReport.Refresh()
#--------------------------------------------------------------
# set rendering parameters
#--------------------------------------------------------------
$mimeType = $null
$encoding = $null
$extension = $null
$streamids = $null
$warnings = $null
#--------------------------------------------------------------
# render the SSRS report in PDF
#--------------------------------------------------------------
$bytes = $null
$bytes = $rv.ServerReport.Render("PDF",
$null,
[ref] $mimeType,
[ref] $encoding,
[ref] $extension,
[ref] $streamids,
[ref] $warnings)
#--------------------------------------------------------------
# save the report to a file
#--------------------------------------------------------------
$fileName = "C:/Scripts/Labor/" + $reports
$fileStream = New-Object System.IO.FileStream($fileName, [System.IO.FileMode]::OpenOrCreate)
$fileStream.Write($bytes, 0, $bytes.Length)
$fileStream.Close()
$fileStream.Dispose();
$reportdone = $reportname + " " + $(Get-Date -f yyyy-MM-dd) + ".PDF"
#Write-Host $reportdone
#--------------------------------------------------------------
# Copy FIlES to Sharepoint
#--------------------------------------------------------------
# Set the variables
$destination = "http://intranet.shccs.com/shcsites/toolbox/D Labor/Daily Reports/"
$File = Get-ChildItem "C:\Scripts\Labor"
$FilePath = $File.FullName
Write-Host $FilePath
# Since we're doing this remotely, we need to authenticate
$securePasssword = ConvertTo-SecureString "password" -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential ("example\user", $securePasssword)
# Upload the file
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = $credentials
$fileupload = $destination + $File
Write-Host $fileupload
$webclient.UploadFile($fileupload, "PUT", $FilePath)
#$file = "c:\Scripts\file.txt"
#$FilePath | Add-Content -Path $file
#$fileupload | Add-Content -Path $file
$a++
}
Send-MailMessage -To "user <user@example.com>" -From "user <user@example.com>" -Cc "user <user@example.com>" -Subject "Daily Labor Reports are Complete" -SmtpServer "relay.example.com" -Body "Labor Reports have been uploaded"
#--------------------------------------------------------------
# LOG ERROS
#--------------------------------------------------------------
$LaborLogs = "c:\Scripts\LaborLogs.txt"
#$error[0].Exception | gm
Add-Content $LaborLogs ("=" * 80)
Add-Content $LaborLogs $startTime
Add-Content $LaborLogs $strdate
Add-Content $LaborLogs $error[0]
Add-Content $LaborLogs ("=" * 80)