Get Site Classification via PowerShell

Viewed 381

For internal audit /validation do I need to load all sites in my tenant where the classification of the site is set to "Internal Only" (custom classification created for our tenant).

It seems like that I cannot retrieve the classification using the standard PowerShell module Microsoft.Online.SharePoint.PowerShell - the object returned by Get-SPOSite simply does not provided a property Classification.

The only way I have found is using PnP.PowerShell, see Microsoft documentation Programmatically read the classification of a site:

Connect-PnPOnline "https://[tenant].sharepoint.com/sites/[modernsite]" -Credentials [credentials]

$site = Get-PnPSite
$classificationValue = Get-PnPProperty -ClientObject $site -Property Classification
Write-Host $classificationValue

But the problem with the PnP.PowerShell based approach is the fact that I need be be an admin /owner of that site in order to load the site-object. I case I am not an admin /owner is the following error raised when executing Get-PnPSite:

Get-PnPSite : The remote server returned an error: (401) Unauthorized.

Question is now if I could retrieve the classification of all sites in my tenant without being a site-collection administrator?

2 Answers

The site collection adiminstator is necessary to retrieve the Classification property.

As a tenant admin, you could add yourself as a site collection admin through powershell. After retrieving the information, just remove Admin account from site colletion administrators.

Like the below:

$site="https://[tenant].sharepoint.com/sites/[modernsite]"
$admin="admin@tenant.com"
#Add Admin account as an additional site collection adminstrator
Set-PnPTenantSite  $site -Owners $admin

Connect-PnPOnline $site -Credentials [credentials]

$site = Get-PnPSite
$classificationValue = Get-PnPProperty -ClientObject $site -Property Classification
Write-Host $classificationValue

#Remove Admin account from site colletion administrators
Remove-PnPSiteCollectionAdmin -Owners $admin

You really need that SharePoint admin role for this to work... There is no way around it.

Related