Public/New-AppEnhancerTxtBatch.ps1
|
function New-AppEnhancerTxtBatch { <# .SYNOPSIS This script creates a new batch for text documents in Application Enhancer. .NOTES Name: New-AppEnhancerTxtBatch 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 text document in preparation for indexing. All parameters are mandatory for script. -inputFilePath: File Path to text file to be imported. -outputFilePath: File Path to encoded text file, encoded inputFilePath. -serverURL: Server URL. -Cred: Credential Object, must be a PSCustomObject. -dsn: Database Name. -appid: Application ID. -BatchName: Name of Batch that will be created in App Enhancer. -BatchDescription: Description of Batch created in App Enhancer. .PARAMETER inputFilePath File Path to text file to be imported. .PARAMETER outputFilePath File Path to encoded text file, encoded inputFilePath. App Enhancer requires text files to be encoded to UTF8. Create a name that will distinguish what this file is, for example, C:\Path\inputFile_UTF8.txt. .PARAMETER serverUrl Server URL. .PARAMETER Cred Credential Object, must be a PSCustomObject. .PARAMETER dsn Database Name. .PARAMETER appid Application ID. .PARAMETER BatchName Name of Batch that will be created in App Enhancer. .PARAMETER BatchDescription Description of Batch created in App Enhancer. .EXAMPLE New-AppEnhancerTxtBatch -inputFilePath $txtFilePath -outputFilePath $outFilePath -serverUrl $serverUrl -Cred $credential -dsn $database -appid $AppID -BatchName $BatchName -BatchDescription $BatchDescription #> [CmdletBinding()] param ( [Parameter(Mandatory=$true)] [pscredential]$Cred, [Parameter(Mandatory=$true)] [string]$serverUrl, [Parameter(Mandatory=$true)] [string]$dsn, [Parameter(Mandatory=$true)] [string]$appid, [Parameter(Mandatory=$true)] [string]$BatchName, [Parameter(Mandatory=$true)] [string]$BatchDescription, [Parameter(Mandatory=$true)] [string]$inputFilePath, [Parameter(Mandatory=$true)] [string]$outputFilePath ) begin { # Define your credentials $username = $Cred.UserName $password = $cred.GetNetworkCredential().password } process { # Convert credentials to Base64 for Basic Authentication $pair = $username + ':' + $password $bytes = [System.Text.Encoding]::ASCII.GetBytes($pair) $base64 = [Convert]::ToBase64String($bytes) $authHeader = "Basic $base64" #$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(($pair))) # Create Endpoint $endPoint = "$serverUrl/AppEnhancerRest/api/AXDataSources/$dsn/axbatches/$appid" # Create boundary, delimeter between different sections of data $boundary = [System.Guid]::NewGuid().ToString() # Header need for Authorization $headers = @{ "Authorization" = $authHeader "Accept" = "application/vnd.emc.ax+json" "Content-Type" = "multipart/form-data;boundary=$boundary" } # Batch information $batchData = @{ "Name" = $BatchName "Description" = $BatchDescription "Private" = $false } # Read the content of the input file $content = Get-Content -Path $inputFilePath -Raw # Write the content to the output file with UTF-8 encoding [System.IO.File]::WriteAllText($outputFilePath, $content, [System.Text.Encoding]::UTF8) # Read bytes of text file with utf-8 encoding $fileContent = [System.IO.File]::ReadAllBytes($outputFilePath) $body = "--$boundary`r`n" $body += "Content-Type: application/vnd.emc.ax+json; charset=utf-8`r`n" $body += "Content-Disposition: form-data; name=`"data`"`r`n`r`n" $body += (ConvertTo-Json $batchData) + "`r`n" $body += "--$boundary`r`n" $body += "Content-Type: text/plain`r`n" $body += "Content-Disposition: form-data; name=`"bin`"; filename=`"test2_utf8.txt`"; filename*=utf-8''test2_utf8.txt`r`n`r`n" # Code to use for encryption $CODEPAGE = "iso-8859-1" # alternatives are ASCII, UTF-8, iso-8859-1 # Get Encoding for Codepage $enc = [System.Text.Encoding]::GetEncoding($CODEPAGE) # Get Encrypted File. $fileEnc = $enc.GetString($fileContent) # Check for BOM and remove it if present if ($fileEnc.StartsWith([System.Text.Encoding]::UTF8.GetString([byte[]](0xEF, 0xBB, 0xBF)))) { $fileEnc = $fileEnc.Substring(3) } $body += $fileEnc + "`r`n" $body += "--$boundary--" Invoke-RestMethod -Uri $endpoint -Method Post -Headers $headers -Body $body } end {} } |