Powershell ShareViolation Error when using DSOFile

0

I am trying to use DSOFile on a 64-bit system to obtain Office metadata properties from Office files without having to have Office installed on the system. I registered the 64-bit version of it I've obtained online, and it works the first time I run it. It crashes the PS console the second time around. I have no idea why it does that, considering that I close it.

(link: http://www.keysolutions.com/blogs/kenyee.nsf/d6plinks/KKYE-79KRU6)

Code:

[System.Reflection.Assembly]::LoadFrom('C:\TEMP\Interop.DSOFile.dll')
function Get-Summary([string]$file) {
    $oled = New-Object -COM DSOFile.OleDocumentProperties
    $oled.Open($file, $true, [DSOFile.dsoFileOpenOptions]::dsoOptionDefault)
    $spd =  $oled.SummaryProperties
    return $spd
    $oled.close()
}
Get-Summary ('Z:\SCRIPTS\TestFiles\color-difference.xls')

Error:

A share violation has occurred. (Exception from HRESULT: 0x80030020 
(STG_E_SHAREVIOLATION))
At Z:\SCRIPTS\test03.ps1:7 char:5
+     $oled.Open($file, $true, [DSOFile.dsoFileOpenOptions]::dsoOptionD ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

EDIT:

I got around it by creating an object within the function like so:

[System.Reflection.Assembly]::LoadFrom('C:\TEMP\Interop.DSOFile.dll')
function Get-Summary([string]$item) {
    $oled = New-Object -TypeName DSOFile.OleDocumentPropertiesClass
    $oled.Open($item)
    $spd = [PSCustomObject]@{
        Title                = $oled.SummaryProperties.Title
        Subject              = $oled.SummaryProperties.Subject
        Author               = $oled.SummaryProperties.Author
        DateCreated          = $oled.SummaryProperties.DateCreated
        LastSavedBy          = $oled.SummaryProperties.LastSavedBy
        DateLastSaved        = $oled.SummaryProperties.DateLastSaved
        Keywords             = $oled.SummaryProperties.Keywords
    }
    $spd
    $oled.close($item)
}
$officeInfo = Get-Summary('Z:\SCRIPTS\TestFiles\color-difference.xls')
$officeInfo
powershell
dsofile
asked on Stack Overflow Jun 21, 2017 by Just Zis Guy • edited Jun 21, 2017 by Just Zis Guy

1 Answer

0

In your code, you return in the function before you ever call the .Close() method.

[System.Reflection.Assembly]::LoadFrom('C:\TEMP\Interop.DSOFile.dll')
function Get-Summary([string]$file) {
    $oled = New-Object -COM DSOFile.OleDocumentProperties
    $oled.Open($file, $true, [DSOFile.dsoFileOpenOptions]::dsoOptionDefault)
    $spd =  $oled.SummaryProperties
    #return $spd
    $oled.close()
    return $spd # return here instead
}

And also, in the spirit of PowerShell, you don't truly need to call return. Just add the $spd variable by itself, and it will return it.

answered on Stack Overflow Jun 21, 2017 by SomeShinyObject

User contributions licensed under CC BY-SA 3.0