0

Hey Everyone so I am able to set the sharepoint sites sensitivity labels in my tenant by doing them individually using the following commands:

# Get the label (required ExchangeOnlineManagement module)
Connect-IPPSSession -userprincipalname wise@redacted
 
$c = Get-Credential wise@redacted
 
Connect-SPOService -Url https://redacted-admin.sharepoint.com -Credential $c -ModernAuth:$true -AuthenticationUrl https://login.microsoftonline.com/organizations

Set-SPOSite -identity {site_url} -SensitivityLabel:'{GUID of sens label here}'

And now I am trying to set a default label for all sites that have not been manually set by users with the following code, but its throwing the "Site already has a sensitivity label" message, meaning the if statement isnt triggering when ran from a site in a variable???

# Get the label (required ExchangeOnlineManagement module)
    Connect-IPPSSession -userprincipalname wise@redacted
     
    $c = Get-Credential wise@redacted
     
    Connect-SPOService -Url https://redacted-admin.sharepoint.com -Credential $c -ModernAuth:$true -AuthenticationUrl https://login.microsoftonline.com/organizations

#Create a progress counter and fail counter
$count = 0
$fail = 0

#get all sites
write-host "Collecting site data" -ForegroundColor Yellow
$SiteCollections = Get-SPOSite -Limit All
write-host "Collecting site data - COMPLETED" -ForegroundColor Green

#get a count of total sites
$total = $SiteCollections.count
write-host "There are $total total sites." -ForegroundColor Green
 
foreach ($site in $SiteCollections){
    $count++
    try{
        if ( $site.SensitivityLabel -eq '' ){ 
            Set-SPOSite $site -SensitivityLabel:'{GUID}'
            } else {
                $label = $site.SensitivityLabel 
                Write-host "Site already has a sensitivity label: $label"
                $site.Url | Out-File '.\Sites_already_labeled.txt' -Append
            }
    } catch {
    $sitename = $site.Name
    "Bad site: $sitename" | Out-File '.\Failed_change_sites.txt' -Append
    $fail++
    Write-Host "Failed site count = $fail" -ForegroundColor Blue
    Write-Host "Failed site = $sitename" -ForegroundColor Blue
    }
    Write-Host "Processing Site $count of $total" -ForegroundColor Red
}

I have tested the if sens label = '' on singular sites and it does infact fufill the if statement so im completely lost.

Wise
  • 69
  • 7

1 Answers1

0

Thanks ChatGPT!

It's likely that the issue is with the following line of code: if ( $site.SensitivityLabel -eq '' ). The SensitivityLabel property of a SharePoint Online site may not be an empty string when it has no label applied to it. Instead, it may be $null, so the if statement should be updated to if ( $site.SensitivityLabel -eq $null ).

Wise
  • 69
  • 7