Unable to download objects using IshRemote

Hi,

I'm unable to get this script to work, would someone be able to take a peek at it?   maybe? :D 

This gist is, I'm trying to download the latest version of all map objects from my repo modified after 2019. I need to do some analysis on our doc set. Let me know if any more info is needed.

$ishSession = New-IshSession -WsBaseUrl "https://*******/ISHWS" -IshUserName "******" -IshPassword "******"

# Define MetadataFields for status and FDOCUMENTS
$statusFields = Set-IshMetadataFilterField -IshSession $ishSession -Name "FSTATUS" -Level "Lng" -ValueType "Element" -FilterOperator "Equal" -Value "VSTATUSRELEASED"
$cardtypeFields = Set-IshMetadataFilterField -IshSession $ishSession -Name "FMASTERTYPE" -Level "Logical" -ValueType "Value" -FilterOperator "Equal" -Value "Map"
$modifiedOn = Set-IshMetadataFilterField -Level Lng -Name MODIFIED-ON -FilterOperator GreaterThanOrEqual -Value "01/01/2019"
$docLang = Set-IshMetadataFilterField -Level Lng -Name DOC-LANGUAGE -FilterOperator Equal -Value en

# Merge metadatafields into an array
$metadataFields = $statusFields, $cardtypeFields, $modifiedOn, $docLang

# Get unique logical objects
$logicalIds = Find-IshDocumentObj -IshSession $ishSession -MetadataFilter $metadataFields | Select-Object -Property IshRef -Unique

# Define the directory where files will be downloaded
$directoryPath = "D:\InfoShare\Data\Publish\ExportedMaps"

# Create the directory if it does not exist
if (!(Test-Path -Path $directoryPath)) {
    New-Item -ItemType Directory -Path $directoryPath
}

# Define the metadata to request
$requestedMetadata = Set-IshRequestedMetadataField -IshSession $ishSession -Name "VERSION" -Level Version -ValueType Value 

# Total object counter
$totalObjects = 0

# For each logical object, get the latest version 
foreach($logicalId in $logicalIds) {
    $totalObjects++
    try {
        $documentVersions = Get-IshDocumentObj -IshSession $ishSession -LogicalId $logicalId.IshRef -RequestedMetadata $requestedMetadata
        
        # Check if Get-IshDocumentObj returned any data
        if ($documentVersions) {
            # Validate if all retrieved documents have a VERSION field
            $filteredVersions = $documentVersions | Where-Object { $_.IshFields.Field("VERSION") -ne $null }

            if($filteredVersions){
                $latestVersion = $filteredVersions |
                Sort-Object -Property { $_.IshFields.GetFieldValue('VERSION', 'Version', 'value') } | Select-Object -Last 1
                
                # Check if VERSION field is not null
                if ($latestVersion.IshFields.Field("VERSION")) {
                    # Uncomment the following line when you're ready to download files
                    #Get-IshDocumentObjData -IshSession $ishSession -LogicalId $latestVersion.IshRef.IshRef -Version $latestVersion.IshFields.Field("VERSION").ElementValue  -Language $latestVersion.IshRef.IshLng  -OutputPath $directoryPath -Force
                    Write-Host "`nDownloaded file: $directoryPath\$($logicalId.IshRef)_$($latestVersion.IshFields.GetFieldValue('VERSION', 'Version', 'value')).xml"
                } else {
                    throw "Error: VERSION field is missing or null in IshDocumentObj with LogicalId $logicalId.IshRef"
                }
            } else {
                throw "Error: No documents with a VERSION field were found for LogicalId $logicalId.IshRef"
            }
        } else {
            throw "Error: No documents found with LogicalId $logicalId.IshRef"
        }
    }
    catch {
        Write-Host "`nError processing file: $logicalId.IshRef"
        Write-Host "`nError details: $($_.Exception.Message)"
    }
}

# Write the total number of files downloaded
Write-Host "`nProcessed $totalObjects files in total."


thanks,

Charlie

emoji