Public/ps1/Files/Send-FileToVDB.ps1
|
<#
.SYNOPSIS Sends a file to VDB, optionally encoding as base64. .DESCRIPTION Reads a file, optionally encodes its content as base64, generates a key from the first 8 characters of the filename plus a hash, and sends it to VDB using Set-ApprxrVDBEntry. .PARAMETER FilePath The path to the file to send. .PARAMETER DatabaseName The database name for the VDB entry. .PARAMETER AsBase64 If set, encodes the file content as base64 (default: false). .EXAMPLE Send-FileToVDB -FilePath 'C:\data\foo.txt' -DatabaseName 'mydb' -AsBase64 #> function Send-FileToVDB { [CmdletBinding()] param( [Parameter(Mandatory)] [string]$FilePath, [Parameter(Mandatory)] [string]$DatabaseName, [switch]$AsBase64 ) if (-not (Test-Path $FilePath)) { throw "File not found: $FilePath" } $fileName = [System.IO.Path]::GetFileName($FilePath) $first8 = $fileName.Substring(0, [Math]::Min(8, $fileName.Length)) $hash = [BitConverter]::ToString((New-Object -TypeName System.Security.Cryptography.SHA256Managed).ComputeHash([Text.Encoding]::UTF8.GetBytes($fileName))).Replace("-","").Substring(0,8) $key = "$first8$hash" $bytes = [System.IO.File]::ReadAllBytes($FilePath) $ext = [System.IO.Path]::GetExtension($fileName).ToLower() $textExts = @(".txt", ".csv", ".xml", ".html", ".htm", ".json", ".log") $isText = $textExts -contains $ext $useBase64 = $false if ($AsBase64) { $useBase64 = $true } elseif (-not $isText) { $useBase64 = $true } if ($useBase64) { $content = [Convert]::ToBase64String($bytes) } else { $content = [Text.Encoding]::UTF8.GetString($bytes) } $entry = [PSCustomObject]@{ FileName = $fileName Key = $key Content = $content } Set-ApprxrVDBEntry -InnerObject $entry -Key $key -DatabaseName $DatabaseName } |