PowerAppEnhancer.psm1

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()
    }
}

function Add-AppEnhancerBatchIndex {
    <#
    .SYNOPSIS
        This script adds indexes to batches.
 
    .NOTES
        Name: Add-AppEnhancerIndex
        Author: Bruce Stump
 
    .DESCRIPTION
        Adds indexes to batches in Application Enhancer after the batches have been created.
 
    .EXAMPLE
        Add-AppEnhancerIndex -serverUrl $ServerUrl -AppEnhancerCred $Cred -AppId ApplicationID
        -FromBatch $BatchId
    #>


    param (
        [Parameter(Mandatory=$true)]
        [pscredential]$AppEnhancerCred,
        [Parameter(Mandatory=$true)]
        [string]$serverUrl,
        [Parameter(Mandatory=$true)]
        [string]$AppId,
        [Parameter(Mandatory=$true)]
        [string]$FromBatch,
        [string]$EndPoint = 'AppXtenderRest/api/AXDataSources/AXDEV/AXDocs',
        [Parameter(Mandatory=$true)]
        [string]$StringIndex
    )

    begin {
        # Load the necessary assembly
        Add-Type -AssemblyName System.Net.Http

        # Define the API endpoint
        $apiEndpoint = "$serverUrl/$EndPoint/$AppId"

        # Application Enhancer Credential Object
        $cred = $AppEnhancerCred.UserName + ':' + $AppEnhancerCred.GetNetworkCredential().password
    }

    process {
        # Convert credentials to Base64 for Basic Authentication
        $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes($cred))

        $authHeader = "Basic $base64AuthInfo"

        # Create an instance of HttpClient
        $httpClient = New-Object System.Net.Http.HttpClient

        # Set the Accept header
        $httpClient.DefaultRequestHeaders.Accept.Add([System.Net.Http.Headers.MediaTypeWithQualityHeaderValue]::new("application/vnd.emc.ax+json"))

        # Define the number of times to repeat the hash table
        $repeatCount = (Get-AppEnhancerApplicationFields -AppId $AppId).entries.count

        # Initialize an empty array to store the repeated hash tables
        $repeatedValues = @()

        # Use a loop to repeat the hash table
        for ($i = 1; $i -le $repeatCount; $i++) {
            $repeatedValues += @{
            FieldID = "field$i"
            FieldValue = ""
            }
        }
    
        # Define the data
        $data = @{
            TargetDoc = @{
                ID = 0
            }
            NewIndex = @{
                indexid = 0
                values = @(
                    $repeatedValues
                )
            }
            FromBatch = @{
                ID = $FromBatch
            }
            BatchPageNum = 0
            IngoreDuplicateIndex = $true
            IgnoreDlsViolation = $false
        }

        $index = $StringIndex.split('|')

        $indexFields = @()
        
        for ($i = 0;$i -le $index.count-1;$i++) {
            $indexFields += @{ FieldID = $data.NewIndex.Values[$i].FieldID; FieldValue = $index[$i]}
        }

        $newRecords = $indexFields
 
        # Replace the existing records with the new records
        $data.NewIndex.values = $newRecords

        # Convert the data to JSON
        $jsonData = $data | ConvertTo-Json -Depth 3

        # Create the content for the request
        $content = New-Object System.Net.Http.StringContent($jsonData, [System.Text.Encoding]::UTF8, "application/vnd.emc.ax+json")

        # === Send request with Basic Auth ===
        $HttpClient.DefaultRequestHeaders.Authorization = $authHeader

        # Send the POST request
        $response = $httpClient.PostAsync($apiEndpoint, $content).Result

        $response
    }

    end {}
}

function Get-AppEnhancerApplicationFields {
    <#
    .SYNOPSIS
        This script gets field information for specific application.
 
    .NOTES
        Name: Get-AppEnhancerApplicationFields
        Author: Bruce Stump
 
    .DESCRIPTION
        This script gets field information for specific application in Application
        Enhancer.
 
    .EXAMPLE
        Get-AppEnhancerApplicationFields -AppId $ApplicationId -Cred $credentials -serverUrl $serverUrl
    #>


    param (
        [Parameter(Mandatory=$true)]
        [string]$AppId,
        [Parameter(Mandatory=$true)]
        [pscredential]$Cred,
        [Parameter(Mandatory=$true)]
        [string]$serverUrl
    )

    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"

        # Define headers with Basic Authentication
        $headers = @{
            "Authorization" = "Basic $authHeader"
            "Content-Type"  = "application/json"
        }

        # Define the URL to get the list of applications
        $applicationsUrl = "$serverUrl/AppXtenderReST/api/AXDataSources/AXDEV/axappfields/$AppId"

        # Retrieve the list of applications
        Invoke-RestMethod -Uri $applicationsUrl -Method Get -Headers $headers
    }

    end {}
}

function New-AppEnhancerTxtBatch {
    <#
    .SYNOPSIS
        This script creates a new batch for text documentsin Application Enhancer
        for PDFs.
 
    .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.
 
    .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/AppXtenderRest/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 {}
}