我正在尝试在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
答案 0 :(得分: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
变量,它就会返回它。