-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindExpiredAnyoneLinks.ps1
More file actions
184 lines (177 loc) · 8.32 KB
/
FindExpiredAnyoneLinks.ps1
File metadata and controls
184 lines (177 loc) · 8.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<#
=============================================================================================
Name: Get All Expired Anyone Links in SharePoint Online Using PowerShell
Version: 1.0
Website: o365reports.com
~~~~~~~~~~~~~~~~~~
Script Highlights:
~~~~~~~~~~~~~~~~~~
1. Exports all expired anyone links in your SPO environment.
2. Exports expired anyone links for a list of sites.
3. Automatically installs the PnP PowerShell module (if not installed already) upon your confirmation.
4. The script can be executed with an MFA-enabled account too.
5. Exports report results as a CSV file.
6. The script is scheduler friendly.
7. The script uses modern authentication to connect SharePoint Online.
8. It can be executed with certificate-based authentication (CBA) too.
For detailed script execution: https://o365reports.com/2024/07/16/get-all-expired-anyone-links-in-sharepoint-online-using-powershell/
============================================================================================
#>
Param
(
[Parameter(Mandatory = $false)]
[string]$AdminName ,
[string]$Password ,
[String]$ClientId ,
[String]$CertificateThumbprint,
[string]$TenantName,
[string]$ImportCsv
)
Function Installation-Module{
$Module = Get-InstalledModule -Name PnP.PowerShell -RequiredVersion 1.12.0 -ErrorAction SilentlyContinue
If($Module -eq $null){
Write-Host PnP PowerShell Module is not available -ForegroundColor Yellow
$Confirm = Read-Host Are you sure you want to install module? [Yy] Yes [Nn] No
If($Confirm -match "[yY]") {
Write-Host "Installing PnP PowerShell module..."
Install-Module PnP.PowerShell -RequiredVersion 1.12.0 -Force -AllowClobber -Scope CurrentUser
Import-Module -Name Pnp.Powershell -RequiredVersion 1.12.0
}
Else{
Write-Host PnP PowerShell module is required to connect SharePoint Online.Please install module using Install-Module PnP.PowerShell cmdlet.
Exit
}
}
Write-Host `nConnecting to SharePoint Online...
}
Function Connection-Module{
param
(
[Parameter(Mandatory = $true)]
[String] $Url
)
if(($AdminName -ne "") -and ($Password -ne ""))
{
$SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force
$Credential = New-Object System.Management.Automation.PSCredential $AdminName,$SecuredPassword
Connect-PnPOnline -Url $Url -Credential $Credential
}
elseif($ClientId -ne "" -and $CertificateThumbprint -ne "" -and $TenantName -ne "")
{
Connect-PnPOnline -Url $Url -ClientId $ClientId -Thumbprint $CertificateThumbprint -Tenant "$TenantName.onmicrosoft.com"
}
else
{
Connect-PnPOnline -Url $Url -Interactive
}
}
Function Get-SharedLinkInfo($ListItems) {
$Ctx = Get-PnPContext
ForEach ($Item in $ListItems) {
Write-Progress -Activity ("Site Name: $Site") -Status ("Processing Item: "+ $Item.FieldValues.FileLeafRef)
$HasUniquePermissions = Get-PnPProperty -ClientObject $Item -Property HasUniqueRoleAssignments
If ($HasUniquePermissions) {
$SharingInfo = [Microsoft.SharePoint.Client.ObjectSharingInformation]::GetObjectSharingInformation($Ctx, $Item, $false, $false, $false, $true, $true, $true, $true)
$Ctx.Load($SharingInfo)
$Ctx.ExecuteQuery()
ForEach ($ShareLink in $SharingInfo.SharingLinks) {
If ($ShareLink.Url -and $ShareLink.LinkKind -like "*Anonymous*") {
$LinkStatus = $false
$LinkCreated = ([DateTime]$ShareLink.Created).tolocalTime()
$CurrentDateTime = Get-Date
If($ShareLink.Expiration -ne ""){
$Expiration = ([DateTime]$ShareLink.Expiration).tolocalTime()
If($Expiration -lt $CurrentDateTime){
$daysExpired = ($currentDateTime - $expiration).Days
$LinkStatus = $true
}
}
If($LinkStatus){
If($ShareLink.IsEditLink)
{
$AccessType="Write"
}
ElseIf($shareLink.IsReviewLink)
{
$AccessType="Review"
}
Else
{
$AccessType="Read"
}
$Results = [PSCustomObject]@{
"Site Name" = $Site
"Library" = $List.Title
"File Name" = $Item.FieldValues.FileLeafRef
"File URL" = $Item.FieldValues.FileRef
"Access Type" = $AccessType
"File Type" = $Item.FieldValues.File_x0020_Type
"Link Expired Date" = $Expiration
"Days Since Expired" = $daysExpired
"Link Created Date " = $LinkCreated
"Last Modified On " = ([DateTime]$ShareLink.LastModified).tolocalTime()
"Shared Link" = $ShareLink.Url
}
$Results | Export-CSV -path $ReportOutput -NoTypeInformation -Append -Force
$Global:ItemCount++
}
}
}
}
}
}
Function Get-SharedLinks{
$ExcludedLists = @("Form Templates","Style Library","Site Assets","Site Pages", "Preservation Hold Library", "Pages", "Images",
"Site Collection Documents", "Site Collection Images")
$DocumentLibraries = Get-PnPList | Where-Object {$_.Hidden -eq $False -and $_.Title -notin $ExcludedLists -and $_.BaseType -eq "DocumentLibrary"}
Foreach($List in $DocumentLibraries){
$ListItems = Get-PnPListItem -List $List -PageSize 2000 | Where {$_.FileSystemObjectType -eq "File"}
Get-SharedLinkInfo $ListItems
}
}
Installation-Module
$timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
$ReportOutput = "$PSScriptRoot\Expired_SPO_Shared_Links $timestamp.csv"
$Global:ItemCount = 0
If($TenantName -eq ""){
$TenantName = Read-Host "Enter your tenant name (e.g., 'contoso' for 'contoso.onmicrosoft.com')"
}
If($ImportCsv -ne ""){
$SiteCollections = Import-Csv -Path $ImportCsv
Foreach($Site in $SiteCollections){
Connection-Module -Url $Site.SiteUrl
$Site = (Get-PnPWeb | Select Title).Title
Get-SharedLinks
Disconnect-PnPOnline
}
}
Else{
Connection-Module -Url "https://$TenantName-admin.sharepoint.com"
$SiteCollections = Get-PnPTenantSite | Where -Property Template -NotIn ("SRCHCEN#0", "REDIRECTSITE#0", "SPSMSITEHOST#0", "APPCATALOG#0", "POINTPUBLISHINGHUB#0", "EDISC#0", "STS#-1")
Disconnect-PnPOnline -WarningAction SilentlyContinue
ForEach($Site in $SiteCollections)
{
Connection-Module -Url $Site.Url
$Site = (Get-PnPWeb | Select Title).Title
Get-SharedLinks
Disconnect-PnPOnline -WarningAction SilentlyContinue
}
}
if((Test-Path -Path $ReportOutput) -eq "True")
{
Write-Host `nThe output file contains $Global:ItemCount files
Write-Host `n The Output file availble in: -NoNewline -ForegroundColor Yellow
Write-Host $ReportOutput
Write-Host `n~~ Script prepared by AdminDroid Community ~~`n -ForegroundColor Green
Write-Host "~~ Check out " -NoNewline -ForegroundColor Green; Write-Host "admindroid.com" -ForegroundColor Yellow -NoNewline; Write-Host " to get access to 1800+ Microsoft 365 reports. ~~" -ForegroundColor Green `n`n
$Prompt = New-Object -ComObject wscript.shell
$UserInput = $Prompt.popup("Do you want to open output file?",`
0,"Open Output File",4)
If ($UserInput -eq 6)
{
Invoke-Item "$ReportOutput"
}
}
else{
Write-Host -f Yellow "No Records Found"
}