Public/New-AppEnhancerPdfBatch.ps1
|
function New-AppEnhancerPdfBatch { <# .SYNOPSIS This script creates a new batch in Application Enhancer for PDFs. .NOTES Name: New-AppEnhancerBatch Author: Bruce Stump .DESCRIPTION Uploading applications into OpenText Application Enhancer is a two step process; create a new batch, then index the document. This script creates a batch for a PDF in preparation for indexing. All parameters are mandatory for script. -FilePath: Path to PDF. -serverURL: Server URL. -Cred: Credential Object, must be a PSCustomObject. -dsn: Database Name. -appid: Application ID. .EXAMPLE New-AppEnhancerBatch -FilePath $pdfFilePath -serverUrl $serverUrl -Cred $credential -dsn $database -appid $AppID #> [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string]$FilePath, [Parameter(Mandatory=$true)] [string]$serverUrl, [Parameter(Mandatory=$true)] [pscredential]$Cred, [Parameter(Mandatory=$true)] [string]$dsn, [Parameter(Mandatory=$true)] [string]$appid, [Parameter(Mandatory=$true)] [string]$BatchName, [Parameter(Mandatory=$true)] [string]$BatchDescription ) begin{ Add-Type -AssemblyName "System.Net.Http" $Username = $Cred.UserName $Password = $cred.GetNetworkCredential().password } process { try { # === Encode credentials for Basic Auth === $pair = $Username + ':' + $Password $bytes = [System.Text.Encoding]::ASCII.GetBytes($pair) $base64 = [Convert]::ToBase64String($bytes) $authHeader = "Basic $base64" # Create Endpoint $endPoint = "$serverUrl/AppXtenderRest/api/AXDataSources/$dsn/axbatches/$appid" # Create JSON content (as a string) $JsonPayload = @{ "Name" = $BatchName "Description" = $BatchDescription "Private" = $false } | ConvertTo-Json -Depth 3 # Prepare PDF file content $FileStream = [System.IO.File]::OpenRead($FilePath) $FileContent = New-Object System.Net.Http.StreamContent($FileStream) $FileContent.Headers.ContentType = [System.Net.Http.Headers.MediaTypeHeaderValue]::Parse("application/pdf") # Prepare JSON content $JsonContent = New-Object System.Net.Http.StringContent($JsonPayload, [System.Text.Encoding]::UTF8, "application/vnd.emc.ax+json") # Create multipart form content $Form = New-Object System.Net.Http.MultipartFormDataContent # Add PDF file to form (use the correct form field name expected by the API) $Form.Add($FileContent, "bin", [System.IO.Path]::GetFileName($FilePath)) # Add JSON to form (the name "metadata" is an example, adjust as needed) $Form.Add($JsonContent, "data") # Create HTTP client $HttpClient = New-Object System.Net.Http.HttpClient # === Send request with Basic Auth === $HttpClient.DefaultRequestHeaders.Authorization = $authHeader # Send the POST request $response = $HttpClient.PostAsync($endPoint, $Form).Result # Read the response $responseContent = $response.Content.ReadAsStringAsync().Result Write-Output "Status Code: $($response.StatusCode)" Write-Output "Response: $responseContent" } catch { $_.Exception.Message } } end { # Clean up $FileStream.Dispose() } } |