forked from jameswh3/MW-Toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-SharePointFileProperties.ps1
More file actions
241 lines (203 loc) · 9.24 KB
/
Get-SharePointFileProperties.ps1
File metadata and controls
241 lines (203 loc) · 9.24 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<#
.SYNOPSIS
Gets metadata properties of a file in a SharePoint document library.
.DESCRIPTION
This script connects to SharePoint Online and retrieves metadata properties
of a specified file in a document library using the file name and library URL.
.PARAMETER SiteUrl
The URL of the SharePoint site (e.g., https://tenant.sharepoint.com/sites/sitename)
.PARAMETER LibraryUrl
The relative URL of the document library (e.g., /sites/sitename/Shared Documents)
.PARAMETER FileName
The name of the file to check metadata for
.PARAMETER Credential
Optional PSCredential object for authentication
.EXAMPLE
.\Get-SharePointFileProperties.ps1 -SiteUrl "https://contoso.sharepoint.com/sites/team" -LibraryUrl "/sites/team/Shared Documents" -FileName "Document.docx"
.NOTES
Requires PnP.PowerShell module: Install-Module PnP.PowerShell
#>
param(
[Parameter(Mandatory = $true)]
[string]$SiteUrl,
[Parameter(Mandatory = $true)]
[string]$LibraryUrl,
[Parameter(Mandatory = $true)]
[string]$FileName,
[Parameter(Mandatory = $true)]
[string]$ClientId,
[Parameter(Mandatory = $true)]
[string]$Tenant,
[Parameter(Mandatory = $true)]
[string]$CertificatePath
)
# Import required module
try {
Import-Module PnP.PowerShell -ErrorAction Stop
Write-Host "PnP.PowerShell module loaded successfully" -ForegroundColor Green
}
catch {
Write-Error "PnP.PowerShell module is required. Install it using: Install-Module PnP.PowerShell"
exit 1
}
try {
# Connect to SharePoint Online
Write-Host "Connecting to SharePoint site: $SiteUrl" -ForegroundColor Yellow
# Validate and clean LibraryUrl
if ([string]::IsNullOrWhiteSpace($LibraryUrl)) {
throw "LibraryUrl parameter cannot be null or empty"
}
# Ensure LibraryUrl starts with /
if (-not $LibraryUrl.StartsWith('/')) {
$LibraryUrl = '/' + $LibraryUrl
}
# Remove trailing slash if present
$LibraryUrl = $LibraryUrl.TrimEnd('/')
# Connect to the SharePoint site
Connect-PnPOnline -Url $siteUrl `
-ClientId $ClientId `
-Tenant $Tenant `
-CertificatePath $CertificatePath
Write-Host "Connected successfully" -ForegroundColor Green
# Construct the full file URL
$fileUrl = "$LibraryUrl/$FileName"
Write-Host "Searching for file at URL: $fileUrl" -ForegroundColor Yellow
# Try to get the file with better error handling
try {
$file = Get-PnPFile -Url $fileUrl -ErrorAction Stop
}
catch {
Write-Host "Direct file lookup failed. Attempting alternative methods..." -ForegroundColor Yellow
# For OneDrive personal sites, try without the trailing folder structure
if ($SiteUrl -like "*-my.sharepoint.com*") {
# Get the personal site identifier from LibraryUrl
$urlParts = $LibraryUrl.Split('/')
$personalSiteId = $urlParts[2] # Should be something like "adile_j3msft_com"
# Try different path variations for OneDrive
$alternativeFileUrl = "/personal/$personalSiteId/Documents/$FileName"
Write-Host "Trying alternative URL for OneDrive: $alternativeFileUrl" -ForegroundColor Yellow
try {
$file = Get-PnPFile -Url $alternativeFileUrl -ErrorAction Stop
$fileUrl = $alternativeFileUrl # Update the working URL
}
catch {
# Try with the subfolder if it exists in the original path
if ($urlParts.Count -gt 4) {
$subFolder = ($urlParts[4..($urlParts.Count-1)] -join '/')
$subFolderUrl = "/personal/$personalSiteId/Documents/$subFolder/$FileName"
Write-Host "Trying with subfolder: $subFolderUrl" -ForegroundColor Yellow
try {
$file = Get-PnPFile -Url $subFolderUrl -ErrorAction Stop
$fileUrl = $subFolderUrl
}
catch {
throw "File '$FileName' not found. Tried paths: '$fileUrl', '$alternativeFileUrl', '$subFolderUrl'"
}
}
else {
throw "File '$FileName' not found. Tried paths: '$fileUrl', '$alternativeFileUrl'"
}
}
}
else {
throw "File '$FileName' not found at '$fileUrl'. Error: $($_.Exception.Message)"
}
}
if ($file) {
Write-Host "File found! Retrieving metadata..." -ForegroundColor Green
Write-Host "File location: $($file.ServerRelativeUrl)" -ForegroundColor Green
# Extract list name from the file's actual location
$actualLibraryPath = Split-Path $file.ServerRelativeUrl -Parent
$listName = Split-Path $actualLibraryPath -Leaf
# For OneDrive, the list name is typically "Documents"
if ($SiteUrl -like "*-my.sharepoint.com*" -and $listName -eq "Documents") {
$listName = "Documents"
}
Write-Host "Using list name: '$listName'" -ForegroundColor Yellow
# Get file properties and metadata with improved error handling
try {
$listItem = Get-PnPListItem -List $listName -Query "<View><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>$FileName</Value></Eq></Where></Query></View>" -ErrorAction Stop
}
catch {
Write-Host "Failed to get list item with list name '$listName'. Trying alternative method..." -ForegroundColor Yellow
# Alternative: Get the list item directly from the file
$list = Get-PnPList | Where-Object { $_.RootFolder.ServerRelativeUrl -eq $actualLibraryPath }
if ($list) {
$listItem = Get-PnPListItem -List $list.Title -Query "<View><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>$FileName</Value></Eq></Where></Query></View>"
}
else {
throw "Could not find the document library at '$actualLibraryPath'"
}
}
if ($listItem) {
# Create custom object with file properties
$fileProperties = [PSCustomObject]@{
FileName = $file.Name
FilePath = $file.ServerRelativeUrl
FileSize = [math]::Round($file.Length / 1KB, 2)
Created = $listItem.FieldValues.Created
Modified = $listItem.FieldValues.Modified
CreatedBy = $listItem.FieldValues.Author.LookupValue
ModifiedBy = $listItem.FieldValues.Editor.LookupValue
CheckoutUser = $listItem.FieldValues.CheckoutUser.LookupValue
Title = $listItem.FieldValues.Title
ContentType = $listItem.FieldValues.ContentType.LookupValue
Version = $listItem.FieldValues._UIVersionString
}
# Display basic properties
Write-Host "`n=== FILE PROPERTIES ===" -ForegroundColor Cyan
$fileProperties | Format-List
# Display all metadata fields
Write-Host "`n=== ALL METADATA FIELDS ===" -ForegroundColor Cyan
$listItem.FieldValues.GetEnumerator() |
Where-Object { $_.Key -notlike "*." -and $_.Value -ne $null } |
Sort-Object Key |
ForEach-Object {
$value = if ($_.Value.GetType().Name -eq "FieldLookupValue") {
$_.Value.LookupValue
} elseif ($_.Value.GetType().Name -eq "FieldUserValue") {
$_.Value.LookupValue
} else {
$_.Value
}
Write-Host "$($_.Key): $value" -ForegroundColor White
}
# Return the properties object
return $fileProperties
}
else {
Write-Warning "Could not retrieve list item metadata for the file"
}
}
else {
Write-Error "File '$FileName' not found in library '$LibraryUrl'"
}
}
catch {
Write-Error "Error occurred: $($_.Exception.Message)"
Write-Error "Stack trace: $($_.ScriptStackTrace)"
# Add debugging information
Write-Host "`nDebugging Information:" -ForegroundColor Red
Write-Host "SiteUrl: '$SiteUrl'" -ForegroundColor Red
Write-Host "LibraryUrl: '$LibraryUrl'" -ForegroundColor Red
Write-Host "FileName: '$FileName'" -ForegroundColor Red
if ($fileUrl) {
Write-Host "Constructed FileUrl: '$fileUrl'" -ForegroundColor Red
}
# Add suggestions for OneDrive paths
if ($SiteUrl -like "*-my.sharepoint.com*") {
Write-Host "`nFor OneDrive personal sites, try these LibraryUrl formats:" -ForegroundColor Yellow
Write-Host " - /personal/username_domain_com/Documents" -ForegroundColor Yellow
Write-Host " - /personal/username_domain_com/Documents/FolderName" -ForegroundColor Yellow
}
}
finally {
# Disconnect from SharePoint
try {
# Disconnect-PnPOnline
Write-Host "Disconnected from SharePoint" -ForegroundColor Green
}
catch {
Write-Warning "Could not disconnect cleanly from SharePoint"
}
}