-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetSharePointMetaData.ps1
More file actions
174 lines (152 loc) · 6.3 KB
/
Copy pathSetSharePointMetaData.ps1
File metadata and controls
174 lines (152 loc) · 6.3 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
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]
$SiteUrl,
[Parameter(Mandatory = $true)]
[string]
$DocumentLibrary,
[Parameter(Mandatory = $true)]
[string]
$CSVFile
)
BEGIN {
$ErrorActionPreference = "Stop"
# Disconnect PNP Online
try {
Disconnect-PnPOnline
}
Catch {}
# Connect PNP Online
Connect-PnPOnline -Url $SiteUrl -UseWebLogin
# Get all Taxonomy IDs
Write-Host "Getting all Taxonomies..." -NoNewline
$TermIds = Export-PnPTaxonomy -IncludeID
Write-Host "DONE" -ForegroundColor Green
$CSV = Import-Csv -LiteralPath $CSVFile
}
PROCESS {
$ErrorList = @()
$Total = $CSV | Measure-Object | Select-Object -ExpandProperty Count
$i = 0
foreach ($Line in $CSV) {
$RelativePath = $Line.RelativePath
$VendorFullPath = $Line.VendorFullPath
$VehicleFullPath = $Line.VehicleFullPath
$CustomersFullPath = $Line.CustomersFullPath
$ManufacturerFullPath = $Line.ManufacturerFullPath
# Update relative path
$RelativePath = $RelativePath -replace "^/teams/Acctg", "/sites/Taborda_Acctg"
Write-Progress -Activity "Adding metadata to files..." -Status "Files: [ $i / $Total ] | Errors: $($ErrorList | Measure-Object | Select-Object -ExpandProperty Count) | Current File: $RelativePath" -PercentComplete (($i / $Total) * 100)
$i++
if (-not $RelativePath) {
$Line | Add-Member -MemberType NoteProperty -Name Error -Value "Relative Path can't be empty."
$ErrorList += $Line
$Line | Out-Host
Continue
}
Try {
$File = Get-PnPFile -Url $RelativePath -AsListItem
}
Catch {
$err = $_
$Line | Add-Member -MemberType NoteProperty -Name Error -Value "Error getting PNP file. Error: $err"
$ErrorList += $Line
$Line | Out-Host
Continue
}
if (-not $File) {
$Line | Add-Member -MemberType NoteProperty -Name Error -Value "Failed to find file. Skipping"
$ErrorList += $Line
$Line | Out-Host
Continue
}
# Vendor
if ($VendorFullPath) {
$VendorLabel = ($VendorFullPath -split "\|")
$VendorSearch = (($VendorLabel | ForEach-Object { "$_;#*|" }) -join "").TrimEnd("|")
$Vendor_TermID = $TermIds | Where-Object { $_ -like $VendorSearch }
if (($Vendor_TermID | Measure-Object | Select-Object -ExpandProperty Count) -ne 1) {
$Line | Add-Member -MemberType NoteProperty -Name Error -Value "Found multiple Vendor term IDs. Skipping"
$ErrorList += $Line
$Line | Out-Host
Continue
}
$Vendor_TermID = ($Vendor_TermID -split "#")[-1]
}
else {
$Vendor_TermID = $null
}
# Vehicle
if ($VehicleFullPath) {
$VehicleLabel = ($VehicleFullPath -split "\|")
$VehicleSearch = (($VehicleLabel | ForEach-Object { "$_;#*|" }) -join "").TrimEnd("|")
$Vehicle_TermID = $TermIds | Where-Object { $_ -like $VehicleSearch }
if (($Vehicle_TermID | Measure-Object | Select-Object -ExpandProperty Count) -ne 1) {
$Line | Add-Member -MemberType NoteProperty -Name Error -Value "Found multiple Vehicle term IDs. Skipping"
$ErrorList += $Line
$Line | Out-Host
Continue
}
$Vehicle_TermID = ($Vehicle_TermID -split "#")[-1]
}
else {
$Vehicle_TermID = $null
}
# Customers
if ($CustomersFullPath) {
$CustomersLabel = ($CustomersFullPath -split "\|")
$CustomersSearch = (($CustomersLabel | ForEach-Object { "$_;#*|" }) -join "").TrimEnd("|")
$Customers_TermID = $TermIds | Where-Object { $_ -like $CustomersSearch }
if (($Customers_TermID | Measure-Object | Select-Object -ExpandProperty Count) -ne 1) {
$Line | Add-Member -MemberType NoteProperty -Name Error -Value "Found multiple Customers term IDs. Skipping"
$ErrorList += $Line
$Line | Out-Host
Continue
}
$Customers_TermID = ($Customers_TermID -split "#")[-1]
}
else {
$Customers_TermID = $null
}
# Manufacturer
if ($ManufacturerFullPath) {
$ManufacturerLabel = ($ManufacturerFullPath -split "\|")
$ManufacturerSearch = (($ManufacturerLabel | ForEach-Object { "$_;#*|" }) -join "").TrimEnd("|")
$Manufacturer_TermID = $TermIds | Where-Object { $_ -like $ManufacturerSearch }
if (($Manufacturer_TermID | Measure-Object | Select-Object -ExpandProperty Count) -ne 1) {
$Line | Add-Member -MemberType NoteProperty -Name Error -Value "Found multiple Manufacturer term IDs. Skipping"
$ErrorList += $Line
$Line | Out-Host
Continue
}
$Manufacturer_TermID = ($Manufacturer_TermID -split "#")[-1]
}
else {
$Manufacturer_TermID = $null
}
try {
Set-PnpListItem -List $DocumentLibrary -Identity $File.Id -Values @{"Vendor" = $Vendor_TermID; "Vehicle" = $Vehicle_TermID; "Customers" = $Customers_TermID; "Manufacturer" = $Manufacturer_TermID } | Out-Null
Write-Host "Filename: $($Line.FileName)"
Write-Host "`tVendor: $VendorFullPath ($Vendor_TermID)"
Write-Host "`tVehicle: $VehicleFullPath ($Vehicle_TermID)"
Write-Host "`tCustomers: $CustomersFullPath ($Customers_TermID)"
Write-Host "`tManufacturer: $ManufacturerFullPath ($Manufacturer_TermID)"
}
Catch {
$err = $_
$Line | Add-Member -MemberType NoteProperty -Name Error -Value "Error setting PNP list item. Error: $err"
$ErrorList += $Line
$Line | Out-Host
Continue
}
}
}
END {
if ($ErrorList) {
$Date = Get-Date -Format "yyyy-MM-dd_HH.mm"
$ErrorFile = "SharePoint_MetaData_Update_Error_List_$Date.csv"
$ErrorList | Export-Csv -NoTypeInformation -LiteralPath $ErrorFile
Write-Host "Error file saved to: $ErrorFile"
}
}