-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGet-SPOWebPartInventory.ps1
71 lines (64 loc) · 2.69 KB
/
Get-SPOWebPartInventory.ps1
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
#Original from: https://www.sharepointdiary.com/2019/08/sharepoint-online-web-part-usage-report-using-powershell.html#ixzz8eQAs8Lio
$CSVPath = "C:\Temp\webparts.csv"
$tenant = "mytenant"
$ownerToSetTemporarely = "admin@$tenant.onmicrosoft.com"
$clientId = "" # Register an app with https://pnp.github.io/powershell/cmdlets/Register-PnPEntraIDAppForInteractiveLogin.html
$spoUrl = "https://$tenant.sharepoint.com"
$adminUrl = "https://$tenant-admin.sharepoint.com"
#TODO: Get Valo Sites
$tenantConnection = Connect-PnPOnline -Url $adminUrl -Interactive -ReturnConnection -ClientId $clientId
$sites = Get-PnPTenantSite -Connection $tenantConnection
foreach ($s in $sites) {
$siteUrl = $s.Url
Write-Host "Working on $siteUrl" -ForegroundColor Green
try {
Set-PnPTenantSite -Identity $siteUrl -Owners $ownerToSetTemporarely -Connection $tenantConnection
Connect-PnPOnline -Url $siteURL -Interactive
#Get all pages from "Site Pages" library
$SitePages = $null
try {
$SitePages = Get-PnPListItem -List "Site Pages"
}
catch {
$SitePages = Get-PnPListItem -List "Websiteseiten"
}
$WebPartsData = @()
#Iterate through each page
ForEach ($Page in $SitePages) {
$pageFileLeafRef = $Page.FieldValues.FileLeafRef
#Get All Web parts from the page
try {
$clientSidePage = Get-PnPClientSidePage -Identity $pageFileLeafRef
$webparts = $clientSidePage.Controls
}
catch {
Write-Host "Error getting webparts for page $pageFileLeafRef" -ForegroundColor Red
continue
}
#Iterate through webparts and collect details
ForEach ($webpart in $webparts) {
#Get Web part properties
$WebPartsData += New-Object PSObject -Property @{
"SiteUrl" = $siteUrl
"PageUrl" = $Page.FieldValues.FileRef
"PageAbsoluteUrl" = $spoUrl + $Page.FieldValues.FileRef
"PageTitle" = $Page.FieldValues.Title
"WebPart Title" = $webpart.Title
"WebPart Properties" = $Webpart.PropertiesJson
}
}
}
#Export Web part data to CSV
if ($WebPartsData.Count -gt 0) {
$WebPartsData
$WebPartsData | Export-Csv -Path $CSVPath -NoTypeInformation -Append
}
}
catch {
Write-Host "Error for site '$siteUrl'" -ForegroundColor Red
$_
}
finally {
Remove-PnPSiteCollectionAdmin -Owners $ownerToSetTemporarely
}
}