Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joerodgers/f2ac7e91bc55a267ca403fd2fe33da11 to your computer and use it in GitHub Desktop.
Save joerodgers/f2ac7e91bc55a267ca403fd2fe33da11 to your computer and use it in GitHub Desktop.
Sample that will disable the guest sharing option on all SPO sites that currently have no external external users on them.
[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls11 -bor [System.Net.SecurityProtocolType]::Tls12
# start logging
$transcriptFile = if([string]::IsNullOrWhiteSpace($MyInvocation.MyCommand.Name)) { "Set-TeamsChannelStorageQuota.ps1" }else{ $MyInvocation.MyCommand.Name }
$transcriptPath = "$($transcriptFile)_$(Get-Date -Format FileDateTime).log"
Start-Transcript -Path $transcriptPath
# clear error history
$Error.Clear()
# load module
Import-Module -Name SharePointPnPPowerShellOnline -WarningAction SilentlyContinue
function Set-TeamsChannelStorageQuota
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][Microsoft.Online.SharePoint.TenantAdministration.SiteProperties]$SiteProperties,
[Parameter(Mandatory=$false)][int]$StorageMaximumLevel = (1GB/1MB) <# 1GB #>,
[Parameter(Mandatory=$false)][int]$StorageWarningLevel = (500MB/1MB) <# 500MB #>,
[Parameter(Mandatory=$false)][switch]$RetainCurrentStorageMaximumLevelIfHigher,
[Parameter(Mandatory=$false)][switch]$WhatIf
)
begin
{
}
process
{
$NewStorageMaximumLevel = $StorageMaximumLevel
$NewStorageWarningLevel = $StorageWarningLevel
Write-Verbose -Message "$($SiteProperties.Url) - StorageMaximumLevel: $($SiteProperties.StorageMaximumLevel)"
Write-Verbose -Message "$($SiteProperties.Url) - StorageWarningLevel: $($SiteProperties.StorageWarningLevel)"
if( $SiteProperties.StorageMaximumLevel -eq (25TB/1MB) )
{
# channel has default 25TB quota, change it to default or supplied value
}
elseif( $RetainCurrentStorageMaximumLevelIfHigher.IsPresent )
{
$NewStorageMaximumLevel = [System.Math]::Max( $StorageMaximumLevel, $SiteProperties.StorageMaximumLevel )
$NewStorageWarningLevel = [System.Math]::Max( $StorageWarningLevel, $SiteProperties.StorageWarningLevel )
}
if( $WhatIf.IsPresent )
{
if( $SiteProperties.StorageMaximumLevel -ne $NewStorageMaximumLevel -and $SiteProperties.StorageWarningLevel -ne $NewStorageWarningLevel)
{
Write-Output "$($SiteProperties.Url) - Would have changed StorageMaximumLevel from $($SiteProperties.StorageMaximumLevel) to $NewStorageMaximumLevel and StorageWarningLevel from $($SiteProperties.StorageWarningLevel) to $NewStorageWarningLevel"
}
else
{
Write-Output "$($SiteProperties.Url) - No changes required"
}
}
else
{
if( $SiteProperties.StorageMaximumLevel -ne $NewStorageMaximumLevel-and $SiteProperties.StorageWarningLevel -ne $NewStorageWarningLevel)
{
Write-Verbose -Message "$($SiteProperties.Url) - Changing StorageMaximumLevel from $($SiteProperties.StorageMaximumLevel) to $NewStorageMaximumLevel and StorageWarningLevel from $($SiteProperties.StorageWarningLevel) to $NewStorageWarningLevel"
Set-PnPTenantSite -Url $SiteProperties.Url -StorageMaximumLevel $NewStorageMaximumLevel -StorageWarningLevel $NewStorageWarningLevel
}
}
}
end
{
}
}
function Send-ExecutionSummary
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)][string]$SmtpServer,
[Parameter(Mandatory=$true)][string[]]$To,
[Parameter(Mandatory=$true)][string]$From,
[Parameter(Mandatory=$false)][string[]]$Cc,
[Parameter(Mandatory=$true)][string]$TranscriptPath
)
begin
{
$head = "
<style>
h1 {
font-family: Arial, Helvetica, sans-serif;
color: #e68a00;
font-size: 28px;
}
h2 {
font-family: Arial, Helvetica, sans-serif;
color: #000099;
font-size: 16px;
}
table {
font-size: 12px;
border: 0px;
font-family: Arial, Helvetica, sans-serif;
}
td {
padding: 4px;
margin: 0px;
border: 0;
}
th {
background: #395870;
background: linear-gradient(#49708f, #293f50);
color: #fff;
font-size: 11px;
text-transform: uppercase;
padding: 10px 15px;
vertical-align: middle;
}
tbody tr:nth-child(even) {
background: #f0f0f2;
}
</style>"
if( $Error.Count -eq 0 )
{
$result = "SUCCESS"
}
else
{
$result = "FAILURE"
}
$errors = ""
$body = [PSCustomObject] @{
Computer = $env:COMPUTERNAME
Script = $MyInvocation.MyCommand.Name
Result = $result
Transcript = $TranscriptPath
} | ConvertTo-Html -PreContent "<h2>Execution Summary</h2>" -As Table
$params = @{
SmtpServer = $SmtpServer
To = $To
From = $From
Cc = $Cc
Subject = "$($env:COMPUTERNAME) - $($MyInvocation.MyCommand.Name) - $result"
Body = $body
BodyAsHtml = $true
Priority = "Normal"
}
}
process
{
if( $Error.Count -gt 0 )
{
$errors = ($Error | ConvertTo-Html -As Table -Fragment -PreContent "<h2>Summary of $($Error.Count) Exceptions</h2>") -join "`n"
}
$params["Body"] = ConvertTo-Html -Body "$body $errors" -Head $head -As List
#$params["Body"] | Set-Content -Path "C:\_temp\debug.html"
Send-MailMessage @params
}
end
{
}
}
# requires Azure AD App Principals > SharePoint > Sites.FullControl.All permissions
$tenant = "contoso"
$clientId = "8a6b10a8-1234-1234-1234-9b8e49b6f6b7"
$certificatePath = "E:\_certs\AppPrincipalCert.pfx"
$certificatePassword = ConvertTo-SecureString -String 'pass@word1' -AsPlainText -Force
# connect to the tenant
Connect-PnPOnline -Url "https://$tenant-admin.sharepoint.com" -ClientId $clientId -CertificatePath $certificatePath -CertificatePassword $certificatePassword -Tenant "$tenant.onmicrosoft.com" -WarningAction SilentlyContinue
# get all channels with StorageMaximumLevel > 1GB
$channels = Get-PnPTenantSite -Template "TEAMCHANNEL#0" | ? StorageMaximumLevel -gt 1GB
# process each channel
$channels | Set-TeamsChannelStorageQuota -Verbose -RetainCurrentStorageMaximumLevelIfHigher -WhatIf
# send email summary
Send-ExecutionSummary -SmtpServer "mail.contoso.com" -To "joe.smith@contoso.com" -From "automation@contoso.com" -TranscriptPath $transcriptPath
# stop logging
Stop-Transcript
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment