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)
Get-MsolSubscription
 


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"
$LicOptions = 'SHAREPOINTWAC_EDU','MCOSTANDARD','SHAREPOINTSTANDARD_EDU'

#Faculty members
$Faculty = "<TenantID>:STANDARDWOFFPACK_FACULTY"
$PlanFc = New-MsolLicenseOptions -AccountSkuId $Faculty -DisabledPlans $LicOptions 

#Students
$Student = "<TenantID>:STANDARDWOFFPACK_STUDENT"
$PlanSt = New-MsolLicenseOptions -AccountSkuId $Student -DisabledPlans $LicOptions

function LogLine {
 Param(
 [string]$LogInput
 )
 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
     $Stu++
   }
   else 
   {  
    $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
     $Fac++
   }
 }
 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 ""
}

LogLine
LogHeader
LicAdd

No comments:

Post a Comment