Public/Update-AppEnhancerDocumentIndex.ps1

function Update-AppEnhancerDocumentIndex {
    <#
    .SYNOPSIS
        Updates a specific index field of a document in ApplicationXtender using the REST API.
 
    .DESCRIPTION
        This function authenticates to the ApplicationXtender REST API using basic authentication
        and updates a specified index field for a given document within a specific application.
 
    .PARAMETER ServerUrl
        The base URL of the ApplicationXtender REST API server (e.g., https://yourserver.domain.com).
 
    .PARAMETER Credential
        A PSCredential object containing the username and password for authentication.
 
    .PARAMETER DocId
        The ID of the document whose index is to be updated.
 
    .PARAMETER AppId
        The ID of the ApplicationXtender application containing the document.
 
    .PARAMETER IndexId
        The ID of the index field to update.
 
    .EXAMPLE
        $cred = Get-Credential
        $fields = @(
            @{ FieldID = "field1"; FieldValue = "4860012" }
            @{ FieldID = "field4"; FieldValue = "07/31/2025" }
            @{ FieldID = "field5"; FieldValue = "Box Delinquency Notice" }
        )
        $docId = '1234'
        $appId = '5'
        $indexId = '2'
 
        Update-AppEnhancerDocumentIndex -ServerUrl "https://axwebserver.local" `
            -Credential $cred `
            -DocId $docId `
            -AppId $appId `
            -IndexId $indexId `
            -FieldUpdates $fields
 
        This example updates multiple index fields for document ID 1234 in application ID 5 using the provided credentials and field values.
 
    .NOTES
        Author: Bruce Stump
        Date: August 2025
        Requires: PowerShell 5.1+, ApplicationXtender REST API access
    #>


    [CmdletBinding(SupportsShouldProcess=$true)]
    param (
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$ServerUrl,
        
        [Parameter(Mandatory=$true)]
        [ValidateNotNull()]
        [PSCredential]$Credential,
        
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$DocId,
        
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$AppId,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$IndexId,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [hashtable[]]$FieldUpdates
    )

    begin {}

    process {
        Write-Verbose "Starting Update-AppEnhancerDocumentIndex for DocId '$DocId', AppId '$AppId', IndexId '$IndexId'."

        foreach ($field in $FieldUpdates) {
            if (-not ($field.ContainsKey('FieldID') -and $field.ContainsKey('FieldValue'))) {
                throw "Each item in -FieldUpdates must contain 'FieldID' and 'FieldValue'."
            }
        }
    
        # Authentication
        Write-Verbose "Preparing authentication headers."

        if ([string]::IsNullOrWhiteSpace($Credential.UserName)) {
            throw "Credential username cannot be empty."
        }

        # Convert SecureString to plain text
        $plainPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto(
            [Runtime.InteropServices.Marshal]::SecureStringToBSTR($Credential.Password)
        )

        $cred = "$($Credential.Username):$plainPassword"
        $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes($cred))

        $queryUrl = "$ServerUrl/AppXtenderReST/api/AXDataSources/ApplicationXtender/axapps/$AppId/axdocindexes/$DocId/$IndexId"
        Write-Verbose "Constructed query URL: $queryUrl"

        $headers = @{
            "Authorization" = "Basic $base64AuthInfo"
            "Content-Type"  = "application/vnd.emc.ax+json"
        }

        $documentsToUpdate = @(
            @{
                DocId = $DocId
                Values = $FieldUpdates
            }
        )

        $body = @{
            values = $documentsToUpdate[0].Values
        } | ConvertTo-Json -Depth 5

        Write-Verbose "Request body prepared: $body"

        try {
            Write-Verbose "Sending PUT request to ApplicationXtender API..."
            if ($PSCmdlet.ShouldProcess("Document ID $DocId in App ID $AppId", "Update index fields")) {
                $response = Invoke-RestMethod -Uri $queryUrl -Headers $headers -Method Put -Body $body -ErrorAction Stop
                Write-Verbose "Update successful."
                return $response
            } 
            
            else {
                Write-Verbose "WhatIf: Skipped update for Document ID $DocId in App ID $AppId."
            }
        }   
        
        catch {
            Write-Error "Failed to update document index: $_"
        }
    }

    end {}
}