Public/Add-AppEnhancerBatchIndex.ps1

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 -Database $database
    #>


    param (
        [Parameter(Mandatory=$true)]
        [pscredential]$AppEnhancerCred,
        [Parameter(Mandatory=$true)]
        [string]$serverUrl,
        [Parameter(Mandatory=$true)]
        [string]$AppId,
        [Parameter(Mandatory=$true)]
        [string]$Database,
        [Parameter(Mandatory=$true)]
        [string]$FromBatch,
        [Parameter(Mandatory=$true)]
        [string]$StringIndex
    )

    begin {
        [string]$EndPoint = "AppEnhancerRest/api/AXDataSources/$Database/AXDocs"

        # 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 -serverUrl $serverUrl -Cred $AppEnhancerCred).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 {}
}