We help IT Professionals succeed at work.

PowerShell: Office 365:  How to pass multiple license options from user input?

717 Views
Last Modified: 2017-03-16
I am trying to pass the user's input to ...

Set-MsolUserLicense -Verbose -UserPrincipalName $_.UserPrincipalName -AddLicenses $SKU -LicenseOptions $Options

Open in new window


meaning, $Sku and $Options


Unfortunately, the methods don't support op_addition.

Is there any methodology that would work for this?

function Test-SentLicenses {
    [CmdletBinding()]
    Param
    (
        # Users to be licensed
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
        [string[]] $Users,

        [Parameter(Mandatory=$False)]
        [switch]$E3,
 
        [parameter(Mandatory=$False)]
        [switch]$EMS,
 
        [Parameter(Mandatory=$False)]
        [string]$PowerBIPro,
 
        [Parameter(Mandatory=$False)]
        [string]$PowerBIIndividual,
 
        [Parameter(Mandatory=$False)]
        [switch]$PowerBIFree,
 
        [Parameter(Mandatory=$False)]
        [switch]$RMSAdhoc

    )

    Begin {
        # Assign Tenant and Location to a variable
        $Tenant     = "SENT"
        $Location   = "US"
        
        # Assign each AccountSkuID to a variable
        $TenantE3      = ($Tenant + ':ENTERPRISEPREMIUM')
        $TenantEMS     = ($Tenant + ':EMSPREMIUM')
        $TenantProBI   = ($Tenant + ':POWER_BI_PRO')
        $TenantIUserBI = ($Tenant + ':POWER_BI_INDIVIDUAL_USER')
        $TenantFreeBI  = ($Tenant + ':POWER_BI_STANDARD')
        $TenantRMS     = ($Tenant + ':RIGHTSMANAGEMENT_ADHOC')              
        
        # Zero out $Options
        $Options = $null
        

        if($E3.IsPresent){
            $TenantE3  = ($Tenant + ':ENTERPRISEPREMIUM')
            $LicenseE3 = New-MsolLicenseOptions -AccountSkuId $TenantE3
            $Options += $LicenseE3

        }
        if($EMS.IsPresent){
            $TenantEMS  = ($Tenant + ':EMSPREMIUM')  
            $LicenseEMS = New-MsolLicenseOptions -AccountSkuId $TenantEMS
            $Options += $LicenseEMS         
        }
        if($PowerBIPro.IsPresent){
            $LicenseProBI = New-MsolLicenseOptions -AccountSkuId $TenantProBI
            $Options += $LicenseProBI
        }
        if($PowerBIIndividual.IsPresent){
            $LicenseIUserBI = New-MsolLicenseOptions -AccountSkuId $TenantIUserBI
            $Options += $LicenseIUserBI
        }
        if($PowerBIFree.IsPresent){
            $LicenseFreeBI = New-MsolLicenseOptions -AccountSkuId $TenantFreeBI
            $Options += $LicenseFreeBI
        }
        if($RMSAdhoc.IsPresent){
            $LicenseRMS = New-MsolLicenseOptions -AccountSkuId $TenantRMS
            $Options += $LicenseRMS
        }
        
    }
    Process {
        Set-MsolUser -UserPrincipalName $_.UserPrincipalName -UsageLocation $Location
        Set-MsolUserLicense -Verbose -UserPrincipalName $_.UserPrincipalName -AddLicenses $SKU -LicenseOptions $Options
    
    }
    End {
    }
}

Open in new window




Frankly I am not even sure if the parameter [switch] would work.

Thank you
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
K B

Author

Commented:
wow, thank you.. I can't believe how close I was (yet so far away) haha!

Here is a revised and shortened example:

function Test-SentLicenses {
    [CmdletBinding()]
    Param
    (
        # Users to be licensed
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
        [string[]] $Users,

        [Parameter(Mandatory=$False)]
        [switch]$E3,
 
        [parameter(Mandatory=$False)]
        [switch]$EMS

    )

    Begin {
        # Assign Tenant and Location to a variable
        $Tenant     = "SENT"
        $Location   = "US"
        
        # Assign each AccountSkuID to a variable
        $TenantE3      = ($Tenant + ':ENTERPRISEPREMIUM')
        $TenantEMS     = ($Tenant + ':EMSPREMIUM')        
        
        # Assign each License Options to a variable and zero counter
        $Options = @()
        $SKU = @()

        if($E3.IsPresent){
            $TenantE3  = ($Tenant + ':ENTERPRISEPREMIUM')
            $LicenseE3 = New-MsolLicenseOptions -AccountSkuId $TenantE3
            $Options += $LicenseE3
            $SKU += $TenantE3
        }
        if($EMS.IsPresent){
            $TenantEMS  = ($Tenant + ':EMSPREMIUM')  
            $LicenseEMS = New-MsolLicenseOptions -AccountSkuId $TenantEMS
            $Options += $LicenseEMS 
            $SKU += $TenantEMS        
        } 
    }
    Process {
        Set-MsolUser -UserPrincipalName $_.UserPrincipalName -UsageLocation $Location
        Set-MsolUserLicense -Verbose -UserPrincipalName $_.UserPrincipalName -AddLicenses $SKU -LicenseOptions $Options
    }
    End {
    }
}

Open in new window

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.