使用powershell查找word和excel文档的最后保存者
我有一个包含子文件夹的文件夹,我想运行一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这并不像人们希望的那么容易。您可以轻松地从 Powershell 打开 Word 文档,
但文档属性存储在属性BuiltInDocumentProperties 中,它本身是动态 COM 对象(因此不能直接使用)。
我使用的方法是遍历每个属性,然后检索值:
只需打印 $pn 变量即可获取所有可用属性的名称。
It's not as easy as one would have hoped. You can easily open a Word document from Powershell with
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:
You can get the names of all available properties by simply printing the $pn variable.