Set Teams policies from powershell script, based on license


I had a client that was a school not so long ago and they needed to change the policies based on the user license; if it was a student then set X or faculty then set Y.

To help them out, I pieced together this crappy but functional script:

# Please make sure you've installed this:
# http://www.microsoft.com/en-us/download/details.aspx?id=35588
# Also install the Teams Module

# Set your some VARS
$SharePointUrl = "https://<YOURDOMAIN>-admin.sharepoint.com"
$MSDomain = "<YOURDOMAIN>.onmicrosoft.com"

# Get Creds
$cred = Get-Credential -Message "Please enter your credentials to use for Office 365 Admin tasks"
clear

# Connect to MSOnline using MSOL
Write-Host "==============================================================" -foregroundcolor Cyan 
Write-Host "Checking for Connection to MSOL" -foregroundcolor yellow
Write-Host "==============================================================" -foregroundcolor Cyan 

Get-MsolDomain -ErrorAction SilentlyContinue | out-null

if($?)
{
    Write-Host "Connected" -ForegroundColor Green
}
else
{
    Write-Host "Connecting" -ForegroundColor Yellow
    Connect-MsolService -Credential $cred
    Connect-MicrosoftTeams -Credential $cred

    Get-MsolDomain -ErrorAction SilentlyContinue | out-null
    if($?)
    {
        Write-Host "Connected" -ForegroundColor Green
        }
    else
    {
        Write-Host "Failed to connect, aborting script" -ForegroundColor Red
        exit
        }

}

Function Connect-S4B {
        # Connect to SkypeForBusiness because MS is slightly backward at the moment
        Write-Host "==============================================================" -foregroundcolor Cyan 
        Write-Host "Checking for Connection to S4B" -foregroundcolor yellow
        Write-Host "==============================================================" -foregroundcolor Cyan 
        Write-Host "Connecting" -ForegroundColor Yellow
        # Connect to Skype

        Import-Module SkypeOnlineConnector
        # If the $SfBsession is empty then ask for creds...
        Get-PSSession | Remove-PSSession
        $SfBsession = New-CsOnlineSession -Credential $cred -Verbose -OverrideAdminDomain $MSDomain
        # Check PSSession
        Import-PSSession -Session $SfBsession
        #Fix Online Session TimeOut
        Enable-CsOnlineSessionForReconnection

        Get-CsOnlineDirectoryTenant -ErrorAction SilentlyContinue | out-null
        if($?)
        {
            Write-Host "Connected" -ForegroundColor Green
            }
        else
        {
            Write-Host "Connection Failed!" -ForegroundColor Red
            exit
        }
}

Connect-S4B

## Make it fresh
clear

## Get the list of licenses and select one
$licenses = Get-MsolAccountSku
$menu = @()
$menu = @{}

Write-Host "==============================================================" -foregroundcolor Cyan 
Write-Host "Your available licenses - for reference" -foregroundcolor yellow
Write-Host "==============================================================" -foregroundcolor Cyan 

foreach ($license in $licenses) {
    $UnusedUnits = $license.ActiveUnits - $license.ConsumedUnits
    Write-Host "$($license.SkuPartNumber) has $UnusedUnits available" 
    $i++}
write-host ""

## Let's pick the users with a type of license that we're going to affect...
Write-Host "==============================================================" -foregroundcolor Cyan 
Write-Host "Pick a License type to assign a policy to" -foregroundcolor yellow
Write-Host "==============================================================" -foregroundcolor Cyan 

write-host "Please select a license type that we will apply a policy to:"
write-host ""
for ($i=1;$i -le $licenses.count; $i++)
{ Write-Host "$i. $($licenses[$i-1].AccountSkuID)"
$menu.Add($i,($licenses[$i-1].AccountSkuID))}

[int]$LicenseMenu = Read-Host 'Enter selection'
$LicenseSelection = $menu.Item($LicenseMenu) 
Write-Host "License Selected: " $LicenseSelection -ForegroundColor Red


## Now let's pick the policy we're going to apply
Write-Host "==============================================================" -foregroundcolor Cyan 
Write-Host "Pick a policy to assign to everyone with that license" -foregroundcolor yellow
Write-Host "==============================================================" -foregroundcolor Cyan 
Write-Host ""

$licenses = Get-CsPolicyPackage
$menu = @()
$menu = @{}
write-host $menu

for ($i=1;$i -le $licenses.count; $i++)
{ Write-Host "$i. $($licenses[$i-1].Name)"
$menu.Add($i,($licenses[$i-1].Name))}

[int]$LicenseMenu = Read-Host 'Enter selection'
$PolicySelection = $menu.Item($LicenseMenu) 
Write-Host "Policy Selected: " $PolicySelection -ForegroundColor Red

## Quick warning before doing the work
Write-Host "==============================================================" -foregroundcolor Cyan 
Write-Host "What we're about to do!" -foregroundcolor yellow
Write-Host "==============================================================" -foregroundcolor Cyan 
Write-Host ""
Write-Host "You are going to pick all users with Licence type $LicenseSelection and apply Policy $PolicySelection"
Write-Host ""
Write-Host "This will apply to the Teams Meeting and Messaging policies."

## Do they really want to build this snowman?
While ($Continue -notmatch '^(Y|N)$'){
$Continue = 
   Read-Host "Do you REALLY want to do this? (y/n)?"
}

$ALLUsers = Get-MsolUser -All | ?{ $_.isLicensed -eq "TRUE" }
$ALLUsersApply = $ALLUsers | ?{ ($_.Licenses | ?{ $_.AccountSkuId -eq $LicenseSelection}).Length -gt 0} | select SignInName
$counttotal = $AllUsersApply.count
Write-Host "Applying to" $counttotal
$count=1

foreach ($User in $ALLUsersApply) {
    echo "Applying to $count of $counttotal"
    write-host Grant-CsUserPolicyPackage -PackageName $PolicySelection -Identity $User.SignInName
    Grant-CsUserPolicyPackage -PackageName $PolicySelection -Identity $User.SignInName
    $count++
    if ( $count -ilike "?0" -or $count -ilike "??0" -or $count -ilike "???0" ) {
        Connect-S4B
    }
}

Write-Host "==============================================================" -foregroundcolor Cyan 
Write-Host "We are done!" -foregroundcolor yellow
Write-Host "==============================================================" -foregroundcolor Cyan 
write-host ""
write-host "Press any key to exit"
read-host " " $exit
exit

Leave a comment