使用powershell查找word和excel文档的最后保存者

发布于 2024-10-31 15:58:04 字数 839 浏览 0 评论 0原文

我有一个包含子文件夹的文件夹,我想运行一个 powershell 脚本来查找所有 Office 文档(目前为 word 和 excel 2003、2007 和 2010)并打印我们可以在属性上找到的“上次保存者”属性,文件的详细信息选项卡。

有人可以帮忙吗?

--- 解决方案 ---

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

$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)
         write-host "Last saved by: "$lastSaved      
      }        }
   catch { }
}

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

对正确答案进行了一些调整,现在它可以工作了。

I have a folder with sub folders and I want to run a powershell script that finds all the office documents (word and excel 2003,2007 and 2010 for the moment) and print the "last saved by" property that we can find on the properties, details tab of the files.

Can anybody help?

--- solution ---

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

$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)
         write-host "Last saved by: "$lastSaved      
      }        }
   catch { }
}

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

Did some adjustments to the correct answer and now it's working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

‘画卷フ 2024-11-07 15:58:04

这并不像人们希望的那么容易。您可以轻松地从 Powershell 打开 Word 文档,

$word = New-Object -COM Word.Application
$word.Visible = $false #to prevent the document you open to show
$doc = $word.Document.Open("path-to-document")

但文档属性存储在属性BuiltInDocumentProperties 中,它本身是动态 COM 对象(因此不能直接使用)。

我使用的方法是遍历每个属性,然后检索值:

$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 save time") {
         $lastSaved = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$property,$null)
      }
   }
   catch { }
}

只需打印 $pn 变量即可获取所有可用属性的名称。

It's not as easy as one would have hoped. You can easily open a Word document from Powershell with

$word = New-Object -COM Word.Application
$word.Visible = $false #to prevent the document you open to show
$doc = $word.Document.Open("path-to-document")

But Document properties are stored in the property BuiltInDocumentProperties, which in themselves are dynamic COM-objects (and therefore not directly available)

The method I've used is to traverse each of these properties and then retrieved the value:

$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 save time") {
         $lastSaved = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$property,$null)
      }
   }
   catch { }
}

You can get the names of all available properties by simply printing the $pn variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文