网站搜索

分配给 SharePoint Online 文档的报告敏感度标签


检索 SharePoint Online 文档的敏感度标签信息

2022 年 7 月,我撰写了有关使用图形 API 创建 SharePoint Online 网站中存储的文件报告的文章。这是一个有趣的项目,我学到了很多关于使用驱动器和驱动器项目等概念通过图形请求导航 SharePoint Online 的知识。我经常使用脚本生成报告,所以我从练习中得到了一些东西。

我无法报告的一件事是报告的文件是否有敏感度标签。我过去创建过脚本来检索带有敏感度标签的文件(此示例提供了使用 Unlock-SPOSensitivityLabelEncryptedFile cmdlet 解密带有敏感度标签的文件的机会),其中像下面所示的请求会检索必要信息:

$Uri = "https://graph.microsoft.com/v1.0/sites/$($Siteid)/lists/Documents/Drive/Items/$($DriveId)/children?`$select=sensitivitylabel,weburl,name"} 
[array]$Files = (Invoke-RestMethod -Uri $URI -Headers $Headers -Method Get -ContentType "application/json")

我本可以继续沿该路径检索敏感度标签信息,但我决定使用新的 extractSensitivityLabels API。此 API 的最大区别在于,当您使用它时,API 会更新“使用分配标签的最新详细信息的驱动器项目的元数据”。正如我们将看到的,API 的这方面可能会产生意想不到的副作用。

此外,API 还可以处理“分配给驱动器项目的一个或多个敏感度标签”的提取。我不知道 SharePoint Online 文档可以有多个敏感度标签,但显然,如果文档从一个租户传递到另一个租户并从每个租户积累标签,则可以拥有多个敏感度标签。但是,一旦用户应用加密标签,文档就可以只存储该标签。

Quest 的 Microsoft 平台迁移规划和整合

检索敏感度标签数据所做的更改

为了测试 API,我在原始脚本(可从 GitHub 获取)中的 UnpackFilesRecursively 函数中添加了一些代码。如果您想尝试该脚本,请下载该脚本并插入以下代码来替换原始的 UnpackFilesRecursively 函数:

Function UnpackFilesRecursively {
# Unpack set of items (files and folders)
param (
        [parameter(Mandatory = $true)]
        $Items, # Items to unpack
	
		[parameter(Mandatory = $true)]
        $SiteUri, # Base site URI
		
		[parameter(Mandatory = $true)]
        $FolderPath, # Folder path
		
        [parameter(Mandatory = $true)]
        $SiteFiles,
		
		[parameter(Mandatory = $false)]
		[bool]$IsNextLink
    )

  # Sensitivity label document types
  [array]$ValidDocumentTypes = "docx", "pptx", "xlsx", "pdf"
  # Find sub-folders that we need to check for files
  [array]$Folders = $Items.Value | Where-Object {$_.Folder.ChildCount -gt 0 }
  # And any files in the folder
  [array]$Files = $Items.Value | Where-Object {$_.Folder.ChildCount -eq $Null}
  
  $before = $SiteFiles.count
  
  # Report the files
  ForEach ($D in $Files) {
    $LabelName = $Null
    $FileSize = FormatFileSize $D.Size
    # Check Sensitivity label
    $Type = $D.Name.Split(".")[1]
    If ($Type -in $ValidDocumentTypes) { 
     # Write-Host "Processing filename:" $D.Name
      $Uri = ("https://graph.microsoft.com/beta/sites/{0}/drive/items/{1}/extractSensitivityLabels" -f $Site.Id, $D.id)
      Try {
         $LabelsInfo = Invoke-MgGraphRequest -Uri $Uri -Method POST }
      Catch {
         Write-Host ("Failure reading data from file {0}" -f $D.Name) 
         $LabelsInfo = $Null
      }
      # Resolve sensitivity label identifier if one is found to find label name
      If ($LabelsInfo.labels.sensitivityLabelId) { 
      #  Write-Host "Label Id" $LabelsInfo.labels.sensitivityLabelId 
        $LabelName = $LabelsHash[$LabelsInfo.labels.sensitivityLabelId ]
     } # End if Label data
    } # End if type
    $ReportLine  = [PSCustomObject] @{   
        FileName            = $D.Name
        Folder              = $FolderPath
        Author              = $D.createdby.user.displayname
        Created             = $D.createdDateTime
        Modified            = $D.lastModifiedDateTime
        Size                = $FileSize
        'Sensitivity Label' = $LabelName
        Uri                 = $D.WebUrl 
        Id                  = $D.Id}
     $SiteFiles.Add($ReportLine) 
  } # End If

  $NextLink = $Items."@odata.nextLink"
  $Uri = $Items."@odata.nextLink"
  While ($NextLink) { 
    $MoreData = Invoke-MgGraphRequest -Uri $Uri -Method Get
    UnpackFilesRecursively -Items $MoreData -SiteUri $SiteUri -FolderPath $FolderPath -SiteFiles $SiteFiles -IsNextLink $true
  
    $NextLink = $MoreData."@odata.nextLink"
    $Uri = $MoreData."@odata.nextLink" 
  } # End While
  
  $count = $SiteFiles.count - $before
  if (-Not $IsNextLink) {
    Write-Host "  $FolderPath ($count)"
  }
  
  # Report the files in each sub-folder
  ForEach ($Folder in $Folders) {
	$NewFolderPath = $FolderPath + "/" + $Folder.Name
	$Uri = $SiteUri + "/" + $Folder.parentReference.path + "/" + $Folder.Name + ":/children"
	$SubFolderData = Invoke-MgGraphRequest -Uri $Uri -Method Get
    UnpackFilesRecursively -Items $SubFolderData -SiteUri $SiteUri -FolderPath $NewFolderPath -SiteFiles $SiteFiles -IsNextLink $IsNextLink
  } # End Foreach Folders
}

您可以看到该脚本声明了文件扩展名集,并且它将检查敏感度标签。我已将 Word、PowerPoint、Excel 和 PDF 的扩展添加到该数组中。随着时间的推移,随着 Microsoft 信息保护支持其他文件类型,可以添加这些文件的扩展名。

解释敏感度标签数据

文件返回的信息如下所示:

Name                           Value
----                           -----
tenantId                       a662313f-14fc-43a2-9a7a-d2e27f4f3478
assignmentMethod               standard
sensitivityLabelId             1b070e6f-4b3c-4534-95c4-08335a5ca610

并不是每个人都能说流利的 GUID,因此为了将敏感度标签标识符解释为标签名称,我们创建一个哈希表来保存脚本可以查找的标签标识符和名称。此代码必须在脚本开始时运行:

Connect-IPPSSession
[Array]$LabelData = Get-Label | Select-Object ImmutableId, DisplayName
$Global:LabelsHash = @{}
ForEach ($L in $LabelData) {$LabelsHash.Add([string]$L.ImmutableId,[string]$L.DisplayName) }

一切都运行得非常好(图 1),但需要注意的是,脚本现在对每个具有受支持文件类型的文档发出额外的图形请求。这将不可避免地减慢处理速度。我已经针对包含数千个文件的文档库运行了该脚本,并且性能并非不可接受。获取非常大的文档(例如 1,400 页 38 MB Word 文档)的标签信息可能需要一秒钟左右的时间,但较小的文件不会导致较大的延迟。

文件不匹配

如上所述,出现了意想不到的副作用。由于 API 使用最新的标签元数据更新 SharePoint 网站,因此针对大型文档库运行脚本会导致十个文档不匹配通知快速连续到达。解释很简单:每个敏感度标签都有一个优先级顺序。如果将具有高优先级标签的文档放入分配了较低优先级(容器管理)标签的站点中,则会发生不匹配。就我而言,分配给文档的敏感度标签的优先级自最初分配以来发生了变化,并且文档现在具有比网站更高优先级的标签。

数据治理报告

Microsoft Syntex-SharePoint 高级管理许可证的好处之一是管理员可以通过 SharePoint Online 管理中心访问数据治理报告(图 2)。

数据治理报告没什么特别的,当然也不是购买高级管理许可证的充分理由。您可以使用 PowerShell 自己做得更好,包括输出到(使用 PSWriteHTML 模块)或 Excel(使用 ImportExcel 模块)。如果您对 Microsoft Syntex-SharePoint 高级管理许可证感兴趣,请重点关注阻止下载 Teams 会议录音等功能。你会更快乐。

下一站,使用图形 API 分配敏感度标签

除了用于检索 SharePoint Online 文档的敏感度标签信息的 API 之外,assignSensitivityLabelGraph API 可用于为文档分配敏感度标签。问题在于该 API 是受到计量和保护的。计量意味着您需要通过 Azure 订阅付费才能使用它。受保护意味着 Microsoft 必须同意应用程序使用该 API。我已申请许可并准备 Azure 接受费用。一旦 Microsoft 同意,我将报告使用 API 分配敏感度标签的情况。

相关文章