如何使用特定的文件名(Get-子项目)提取元数据,而不是循环访问ComObject命名空间项 [英] How to extract metadata using a specific filename (get-childitem) rather than looping through ComObject namespace items

查看:17
本文介绍了如何使用特定的文件名(Get-子项目)提取元数据,而不是循环访问ComObject命名空间项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:	empoutput.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()索引吗?

推荐答案

在这里,我也很好奇它是如何完成的,所以我查找了它并创建了一个函数,该函数将该属性添加到[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:TempFile.docx' | Select -Expand CreatedBy

编辑:已修复文件数组!对上一个错误深表歉意。

这篇关于如何使用特定的文件名(Get-子项目)提取元数据,而不是循环访问ComObject命名空间项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆