Public/ps1/Files/Start-ApprxrJobFile.ps1
|
<#
.SYNOPSIS Handles file jobs for Apprxr by saving, moving, or overwriting files based on input parameters. .DESCRIPTION Decodes a base64-encoded file and saves it to the specified location. Handles existing files according to the specified mode: error, move, or overwrite. .PARAMETER Base64File The base64-encoded file content to save. .PARAMETER FileLocation The directory where the file should be saved. .PARAMETER Mode The action to take if the file already exists: error, move, or overwrite. .PARAMETER FileName The name of the file to save. .EXAMPLE Start-ApprxrJobFile -Base64File $b64 -FileLocation 'C:\Temp' -Mode 'overwrite' -FileName 'test.txt' .NOTES Used for file management jobs in Apprxr automation. #> function Start-ApprxrJobFile { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string]$Base64File, [Parameter(Mandatory=$true)] [string]$FileLocation, [Parameter(Mandatory=$true)] [ValidateSet('error','move','overwrite')] [string]$Mode, [Parameter(Mandatory=$true)] [string]$FileName ) $outputPath = Join-Path $FileLocation $FileName $fileExists = Test-Path $outputPath $actionTaken = 'none' $movedPath = $null if ($fileExists) { switch ($Mode) { 'error' { $actionTaken = 'error' return [PSCustomObject]@{ OutputPath = $outputPath FileExisted = $true ActionTaken = $actionTaken MovedPath = $null SizeWritten = 0 Success = $false Message = "File already exists: $outputPath" } } 'move' { $timestamp = Get-Date -Format 'yyyyMMddHHmmss' $movedPath = Join-Path $FileLocation ("$FileName.$timestamp.bak") Move-Item -Path $outputPath -Destination $movedPath -Force $actionTaken = 'moved' } 'overwrite' { $actionTaken = 'overwritten' } } } else { $actionTaken = 'created' } $bytes = [System.Convert]::FromBase64String($Base64File) [System.IO.File]::WriteAllBytes($outputPath, $bytes) $sizeWritten = $bytes.Length $data = @([PSCustomObject]@{ OutputPath = $outputPath FileExisted = $fileExists ActionTaken = $actionTaken FileName = $FileName MovedPath = $movedPath SizeWritten = $sizeWritten }) return [PSCustomObject]@{ Success = $true Message = "File written successfully." Data = $data } } |