|
- param(
- [Parameter(Mandatory = $true)][string]$RequestPath,
- [Parameter(Mandatory = $true)][string]$ResultPath
- )
- $ErrorActionPreference = 'Stop'
- $result = @{ Success = $false; Output = ''; Error = '' }
- try {
- $projectRoot = Split-Path -Parent $PSScriptRoot
- $tempRoot = [System.IO.Path]::GetFullPath((Join-Path $projectRoot 'temp'))
- foreach ($path in @($RequestPath, $ResultPath)) {
- $resolved = [System.IO.Path]::GetFullPath($path)
- if (-not $resolved.StartsWith($tempRoot + [System.IO.Path]::DirectorySeparatorChar,
- [StringComparison]::OrdinalIgnoreCase)) {
- throw 'Hidden helper files must be under the project temp folder'
- }
- }
- $request = Get-Content -LiteralPath $RequestPath -Raw -Encoding UTF8 | ConvertFrom-Json
- $allowed = @('test-smtp-message.ps1', 'import-template.ps1', 'preview-template.ps1',
- 'review-campaign.ps1', 'send-campaign-test.ps1', 'approve-campaign.ps1',
- 'dispatch-control.ps1', 'dispatch-once.ps1', 'send-approved-campaign-test.ps1',
- 'export-report.ps1')
- if ($request.ScriptName -notin $allowed) { throw 'Unsupported hidden helper script' }
- $scriptPath = Join-Path $PSScriptRoot $request.ScriptName
- if (-not (Test-Path -LiteralPath $scriptPath -PathType Leaf)) {
- throw 'Hidden helper script is missing'
- }
- $parameters = @{ ConfigPath = [string]$request.ConfigPath }
- foreach ($property in $request.Parameters.PSObject.Properties) {
- if ($property.Name -notmatch '^[A-Za-z][A-Za-z0-9]*$' -or
- $property.Name -eq 'ConfigPath') { throw 'Invalid hidden helper parameter' }
- $parameters[$property.Name] = $property.Value
- }
- $result.Output = ((& $scriptPath @parameters | Out-String).Trim())
- $result.Success = $true
- }
- catch {
- $result.Error = $_.Exception.Message
- }
- finally {
- $json = ConvertTo-Json -InputObject $result -Compress -Depth 4
- $temporaryPath = $ResultPath + '.writing'
- [System.IO.File]::WriteAllText($temporaryPath, $json,
- (New-Object System.Text.UTF8Encoding($false)))
- [System.IO.File]::Move($temporaryPath, $ResultPath)
- }
- if (-not $result.Success) { exit 1 }
|