PowerShell 如何获取文件所有者/作者?

PowerShell 如何获取文件所有者/作者?

以下脚本从 I: 驱动器检索特定文件类型,并将特定文件属性输出到分隔的 CSV 文件。如何返回文件所有者和作者?任何帮助都非常感谢。

PowerShell 如何获取文件所有者/作者?

<#
https://superuser.com/questions/1732588/powershell-how-to-get-file-owner-author/1733110#1733110
#>
$RB21 = "I:\FIRM\RB\2021"
$Path = "C:\Users\User\Desktop\test\support\rfi\TAC.MS Community"
$ObjectProperties = "Author","Keywords"

$test = "C:\Users\User\Desktop\test\support\rfi\TAC.MS Community\TAC.How to Save MSG to open with original content.pptx"
#$From =  "C:\Users\User\Desktop\test\support\rfi\TAC.MS Community"

$From = $RB21
$FileFilterExtension = '*.ppt*'
Get-ChildItem -Path $From -Filter $FileFilterExtension -Force | 
Select-Object -Property  Fullname, LastAccessTime, LastWriteTime, CreationTime, 
@{Name = 'Owner';Expression = {$PSItem.GetAccessControl().Owner}}

$Application = New-Object -ComObject PowerPoint.Application
$Application.Visible = $false
$Binding = "System.Reflection.BindingFlags" -as [type]

$Select = "Name","Created"
$Select += $ObjectProperties
$Document = $Application.Documents.Open($test)


ForEach ($File in (Get-ChildItem $Path -Include *.pptx -Recurse))
{   $Document = $Application.Documents.Open($File.Fullname)
    $Properties = $Document.BuiltInDocumentProperties
    $Hash = @{}
    $Hash.Add("Name",$File.FullName)
    $Hash.Add("Created",$File.CreationTime)
    ForEach ($Property in $ObjectProperties)
    {   $DocProperties = [System.__ComObject].InvokeMember("item",$Binding::GetProperty,$null,$Properties,$Property)
        Try {
            $Value = [System.__ComObject].InvokeMember("value",$binding::GetProperty,$null,$DocProperties,$null)
        }
        Catch {
            $Value = $null
        }
        $Hash.Add($Property,$Value)
    }
    $Document.Close()
    [System.Runtime.InteropServices.Marshal]::ReleaseComObject($Properties) | Out-Null
    [System.Runtime.InteropServices.Marshal]::ReleaseComObject($Document) | Out-Null
    New-Object PSObject -Property $Hash | Select $Select
}
$Application.Quit()

答案1

所有者和作者是不同的东西。

但是,为什么要把第一部分(即基本文件属性和 ACL 内容)弄得太复杂呢?为什么不只针对文件属性和访问控制细节这样做呢?不需要 Get-Acl。

Clear-Host

$From                = 'D:\Temp'
$FileFilterExtension = '*.ppt*'

Get-ChildItem -Path $From -Filter $FileFilterExtension -Force | 
Select-Object -Property  Fullname, LastAccessTime, LastWriteTime, CreationTime, 
@{Name = 'Owner';Expression = {$PSItem.GetAccessControl().Owner}}

    # Results
    <#
    FullName       : D:\Temp\PresentationNotProtected.pptx
    LastAccessTime : 15-Apr-21 00:39:32
    LastWriteTime  : 11-Dec-20 20:05:42
    CreationTime   : 11-Dec-20 20:05:42
    Owner          : O:S-1-5-21-3...
    
    FullName       : D:\Temp\PresentationProtected.pptx
    LastAccessTime : 23-Jun-22 13:54:28
    LastWriteTime  : 11-Dec-20 19:51:11
    CreationTime   : 11-Dec-20 19:49:41
    Owner          : O:S-1-5-21-3...
    
    FullName       : D:\Temp\Test.pptx
    LastAccessTime : 23-Jun-22 13:54:28
    LastWriteTime  : 12-Jun-21 15:17:57
    CreationTime   : 04-Feb-20 16:45:18
    Owner          : BUILTIN\Administrators
    #>

您无法从文件属性中获取“作者”,因为它不存在。您在该对话框中看到的是操作系统的帮助。您必须使用 MS Office COM 才能获取其他数据。因此,这意味着调用 PowerPoint 或其他任何程序来获取该详细信息。

例如这里的代码: https://community.spiceworks.com/topic/421503-powershell-help-getting-file-details

$Path = "S:\it\documentation"
$ObjectProperties = "Author","Keywords"

$Application = New-Object -ComObject Word.Application
$Application.Visible = $false
$Binding = "System.Reflection.BindingFlags" -as [type]

$Select = "Name","Created"
$Select += $ObjectProperties

ForEach ($File in (Get-ChildItem $Path -Include *.doc -Recurse))
{   $Document = $Application.Documents.Open($File.Fullname)
    $Properties = $Document.BuiltInDocumentProperties
    $Hash = @{}
    $Hash.Add("Name",$File.FullName)
    $Hash.Add("Created",$File.CreationTime)
    ForEach ($Property in $ObjectProperties)
    {   $DocProperties = [System.__ComObject].InvokeMember("item",$Binding::GetProperty,$null,$Properties,$Property)
        Try {
            $Value = [System.__ComObject].InvokeMember("value",$binding::GetProperty,$null,$DocProperties,$null)
        }
        Catch {
            $Value = $null
        }
        $Hash.Add($Property,$Value)
    }
    $Document.Close()
    [System.Runtime.InteropServices.Marshal]::ReleaseComObject($Properties) | Out-Null
    [System.Runtime.InteropServices.Marshal]::ReleaseComObject($Document) | Out-Null
    New-Object PSObject -Property $Hash | Select $Select
}
$Application.Quit()

当然,您必须根据您的使用情况对此进行调整。

答案2

可以使用 Shell com 对象检索所有者和作者:

$shell = New-Object -com shell.application
gci | select name,
    @{N='Author';E={$shell.NameSpace(($_.DirectoryName)).ParseName(($_.Name)).ExtendedProperty("System.Author")}},
    @{N='Owner';E={ If ($_.PSIsContainer) {
        $Shell.NameSpace(($_.FullName)).Self.ExtendedProperty("System.FileOwner")
    } Else {
        $shell.NameSpace(($_.DirectoryName)).ParseName(($_.Name)).ExtendedProperty("System.FileOwner")
    }}}

控制台捕获:

PS Downloads> gci | select name ,
>>     @{N='Author';E={$shell.NameSpace(($_.DirectoryName)).ParseName(($_.Name)).extendedProperty("System.Author")}},
>>     @{N='Owner';E={$shell.NameSpace(($_.DirectoryName)).ParseName(($_.Name)).extendedProperty("System.FileOwner")}} | fl

Name   : keyboardtestutility.exe
Author :
Owner  : JP\keith

Name   : LibreOffice_5.1.6_Win_x86.msi
Author : The Document Foundation
Owner  : JP\keith

Name   : LibreOffice_5.1.6_Win_x86_helppack_en-US.msi
Author : The Document Foundation
Owner  : JP\keith

Name   : LibreOffice_5.4.1_Win_x64.msi
Author : The Document Foundation
Owner  : JP\keith

Name   : LibreOffice_5.4.1_Win_x64_helppack_en-US.msi
Author : The Document Foundation
Owner  : JP\keith

Name   : Medium_Icons_Dialogs_SearchResults_AllUssers.reg
Author :
Owner  : JP\keith

Name   : Medium_Icons_Dialogs_SearchResults_Per-User.reg
Author :
Owner  : JP\keith

Name   : Michigan-Services-Schedule-071618.pdf
Author :
Owner  : JP\keith

Name   : mig_-win-3_3_0-ea31_2.exe
Author :
Owner  : JP\keith

Name   : Miller Keith Health Care POA.odt
Author : Presbyterian Church
Owner  : JP\keith

Name   : Miller Keith POA for Property.odt
Author : Jill Gilpin
Owner  : JP\keith

Name   : Miller Keith Will.odt
Author : krouland
Owner  : JP\keith

相关内容