posts - 104 , comments - 115 , trackbacks - 0

My Links

News

Disclaimer: Right here... Github: Code all the things! PS GUI Series: < Coming Soon! > PS Scripts: < Coming Soon! >

Tag Cloud

Article Categories

Archives

Post Categories

Blogs

Forums

Lync

Storage

Tools

Virtualization

Web comics

O365 Selective licensing script

I finally digged-in and finished this script. It runs based on a CSV file allowing you to specify what user gets what modality enabled. I should probably start a git to manage my scripts better, but that'll have to wait until after my move...

CSV formatting:
UserPrincipalName,DisplayName,UsageLocation,Exchange,SharePoint,Lync,Office,Sway,Intune,Yammer,RMS
user@upn.onmicrosoft.com,DNofUser,US,TRUE,,,TRUE,TRUE,TRUE,TRUE,TRUE

Powershell script:

# By: Marc Dekeyser
# Blog: http://www.geekswithblogs.net/marcde
# Always, Always run this in a test bed first. If you choose to run this script in production you do so at your own risk and peril!


# Begin Functions
function write-log([string]$info)
{
# verify the Log is setup and if not create the file
if($script:loginitialized -eq $false)
{
$FileHeader > $logfile
$script:loginitialized = $True
}
$info = $(get-date).ToString()+": "+$info
$info >> $script:logfile
Write-host $info -ForegroundColor Green
}

# END Functions

# Prepping variables
$accountsku = Get-MsolAccountSku
$script:LostUsers = @()
$script:logfile = ".\Logs\Selective-Licensing-$(get-date -format MMddyyHHmmss).log"
$script:Seperator = $("-" * 25)
$script:loginitialized = $false
$script:FileHeader = "***Application Information***"
$accountsku = Get-MsolAccountSku

# Setup the UI Colors
$host.ui.RawUI.ForegroundColor = "White"
$host.ui.RawUI.BackgroundColor = "Black"

# Verifying the MSOnline powershell module is installed
If (!([System.IO.File]::Exists(("{0}\modules\msonline\Microsoft.Online.Administration.Automation.PSModule.dll" -f $pshome))))
{
Write-EventLog -LogName Application -EntryType Error -EventId 99 -Source O365LicenseUpdate -Message "The Microsoft Online Services Module for PowerShell is not installed. The Script cannot continue."
write-log "Please download and install the Microsoft Online Services Module."
Exit 99
}

# Connecting to Azure AD
#Import-Module MSOnline
#$msolcred = get-credential
#connect-msolservice -credential $msolcred

# Import CSV File
$UsersToLicense = import-csv C:\Selective-licensing\msolusers.csv

# Getting account SKU incase the user is not licensed.
$accountsku = Get-MsolAccountSku

Foreach($user in $UsersToLicense){
   
    # Informational
    $txtUser = $user.UserPrincipalName
    write-log "Processing $txtUser"

    # Checking if the user exists in O365
    $userobject = Get-MsolUser -UserPrincipalName $user.Userprincipalname -ErrorAction Stop

    # Set usage location if it hasn't been set
    if (!$userObject.UsageLocation) {
        Set-MsolUser -UserPrincipalName $UPN -UsageLocation $UsageLocation
    }

    # Mapping the options from the CSV to an Array for usage in the msollicenseoptions
    $DisabledOptions = @()
    If($user.Exchange -eq "TRUE"){$DisabledOptions += "EXCHANGE_S_ENTERPRISE"; write-log "Disabling Exchange."}
    If($user.Sharepoint -eq "TRUE"){$DisabledOptions += "SHAREPOINTENTERPRISE";$DisabledOptions += "SHAREPOINTWAC"; write-log "Disabling Sharepoint."}
    If($user.Lync -eq "TRUE"){$DisabledOptions += "MCOSTANDARD"; write-log "Disabling Lync."}
    If($user.Office -eq "TRUE"){$DisabledOptions += "OFFICESUBSCRIPTION"; write-log "Disabling Office Subscription."}
    If($user.SWAY -eq "TRUE"){$DisabledOptions += "SWAY"; write-log "Disabling Sway."}
    If($user.Intune -eq "TRUE"){$DisabledOptions += "INTUNE_O365"; write-log "Disabling Intune."}
    If($user.Yammer -eq "TRUE"){$DisabledOptions += "YAMMER_ENTERPRISE"; write-log "Disabling Yammer."}
    If($user.RMS -eq "TRUE"){$DisabledOptions += "RMS_S_ENTERPRISE"; write-log "Disabling RMS."}

    # Creating the License Options Object
    $licenseOptions = New-MsolLicenseOptions -AccountSkuId $accountsku.AccountSkuId -DisabledPlans $DisabledOptions

    if($userobject.IsLicensed -Eq "True"){
    # User is already Licensed  
        Set-MsolUserLicense -UserPrincipalName $user.UserPrincipalName -LicenseOptions $licenseOptions
        write-host ""
   
    } Else {
    # User is not licensed just yet
        Set-MsolUserLicense -UserPrincipalName $user.UserPrincipalName -AddLicenses $accountsku.AccountSkuID -LicenseOptions $licenseOptions
        Write-host ""
    }
}












































































}    }        Write-host ""        Set-MsolUserLicense -UserPrincipalName $user.UserPrincipalName -AddLicenses $accountsku.AccountSkuID -LicenseOptions $licenseOptions    # User is not licensed just yet    } Else {            write-host ""        Set-MsolUserLicense -UserPrincipalName $user.UserPrincipalName -LicenseOptions $licenseOptions    # User is already Licensed       if($userobject.IsLicensed -Eq "True"){    $licenseOptions = New-MsolLicenseOptions -AccountSkuId $accountsku.AccountSkuId -DisabledPlans $DisabledOptions    # Creating the License Options Object    If($user.RMS -eq "TRUE"){$DisabledOptions += "RMS_S_ENTERPRISE"; write-log "Disabling RMS."}    If($user.Yammer -eq "TRUE"){$DisabledOptions += "YAMMER_ENTERPRISE"; write-log "Disabling Yammer."}    If($user.Intune -eq "TRUE"){$DisabledOptions += "INTUNE_O365"; write-log "Disabling Intune."}    If($user.SWAY -eq "TRUE"){$DisabledOptions += "SWAY"; write-log "Disabling Sway."}    If($user.Office -eq "TRUE"){$DisabledOptions += "OFFICESUBSCRIPTION"; write-log "Disabling Office Subscription."}    If($user.Lync -eq "TRUE"){$DisabledOptions += "MCOSTANDARD"; write-log "Disabling Lync."}    If($user.Sharepoint -eq "TRUE"){$DisabledOptions += "SHAREPOINTENTERPRISE";$DisabledOptions += "SHAREPOINTWAC"; write-log "Disabling Sharepoint."}    If($user.Exchange -eq "TRUE"){$DisabledOptions += "EXCHANGE_S_ENTERPRISE"; write-log "Disabling Exchange."}    $DisabledOptions = @()    # Mapping the options from the CSV to an Array for usage in the msollicenseoptions    }         Set-MsolUser -UserPrincipalName $UPN -UsageLocation $UsageLocation     if (!$userObject.UsageLocation) {     # Set usage location if it hasn't been set    $userobject = Get-MsolUser -UserPrincipalName $user.Userprincipalname -ErrorAction Stop    # Checking if the user exists in O365    write-log "Processing $txtUser"    $txtUser = $user.UserPrincipalName    # Informational    Foreach($user in $UsersToLicense){$accountsku = Get-MsolAccountSku# Getting account SKU incase the user is not licensed.$UsersToLicense = import-csv C:\Selective-licensing\msolusers.csv# Import CSV File#connect-msolservice -credential $msolcred#$msolcred = get-credential#Import-Module MSOnline# Connecting to Azure AD} Exit 99 write-log "Please download and install the Microsoft Online Services Module." Write-EventLog -LogName Application -EntryType Error -EventId 99 -Source O365LicenseUpdate -Message "The Microsoft Online Services Module for PowerShell is not installed. The Script cannot continue." { If (!([System.IO.File]::Exists(("{0}\modules\msonline\Microsoft.Online.Administration.Automation.PSModule.dll" -f $pshome)))) # Verifying the MSOnline powershell module is installed$host.ui.RawUI.BackgroundColor = "Black" $host.ui.RawUI.ForegroundColor = "White" # Setup the UI Colors $accountsku = Get-MsolAccountSku$script:FileHeader = "***Application Information***" $script:loginitialized = $false $script:Seperator = $("-" * 25) $script:logfile = ".\Logs\Selective-Licensing-$(get-date -format MMddyyHHmmss).log" $script:LostUsers = @()$accountsku = Get-MsolAccountSku# Prepping variables# END Functions} Write-host $info -ForegroundColor Green$info >> $script:logfile $info = $(get-date).ToString()+": "+$info } $script:loginitialized = $True $FileHeader > $logfile { if($script:loginitialized -eq $false) # verify the Log is setup and if not create the file { function write-log([string]$info) # Begin Functions# Always, Always run this in a test bed first. If you choose to run this script in production you do so at your own risk and peril!# Blog: http://www.geekswithblogs.net/marcde# By: Marc Dekeyser

Print | posted on Tuesday, March 22, 2016 6:40 PM | Filed Under [ Powershell O365 scripting! ]

Feedback

No comments posted yet.
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 

Powered by: