Wednesday, November 27, 2013

Auto add specific Office 365 licenses to all users

Ever needed to only add Exchange Online licenses to thousands of users in Office 365? Well i did. Since i didn't want to click i created a powershell script for it.

I have created this particular script for an academic company. Therefore the plan and the service options are specifically for an educational company.

This scripts add the users based on the attribute "Office". If this attribute is empty it's an employee, if the attributed is not empty it's a student. The scripts saves the output to a logfile and also to the display (including a progressbar :)).

If you want to know what plans are available in your tenant, you can type in the below commands in a powershell session:
Import-Module MSOnline
Connect-MsolService -Credential (Get-Credential)

Once you have the subscriptions you can narrow it down to only the services available in the subscription by using the below command:
Get-MsolSubscription -SubscriptionId  | select-object -ExpandProperty servicestatus

Here is the script. Have fun with it!
Import-Module MSOnline
Connect-MsolService -Credential (Get-Credential)
$GlobalLog = "D:\Scripts\Logs\$(gc env:computername)_$(get-date -format hhmm_ddMMyyyy).log"
$Date =  get-date -Format "hh:mm:ss - dd MMMM yyyy"
$Users = Get-MsolUser -All
$Location = "NL"

#Faculty members
$PlanFc = New-MsolLicenseOptions -AccountSkuId $Faculty -DisabledPlans $LicOptions 

$PlanSt = New-MsolLicenseOptions -AccountSkuId $Student -DisabledPlans $LicOptions

function LogLine {
 Add-content $GlobalLog -value $LogInput

function LogHeader {
 LogLine "--------------------------------------------------------------------------------"
 LogLine "Adding user licenses:"
 LogLine "Time: $Date"
 LogLine "--------------------------------------------------------------------------------"

function LicAdd {
$Users | ForEach-Object -Begin {Clear-Host;$i=0;$Stu=0;$Fac=0} -Process `
 if ($_.IsLicensed -ne "TRUE")
 Set-MsolUser -UserPrincipalName $_.Userprincipalname -UsageLocation $Location
   if ($_.Office -ne $null) 
       $User = $_.UserPrincipalName
    Set-MsolUserLicense -UserPrincipalName $User -AddLicenses $Student -LicenseOptions $PlanSt
    LogLine "Added student license for user $User"
    Write-Host "Added student license for user" $User -ForegroundColor 'Gray'
    #Start-Sleep -Milliseconds 30
    $User = $_.UserPrincipalName
    Set-MsolUserLicense -UserPrincipalName $User -AddLicenses $Faculty -LicenseOptions $PlanFc
    LogLine "Added faculty license for user $User"
    Write-Host "Added faculty license for user" $User -ForegroundColor 'Green'
    #Start-Sleep -Milliseconds 30
 write-progress -activity "Adding licenses" -status "Progress:" -percentcomplete ($i/$users.count*100)
 $i = $i+1
} `
-end {}
LogLine "--------------------------------------------------------------------------------"
LogLine "Number licenses added for students: $Stu"
LogLine "Number licenses added for faculty employees: $Fac"
LogLine "--------------------------------------------------------------------------------"
LogLine ""


No comments:

Post a Comment