0

我正在尝试在 64 位系统上使用 DSOFile 从 Office 文件中获取 Office 元数据属性,而无需在系统上安装 Office。我注册了我在网上获得的64位版本,我第一次运行它就可以了。它第二次使 PS 控制台崩溃。考虑到我关闭它,我不知道它为什么会这样做。

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

代码:

[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')

错误:

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

编辑:

我通过在函数中创建一个对象来解决它,如下所示:

[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
4

1 回答 1

0

在您的代码中,您在调用该方法之前返回该函数.Close()

[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
}

而且,本着 PowerShell 的精神,您并不真正需要调用return. 只需$spd自己添加变量,它就会返回它。

于 2017-06-21T01:34:22.750 回答