# encoding: ascii
# api: powershell
# title: O365 Licensing Module
# description: This is a module I create to make getting Office 365 account info a bit easier. 
# version: 0.1
# type: script
# author: BoardWithLife
# license: CC0
# function: Set-MailLicense
# x-poshcode-id: 6147
# x-derived-from-id: 6148
# x-archived: 2016-05-23T22:39:44
# x-published: 2016-12-22T02:43:00
#
# Prerequisites:
# http://go.microsoft.com/fwlink/?LinkID=286152
# http://go.microsoft.com/fwlink/p/?linkid=236297
# After installing the two packages above, run this command to connect to the Microsoft Online service:
# Connect-MsolService -Credential (Get-Credential EMAIL@COMPANY.COM)
# Edit the values for -UserPricipalName and -AddLicenses\-RemoveLicenses parameters in the script to match your domain.
#
<#
.Synopsis
   Tool for O365 Mailbox Licensing
.DESCRIPTION
   This function is used to add and/or remove COMPANY Licenses for users. The -UserName parameter will accept multiple usernames
   passed through the pipeline. Set-MailLicense will also accept a "UserName" property sent through the pipeline. When licensing a user for the first time, the location parameter is mandatory. 
.EXAMPLE
   Set-MailLicense -Username Test.User -Add -Location US
   <This will add the default Enterprise License to a users account and set their UsageLocation to US. To see a full
   list of all available locations, type the -Location Parameter at the prompt and hit space to see the drop down list.>
.EXAMPLE
   Set-MailLicense -Username Test.User -Remove
   <This will remove the COMPANY license from the desired user.>
.Notes
    
#>
Function Set-MailLicense{
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0)]
        [string[]]$UserName,
        [Parameter(ParameterSetName='Remove')]
        [switch]$Remove,
        [Parameter(ParameterSetName='Add')]
        [switch]$Add,
        [Parameter(Mandatory=$True,ParameterSetName='add')]
        [validateset('US','CA','CN','GB','HK','FI','AE')]
        [string]$Location
    )
    Begin{
        $OldEA = $ErrorActionPreference
        $ErrorActionPreference = "SilentlyContinue"
    }
    Process{
            If($Remove){
                Set-MsolUserLicense -UserPrincipalName "$UserName@domain.com" -RemoveLicenses "COMPANY365:ENTERPRISEPACK"
            }
                Else{
                    Set-MsolUser -UserPrincipalName "$UserName@domain.com" -UsageLocation $Location 
                    Set-MsolUserLicense -UserPrincipalName "$UserName@domain.com" -AddLicenses "COMPANY365:ENTERPRISEPACK"
                }
            Get-MailLicense -UserName $UserName
    }
    End{
        $ErrorActionPreference = $OldEA
    }
}

<#
.Synopsis
   This Function retrieves a COMPANY users mailbox status
.DESCRIPTION
   This function is used to find a COMPANY users mailbox license status and type. 
.EXAMPLE
   PS C:\Scripts> Get-MailLicense test.user


    UserName    : Test.User
    Office      : San Francisco
    WhenCreated : 12/21/2015 18:40:35
    Licenses    : {COMPANY365:ENTERPRISEPACK}
    IsLicensed  : True

   <This shows the Office 365 account info provided by a small selection of the Get-MSOLUser Cmdlet output.>
.EXAMPLE
   Set-MailLicense -Username Test.User -Remove
   <This will remove the HOK license from the desired user.>
#>
Function Get-MailLicense {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory=$true,Position=0)]
        [string[]]$UserName
    )
    Process {
        Get-MsolUser -UserPrincipalName "$UserName@hok.com" | Select @{E={$_.DisplayName.Replace(" ",".")};L='UserName'},Office,WhenCreated,Licenses,isLicensed
    }
}
