获取文件"最后由"保存财产没有改变它

时间:2015-12-17 14:03:09

标签: windows excel powershell properties ms-word

我尝试使用PowerShell中的代码行获取文件的属性(所有者):

    $file = "\\networkshare\directory\file.doc"
    Get-ItemProperty -Path $file | Format-list -Property * -Force

很容易提取所有者,修改日期等。但我想提取"最后保存的'和'修订号':

enter image description here

更新

以下代码似乎有效。但每次我运行脚本时,都会更改"的值,最后由"保存。如何防止这种情况只能读取属性?

 $word = New-Object -Com Word.Application
$word.Visible = $false #to prevent the document you open to show
$doc = $word.Documents.Open("\\networkshare\directory\file.doc")

$binding = "System.Reflection.BindingFlags" -as [type]
Foreach($property in $doc.BuiltInDocumentProperties) {
   try {
      $pn = [System.__ComObject].invokemember("name",$binding::GetProperty,$null,$property,$null)
      if ($pn -eq "Last author") {
         $lastSaved = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$property,$null)
         echo "Last saved by: "$lastSaved      
      }        }
   catch { }
}

$doc.Close()
$word.Quit()

1 个答案:

答案 0 :(得分:3)

这是因为您在致电$doc.Close()

时正在保存文档

只需使用SaveChanges将Close调用为false:

$doc.Close($false)

您的代码(我还在只读模式中添加了open):

$word = New-Object -Com Word.Application
$word.Visible = $false #to prevent the document you open to show
$doc = $word.Documents.Open("\\networkshare\directory\file.doc", $false, $true) # open in read only mode

$binding = "System.Reflection.BindingFlags" -as [type]
Foreach($property in $doc.BuiltInDocumentProperties) {
   try {
      $pn = [System.__ComObject].invokemember("name",$binding::GetProperty,$null,$property,$null)
      if ($pn -eq "Last author") {
         $lastSaved = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$property,$null)
         echo "Last saved by: "$lastSaved      
      }        }
   catch { }
}

$doc.Close($false) 
$word.Quit()