0

我找到了多个代码片段来滚动浏览文件夹并显示文件夹中每个项目的元数据,如下所示:

function funLine($strIN) 
{
    $strLine = "=" * $strIn.length
    Write-Host -ForegroundColor Yellow "`n$strIN"
    Write-Host -ForegroundColor Cyan $strLine
}

$sfolder = "S:\Temp"
$objShell = New-Object -ComObject Shell.Application
$objFolder = $objShell.namespace($sFolder)
foreach ($strFileName in $objFolder.items())
    {funline "$($strFileName.name)"
    for ($a ; $a  -le 266; $a++)
    { 
        $a
        if($objFolder.getDetailsOf($strFileName, $a))
        {
            $hash += @{ $($objFolder.getDetailsOf($objFolder.items, $a)) = $a.tostring() + $($objFolder.getDetailsOf($strFileName, $a)) }
            $hash | out-file c:\temp\output.txt -Append
            $hash.clear()
        }
    }
    $a=0
}

但在我的脚本中,我想使用 Get-ChildItem 遍历文件夹,对于选定的文件,我想使用 getDetailsOf() 来提取 MS Office 文档的作者。

那么,知道文件名(例如:$strFileName,我可以跳过 $objFolder.items() 中的每个 $strFileName 的循环,而只访问 $sFileName 的作者的元数据详细信息(其中 $a = 20)吗?

我已经看到它是使用“New-Object -ComObject word.application”完成的,但我相信它会打开文档,所以在一个有许多文件被用户锁定的大型文件系统上,这可能会很慢而且很痛苦。

我可以直接跳到我选择的文件名的 $objFolder.items() 的索引吗?

4

2 回答 2

4

在这里,我也很好奇它是如何完成的,所以我查找了它并创建了一个函数,该函数将该属性添加到您的[FileInfo]对象(通常由Get-ChildItemcmdlet 为文件传递的内容)。

Function Get-CreatedBy{
[cmdletbinding()]
Param(
    [Parameter(ValueFromPipelineByPropertyName=$true)]
    [Alias("Path")]
    [string[]]$FullName
)
Begin{
    $Shell = New-Object -ComObject Shell.Application
}
Process{
    ForEach($FilePath in $FullName){
        $NameSpace = $Shell.NameSpace((Split-Path $FilePath))
        $File = $NameSpace.ParseName((Split-Path $FilePath -Leaf))
        $CreatedBy = $NameSpace.GetDetailsOf($File,20)
        [System.IO.FileInfo]$FilePath|Add-Member 'CreatedBy' $CreatedBy -PassThru
    }
}
}

然后你可以只管东西到那个,或者直接指定一个路径,比如:

Get-ChildItem *.docx | Get-CreatedBy | FT Name,CreatedBy

或者

Get-CreatedBy 'C:\Temp\File.docx' | Select -Expand CreatedBy

编辑:修复了文件数组!很抱歉之前的错误。

于 2015-11-12T19:30:39.137 回答
2

谢谢马特!尽管那个问题有所不同,但它有我正在寻找的一块 - 如何参考$objFolder.items().item($_.Name)

因此,这是一个快速的小片段来显示作者(或任何其他元数据字段):

$FullName = "S:\Temp\filename.xlsx"

$Folder = Split-Path $FullName
$File = Split-Path $FullName -Leaf

$objShell = New-Object -ComObject Shell.Application
$objFolder = $objShell.namespace($Folder)

$Item = $objFolder.items().item($File)
$Author = $objFolder.getDetailsOf($Item, 20)

Write-Host "$FullName is owned by $Author"

其中 Author 是第 20 个元数据项。

于 2015-11-12T19:29:23.237 回答