function Publish-SSRSReportsDirectory { <# .SYNOPSIS .PARAMETER ReportDirectory .PARAMETER WebServiceUrl .PARAMETER ReportFolder .PARAMETER AvoidDoubleHop .PARAMETER ReportServerUserName .PARAMETER ReportServerPassword .PARAMETER ReportHashLocation #> [CmdletBinding()] Param( [ValidateScript( { Test-Path $_ })] [Parameter(Position = 0, Mandatory = $true)] [string]$ReportDirectory, [Parameter(Position = 1, Mandatory = $true)] [Alias("url")] [string]$WebServiceUrl, [Parameter(Position = 2, Mandatory = $true)] [string]$ReportFolder, [Parameter(Position = 3, Mandatory = $true)] [Alias("ApplyDoubleHopFix")] [bool]$AvoidDoubleHop, [Parameter(Position = 4, Mandatory = $false)] [Alias("Username")] [string]$ReportServerUserName, [Parameter(Position = 5, Mandatory = $false)] [Alias("Password")] [string]$ReportServerPassword, [Parameter(Position = 6, Mandatory = $false)] [Alias("FileHashLocation")] $ReportHashLocation = $null ) $logLead = Get-LogLeadName if (-not (Test-Path -Path $ReportDirectory)) { Write-Host "$logLead : Folder not found at [$ReportDirectory], nothing to do here" return } $functionStopWatch = [System.Diagnostics.Stopwatch]::StartNew() Write-Verbose ("$logLead : Getting *.rdl from {0}" -f $ReportDirectory) $reportFiles = Get-ChildItem $ReportDirectory -Recurse -Include *.rdl Write-Verbose ("$logLead : {0} report definition files located." -f $reportFiles.Count) $maxJobs = 5 $jobs = @() $scriptBlock = { param ($reportFile, $WebServiceUrl, $ReportFolder, $AvoidDoubleHop, $ReportServerUserName, $ReportServerPassword) Write-Verbose ("$logLead : Preparing RDL {0}" -f $reportFile.FullName) [xml]$reportXml = Get-Content $reportFile $description = $reportXml.Report.Description if ([String]::IsNullOrEmpty($description)) { $description = [String]::Empty } elseif ($description -match "-") { $description = $description.Split("-")[0] } Write-Verbose ("$logLead : Report description set to {0}" -f $description) Write-Verbose "$logLead : Calling Publish-SSRSReport" (Publish-SSRSReport -webServiceUrl $WebServiceUrl ` -rdlPath $reportFile.FullName ` -reportFolder $ReportFolder ` -avoidDoubleHop $AvoidDoubleHop ` -reportServerUserName $ReportServerUserName ` -reportServerPassword $ReportServerPassword) | Out-Null } if ( $null -ne $ReportHashLocation ) { $reportHashes = Import-Csv $ReportHashLocation } else { $reportHashes = $null } foreach ($reportFile in $reportFiles) { Write-Verbose ("$logLead : Beginning hash comparison for $($reportFile.Name)") $reportHash = Get-FileHash $reportFile if (($null -ne $reportHashes) -and ($null -ne ($reportHashes | Where-Object { $_.Hash -eq $reportHash.Hash } | Select-Object -First 1))) { Write-Host ("$logLead : Hash hasn't changed for report {0}. Not republishing." -f $reportFile.Name) } else { Write-Host ("$logLead : Starting Load Job for {0}." -f $reportFile.Name) $jobs += Start-Job -ScriptBlock $scriptBlock -ArgumentList $reportFile, $WebServiceUrl, $ReportFolder, $AvoidDoubleHop, $ReportServerUserName, $ReportServerPassword $running = @($jobs | Where-Object {$_.State -in ('Running', 'NotStarted')}) while ($running.Count -ge $maxJobs -and $running.Count -ne 0) { Wait-Job -Job $jobs -Any | Out-Null $running = @($jobs | Where-Object {$_.State -in ('Running', 'NotStarted')}) } } } if ($jobs) { Wait-Job -Job $jobs | Out-Null } # Receive-Job to output the logs $jobs | ForEach-Object { Receive-Job -Job $_ } $functionStopWatch.Stop() Write-Host ("$logLead : Total Execution Time: {0}" -f $functionStopWatch.Elapsed.ToString()) }