gpt4 book ai didi

windows - 获取文件 "last saved by"属性而不更改它

转载 作者:可可西里 更新时间:2023-11-01 10:04:51 31 4
gpt4 key购买 nike

我尝试使用 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()

最佳答案

发生这种情况是因为您在调用 $doc.Close()

时正在保存文档

只需调用 Close将 SaveChanges 设置为 false:

$doc.Close($false)

您的代码(我还添加了以只读模式打开):

$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()

关于windows - 获取文件 "last saved by"属性而不更改它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34336486/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com