当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 获取文件属性

作者:精品下载站 日期:2024-12-14 14:55:51 浏览:14 分类:玩电脑

SharePoint Online:使用 PowerShell 获取文件属性


要求:使用 PowerShell 获取 SharePoint Online 中的文件属性。

[玩转系统] SharePoint Online:使用 PowerShell 获取文件属性

用于获取文件属性的 SharePoint Online PowerShell

文档属性是一组元数据,提供有关文档的附加信息,例如作者、创建日期、文件类型和任何自定义列数据。通过使用 PowerShell,您可以快速检索一个或多个文档的文档属性。在本文中,我们将探讨如何使用 PowerShell 获取 SharePoint Online 中的文档属性。让我们开始吧!


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Get-SPOFileProperties($SiteURL,$FileRelativeURL)
{
    #Setup Credentials to connect
    $Cred = Get-Credential
    $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

    Try {
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Cred

        #Get the File
        $File = $Ctx.Web.GetFileByServerRelativeUrl($FileRelativeURL)
        $Ctx.Load($File)
        $Ctx.Load($File.Author)
        $Ctx.Load($File.ModifiedBy)        
        $Ctx.Load($File.CheckedOutByUser)
        $Ctx.Load($File.Author)
        $Ctx.ExecuteQuery()

        #Get File Properties
        Write-host "File Name:" $File.Name
        Write-host "Title:" $File.Title
        Write-host "File Size:" $File.Length
        Write-host "Created:" $File.TimeCreated
        Write-host "Last Modified:" $File.TimeLastModified
        Write-host "Created By:" $File.Author.Title
        Write-host "Modified By:" $File.ModifiedBy.Title
        Write-host "Checked Out To:" $File.CheckedOutByUser.Title
        Write-host "UI Version:" $File.UIVersion
        Write-host "Major Version:" $File.MajorVersion
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}

#Call the Function with Site URL and File URL
Get-SPOFileProperties -SiteURL "https://Crescent.sharepoint.com" -FileRelativeURL "/Shared Documents/Discloser Asia.docx"

PowerShell 在 SharePoint Online 中获取文档元数据

如果您需要检索文件的自定义列值:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Set Variables
$SiteURL = "https://Crescent.sharepoint.com" 
$FileRelativeURL = "/Docs/Compliance Process.xlsx"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred

    #Get the File
    $File = $Ctx.Web.GetFileByServerRelativeUrl($FileRelativeURL)
    $Ctx.Load($File)

    #Get the List Item with all metadata fields of the File
    $ListItem = $File.ListItemAllFields
    $Ctx.Load($ListItem)
    $Ctx.ExecuteQuery()

    #Get Metadata of the File
    Write-host "File Name:" $File.Name
    
    #Get the "Status" column value of the File
    Write-host "Status:" $ListItem["Status"]
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

PnP PowerShell 获取文件元数据列值

您将能够使用 PnP PowerShell 快速获取 SharePoint Online 中文件的属性,如下所示:


#Parameter
$SiteURL= "https://crescent.sharepoint.com/sites/marketing/"
$FileRelativeURL = "/sites/marketing/Shared Documents/Proposal Template.docx"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the File as List Item
$File = Get-PnPFile -Url $FileRelativeURL -AsListItem

#To Get All Available Properties: $File.FieldValues
#Get File Metadata Properties - Field Values
Write-host "File Name:"$File["FileLeafRef"]
Write-host "Created By:"$File["Created_x0020_By"]
Write-host "Last Modified:"$File["Last_x0020_Modified"]
Write-host "File Size:"$File["File_x0020_Size"]

总之,当您需要生成报告或审核大量文档并使用该信息来组织、排序或筛选文档时,使用 PowerShell 获取 SharePoint Online 中的文档属性会很有帮助。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯