0

What I'm looking to do here is assign specific licenses to specific accounts, that is pulled from a csv file. I have some scripts where I can bulk assign but only one license type at a time. What I'm looking to have is more granular control and bulk assign multiple license types off a csv that is something like this:

upn license type
testuser1@testdomain.com Microsoft 365 E3
testuser2@testdomain.com Microsoft 365 E5
testuser3@testdomain.com Microsoft 365 E3
testuser4@testdomain.com Microsoft 365 F3

upn (userprincipalname) and license type are the column headers).Where each specified user is assigned that specific license in it's row.

So as I understand, I can do:

Get-MsolAccountSku

Which will give me all my existing license types, I can take note of the SkuId for the specific license I need, lets say for example it's testtenant:SPE_E3 , I can then do something like:

users = import-csv ".\bulkassignlicenses.csv"
foreach ($user in $users)
{
    $upn=$user.UserPrincipalName
    $SKU= "testtenant:SPE_E3"
    Set-MsolUser -UserPrincipalName $upn 
    Set-MsolUserLicense -UserPrincipalName $upn -AddLicenses $SKU
    Write-Host "License Assigned to UPN:"$upn 
} 

Issue is this will only assign one type of license to the listed users in the csv file and I can't figure out how to make it iterate through a CSV and assign different types of licenses.

Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
coderific
  • 33
  • 1
  • 9
  • 1
    Certainly possible, what part of your code are you having trouble with? – Santiago Squarzon Jul 02 '21 at 02:37
  • So as I understand, once run Connect-MsolService and authenticate onto my tenant. I should run Get-MsolAccountSku and take note of the AccountSkuId for the specific license I need, lets say for example it's testtenant:SPE_E3 , I can then do something like: users = import-csv ".\bulkassignlicenses.csv" foreach ($user in $users) { $upn=$user.UserPrincipalName $SKU= "testtenant:SPE_E3" Set-MsolUser -UserPrincipalName $upn Set-MsolUserLicense -UserPrincipalName $upn -AddLicenses $SKU Write-Host "License Assigned to UPN:"$upn } } – coderific Jul 02 '21 at 02:51
  • But this would only assign one specific license, my issue is I can't figure out how to make it iterate through a CSV and assign different licenses. (Sorry for the formatting of the code ^) – coderific Jul 02 '21 at 02:52
  • Redid the question now, should look better. – coderific Jul 02 '21 at 03:17

1 Answers1

0

There are different ways you could do this, I'll show you 2 different ones. I personally think that Group-Object is the more elegant solution in this case.

I'm assuming that you already know all your available licenses and those licenses are already hardcoded (with no typos) on the CSV.

Given the following array as example:

PS /> $users

upn                           license        
---                           -------        
user.example01@testdomain.com ExampleLicense3
user.example02@testdomain.com ExampleLicense1
user.example01@testdomain.com ExampleLicense1
user.example02@testdomain.com ExampleLicense1
user.example03@testdomain.com ExampleLicense3
user.example01@testdomain.com ExampleLicense3
user.example02@testdomain.com ExampleLicense2
user.example01@testdomain.com ExampleLicense3
user.example03@testdomain.com ExampleLicense1
user.example03@testdomain.com ExampleLicense1
user.example03@testdomain.com ExampleLicense1

For reference, this is how the array looks after grouping it:

Count Name                      Group                                                                                                                                     
----- ----                      -----                                                                                                                                     
    4 user.example01@testdom... {@{upn=user.example01@testdomain.com; license=ExampleLicense3}, @{upn=...
    3 user.example02@testdom... {@{upn=user.example02@testdomain.com; license=ExampleLicense1}, @{upn=...
    4 user.example03@testdom... {@{upn=user.example03@testdomain.com; license=ExampleLicense3}, @{upn=...

Code:

$users | Group-Object upn | ForEach-Object {
    # Note: $_.Group.License may or may not be an array.
    # Since -AddLicenses can take string[] as input this shouldn't be a problem
    Set-MsolUserLicense -UserPrincipalName $_.Name -AddLicenses $_.Group.License
}

Given the following array as example:

PS /> $users

upn                           license                                                        
---                           -------                                                        
user.example02@testdomain.com ExampleLicense2;ExampleLicense1;ExampleLicense1                
user.example01@testdomain.com ExampleLicense3;ExampleLicense1;ExampleLicense2;ExampleLicense1
user.example03@testdomain.com ExampleLicense3;ExampleLicense2;ExampleLicense3;ExampleLicense2

Note: I'm using a semicolon ; as delimiter however you should decide which is the proper delimiter for this example.

Code:

$users | ForEach-Object {
    $licenses = $_.license -split ';'
    Set-MsolUserLicense -UserPrincipalName $_.upn -AddLicenses $licenses
}
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37