我试图在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发布于 2017-06-21 01:34:22
在代码中,在调用.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变量,它就会返回它。
https://stackoverflow.com/questions/44664857
复制相似问题