FileNet-IDM.psm1
$idmobjecttype_idmObjTypeDocument = 1
$idmSaveNewOptions_idmDocSaveNewKeep = 4
function new-fileNetLib {
new-object -com IDMObjects.Library
}
function connect-fileNet {
param (
[__ComObject] $lib,
[string ] $user,
[string ] $password
)
#
# Return true or false
#
return $lib.Logon($user, $password)
}
function disconnect-fileNet {
param (
[__ComObject] $lib
)
#
# TODO: Even after calling Logoff(), calling Logon() again
# results in an error.
#
$lib.Logoff()
}
function get-fileNetDocument {
param (
[__ComObject] $lib,
[int ] $docId
)
$doc = $lib.GetObject($idmobjecttype_idmObjTypeDocument, $docId)
return $doc
}
function export-fileNetDocument {
param (
[__ComObject] $doc,
[string ] $destDir,
[string ] $suffix = 'tif' # TODO: In current environment, all documents are TIFFs
)
$docId = $doc.ID
$nofPages = $doc.PageCount
$null = initialize-emptyDirectory "$destDir/$docId"
foreach ($page in 1 .. $nofPages) {
$cacheFileName = $doc.GetCachedFile($page)
$destFile = "$destDir/$docId/$page.$suffix"
copy-item $cacheFileName $destFile
# write-host $destFile
}
}
function export-fileNetDocumentById {
param (
[__ComObject] $lib,
[int ] $docId,
[string ] $destDir
)
$doc = get-fileNetDocument $lib $docId
export-fileNetDocument $doc $destDir
$null = [System.Runtime.InteropServices.Marshal]::ReleaseComobject($doc)
}
function new-fileNetDocument {
param (
[__ComObject] $lib,
[string ] $docClass
)
$doc = $lib.CreateObject($idmobjecttype_idmObjTypeDocument, $docClass)
return $doc
}
function import-filesIntoFilenetDocument {
param (
[__ComObject] $doc,
[string ] $destDir
)
#
# Import exported TIFF pages in numeric order so page 10 follows page 9.
#
if (-not (test-path -literalPath $destDir -pathType Container)) {
throw "Document directory does not exist: $destDir"
}
$pageFiles = @(get-childItem -literalPath $destDir -file)
if ($pageFiles.Count -eq 0) {
throw "Document directory contains no page files: $destDir"
}
foreach ($pageFile in $pageFiles) {
if ($pageFile.Extension -ine '.tif' -or $pageFile.BaseName -notmatch '^[1-9][0-9]*$') {
throw "Invalid exported page file: $($pageFile.FullName)"
}
}
$pageFiles = @($pageFiles | sort-object { [int] $_.BaseName })
$expectedPage = 1
foreach ($pageFile in $pageFiles) {
if ([int] $pageFile.BaseName -ne $expectedPage) {
throw "Expected page $expectedPage but found page $($pageFile.BaseName) in $destDir"
}
$expectedPage++
}
[String[]] $files = @($pageFiles.FullName)
if ($files.length -gt 1) {
$null = $doc.SaveNew($files, $idmSaveNewOptions_idmDocSaveNewKeep)
}
else {
$null = $doc.SaveNew($files[0], $idmSaveNewOptions_idmDocSaveNewKeep)
}
# return $doc
}
function copy-fileNetDocument {
param (
[__ComObject] $lib,
[int ] $docId
)
$doc = get-fileNetDocument $lib $docId
export-fileNetDocument $doc $env:temp/filenetExp # Possible security risk.
$docClass = $doc.ClassDescription.Label
$docNew = new-fileNetDocument $lib $docClass
foreach ($x in
'D_DAT', 'DA_ABW', 'DA_KEY', 'DA_MAIN', 'DA_SPEC', # TODO: these five index values are customer dependent.
'F_DOCFORMAT' # -> Cannot set property after document is committed
) {
$docNew.Properties[$x].value = $doc.Properties[$x].value -as [String]
}
import-filesIntoFilenetDocument $docNew "$env:temp/filenetExp/$docId"
foreach ($p in $doc.properties) {
# Do not write to Read only properties:
if ($p.name -notIn 'F_ACCESSRIGHTS_AX', 'F_ACCESSRIGHTS_RD', 'F_ACCESSRIGHTS_WR', 'F_DOCFORMAT', 'F_DOCCLASSNAME', 'F_DOCCLASSNUMBER', 'F_DOCLOCATION', 'F_DOCNUMBER', 'F_DOCTYPE', 'F_PAGES', 'F_ENTRYDATE', 'F_DELETEDATE', 'F_ARCHIVEDATE', 'F_CLOSED', 'F_CE_OS_ID', 'F_DOCNUMBER') {
if (-not ($p.value -is [DBNull])) {
if ($p.value.GetType() -eq [datetime]) {
#
# Issue SZ-FNP_ARCHIVE
#
$docNew.Properties[$p.name].value = $p.value.ToString('d')
}
else {
$docNew.Properties[$p.name].value = $p.value -as [String]
}
}
else {
$docNew.Properties[$p.name].value = $null
}
}
}
$annots = $doc.Annotations
foreach ($annot in $annots) {
$annotNew = $docNew.CreateAnnotation($annot.properties['F_PAGENUMBER'].value, $annot.properties['F_CLASSNAME'].value)
if ($annot.properties['F_CLASSNAME'].value -eq 'Pen') {
$len = $annot.properties['F_POINTS'].value.Length
$bytearr = [byte[]]::new($len)
[int]$b = 0 # Assignment to 0 to prevent a 0 going to out stream.
foreach ($b in 0 .. ($len -1) ) {
$bytearr[$b] = $annot.Properties['F_POINTS'].value[$b]
}
$annotNew.Properties['F_POINTS'] = $bytearr
$len = $annot.properties['F_CUSTOM_BYTES'].value.Length
$annotNew.Properties['F_CUSTOM_BYTES'] = [byte[]]::new($len)
if ($len -gt 0) {
foreach ($b in 0 .. ($len -1) ) {
$annotNew.Properties['F_CUSTOM_BYTES'].value[$b] = $annot.Properties['F_CUSTOM_BYTES'].value[$b]
}
}
}
foreach ($p in $annot.properties) {
if ($p.name -notIn 'F_ANNOTATEDID', 'F_CLASSNAME', 'F_CLASSID', 'F_PAGENUMBER', 'F_ORDINAL', 'F_NAME', 'F_ID', 'F_MODIFYDATE', 'F_ENTRYDATE', 'F_CUSTOM_BYTES') {
if (-not ($p.value -is [DBNull])) {
if ($p.name -notIn 'F_CUSTOM_BYTES', 'F_POINTS') {
$annotNew.properties[$p.name] = $p
}
}
}
}
}
$docNew.Save()
$docIdNew = $docNew.id
$null = [System.Runtime.InteropServices.Marshal]::ReleaseComobject($doc )
$null = [System.Runtime.InteropServices.Marshal]::ReleaseComobject($docNew)
return $docIdNew
}
FileNet-IDM.psd1
@{
rootModule = 'fileNet-IDM'
moduleVersion = '2026.12.01'
author = 'René Nyffenegger'
description = 'A PowerShell module for FileNet COM objects'
guid = '46696c65-4e65-7449-444d-0123456789ab'
processorArchitecture = 'x86'
functionsToExport = @(
'new-fileNetLib',
'connect-fileNet',
'disconnect-fileNet',
'get-fileNetDocument',
'export-fileNetDocument',
'export-fileNetDocumentById',
'new-fileNetDocument',
'import-filesIntoFilenetDocument',
'copy-fileNetDocument'
)
}