无法创建列出 SharePoint 共享链接的脚本,而我不是该链接的所有者或成员

无法创建列出 SharePoint 共享链接的脚本,而我不是该链接的所有者或成员

我正在寻找一个脚本,用于列出一个(或所有)SharePoint 网站的所有共享链接,我找到了一个可以指定网站、输入凭据并生成 CSV 报告的脚本。运行良好,但我需要授予自己此网站的权限才能获取它。

请问,我该如何编辑此脚本以以管理员身份列出一个或所有站点的共享链接?

我已经尝试使用 Connect-PnPOnline 进行连接并声明 -TenantAdminUrl、-PnPManagementShell,并使用 Register-PnPManagementShellAccess 授予我的凭据全局权限,但没有成功

这是使用的脚本:

# Parameters
$SiteUrl = "https://xxx.sharepoint.com/sites/xxx"
$ReportOutput = "$env:USERPROFILE\Downloads\sharedlinksandpermission.csv"
$ListName = "Documents"
    
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
$Ctx = Get-PnPContext
$Results = @()
$global:counter = 0
 
#Get all list items in batches
$ListItems = Get-PnPListItem -List $ListName -PageSize 2000
$ItemCount = $ListItems.Count
   
#Iterate through each list item
ForEach($Item in $ListItems)
{
    Write-Progress -PercentComplete ($global:Counter / ($ItemCount) * 100) -Activity "Getting Shared Links from '$($Item.FieldValues["FileRef"])'" -Status "Processing Items $global:Counter to $($ItemCount)";
 
    #Check if the Item has unique permissions
    $HasUniquePermissions = Get-PnPProperty -ClientObject $Item -Property "HasUniqueRoleAssignments"
    If($HasUniquePermissions)
    {
        #Get Users and Assigned permissions
        $RoleAssignments = Get-PnPProperty -ClientObject $Item -Property RoleAssignments
        ForEach($RoleAssignment in $RoleAssignments)
        {
            $Members = Get-PnPProperty -ClientObject $RoleAssignment -Property RoleDefinitionBindings, Member
            #Get list of users
            $Users = Get-PnPProperty -ClientObject ($RoleAssignment.Member) -Property Users -ErrorAction SilentlyContinue
            #Get Access type
            $AccessType = $RoleAssignment.RoleDefinitionBindings.Name
            If ($RoleAssignment.Member.Title -like "SharingLinks*")
            {
                If ($Users -ne $null)
                {
                    ForEach ($User in $Users)
                    {
                        #Collect the data
                        $Results += New-Object PSObject -property $([ordered]@{
                        Name  = $Item.FieldValues["FileLeafRef"]           
                        RelativeURL = $Item.FieldValues["FileRef"]
                        FileType = $Item.FieldValues["File_x0020_Type"]
                        UserName = $user.Title
                        UserAccount  = $User.LoginName
                        Email  =  $User.Email
                        Access = $AccessType
                        })
                    }
                        
                }
            }       
        }
    }      
    $global:counter++
}
$Results | Export-CSV $ReportOutput -NoTypeInformation
Write-host -f Green "Sharing Links Report Generated Successfully!"

如果可能的话,我想了解我可以做哪些更改才能使其正常工作,因为我有其他 2 个脚本存在同样的问题,也使用 Get-PnPListItem、Get-PnPProperty

非常感谢

答案1

SharePoint Online Management Shell如果您没有,则需要使用以下命令进行安装:Install-Module -Name Microsoft.Online.SharePoint.PowerShell

下面是使用它的修改后的脚本:

# Parameters
$TenantUrl = "https://xxx-admin.sharepoint.com"
$ReportOutput = "$env:USERPROFILE\Downloads\sharedlinksandpermission.csv"
$ListName = "Documents"

#Connect to SharePoint Online Management Shell
$UserCredential = Get-Credential
Connect-SPOService -Url $TenantUrl -Credential $UserCredential

# Get all site collections
$SiteCollections = Get-SPOSite

$Results = @()

ForEach ($Site in $SiteCollections) {
    # Connect to PnP Online for the site
    Connect-PnPOnline -Url $Site.Url -Credentials $UserCredential
    $Ctx = Get-PnPContext

    #Get all list items in batches
    $ListItems = Get-PnPListItem -List $ListName -PageSize 2000
    $ItemCount = $ListItems.Count
    $global:counter = 0

    #Iterate through each list item
    ForEach ($Item in $ListItems) {
        Write-Progress -PercentComplete ($global:Counter / ($ItemCount) * 100) -Activity "Getting Shared Links from '$($Item.FieldValues["FileRef"])'" -Status "Processing Items $global:Counter to $($ItemCount)";

        #Check if the Item has unique permissions
        $HasUniquePermissions = Get-PnPProperty -ClientObject $Item -Property "HasUniqueRoleAssignments"
        If ($HasUniquePermissions) {
            #Get Users and Assigned permissions
            $RoleAssignments = Get-PnPProperty -ClientObject $Item -Property RoleAssignments
            ForEach ($RoleAssignment in $RoleAssignments) {
                $Members = Get-PnPProperty -ClientObject $RoleAssignment -Property RoleDefinitionBindings, Member
                #Get list of users
                $Users = Get-PnPProperty -ClientObject ($RoleAssignment.Member) -Property Users -ErrorAction SilentlyContinue
                #Get Access type
                $AccessType = $RoleAssignment.RoleDefinitionBindings.Name
                If ($RoleAssignment.Member.Title -like "SharingLinks*") {
                    If ($Users -ne $null) {
                        ForEach ($User in $Users) {
                            #Collect the data
                            $Results += New-Object PSObject -property $([ordered]@{
                                Name  = $Item.FieldValues["FileLeafRef"]
                                RelativeURL = $Item.FieldValues["FileRef"]
                                FileType = $Item.FieldValues["File_x0020_Type"]
                                UserName = $user.Title
                                UserAccount  = $User.LoginName
                                Email  =  $User.Email
                                Access = $AccessType
                            })
                        }
                    }
                }
            }
        }
        $global:counter++
    }
}

$Results | Export-CSV $ReportOutput -NoTypeInformation
Write-hos

t -f Green "Sharing Links Report Generated Successfully!"

相关内容