I'm trying to set up licences on Office365 users with powershell for a future migration.
To do this, I've firstly put a list (with a powershell script) of existing users/licences in a CSV file. It looks like this:
Users,Licence
[email protected],"MYCOMPANY:LICENCEXXX","MYCOMPANY:LICENCEVISIO"
[email protected],"MYCOMPANY:LICENCEXXX","MYCOMPANY:LICENCEWORD"
[email protected],"MYCOMPANY:LICENCEXXX","MYCOMPANY:LICENCEEXCEL"
....
(For information, I'm sure about the licence name because I get them with the command Get-MsolAccountSku
)
For the future of the migration, I'm currently doing another script to set up the licence automatically on existing users, by taking information on the Licence column of the CSV file
I do it like that:
$csv = Import-Csv C:\output.csv
$toto = $csv[1].Licence
Set-MsolUserLicense -UserPrincipalName [email protected] -AddLicenses $toto
And here is my problem, when I launch this script I've got this error:
Set-MsolUserLicense : Unable to assign this license because it is invalid. Use the Get-MsolAccountSku cmdlet to retrieve a list of valid licenses.
Au caractère Ligne:1 : 1
Set-MsolUserLicense -UserPrincipalName [email protected] -AddLicense ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : OperationStopped: (:) [Set-MsolUserLicense], MicrosoftOnlineException
FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.InvalidUserLicenseException,Microsoft.Online.Administration.Automation.SetUserLicense
It looks like the licence doesnt exist, but if I do the command manually, it work.
Also, if I put the variable $toto like that in the script, it work:
$toto = "MYCOMPANY:LICENCEXXX","MYCOMPANY:LICENCEWORD"
I don't understand why it work when I do it manually, and if I extract it from a CSV file it don't work.
Hope you can help me, Thanks
CodePudding user response:
Let's assume the following code executes as desired:
$toto = "MYCOMPANY:LICENCEXXX","MYCOMPANY:LICENCEWORD"
Set-MsolUserLicense -UserPrincipalName [email protected] -AddLicenses $toto
$toto
is an array of two strings. Each string is a license name. See below for its output:
$toto
Output:
MYCOMPANY:LICENCEXXX
MYCOMPANY:LICENCEWORD
In order for -AddLicenses
to be passed an array of two license names per CSV row, your CSV sample must be tweaked. This is because your header row has two comma-separated values. Each of your subsequent data rows have three comma-separated values. So you need to either add a third header or combine the second and third data fields (the two license names) into one field. Import-Csv
expects perfect alignment of header count to data field count on each row.
We will attempt combining the licenses into one field and note the subtle difference in the quoting as we are quoting the entire field rather than each individual license. This process is called qualifying the text.
$csv = @'
Users,Licence
toto@mycompany.com,"MYCOMPANY:LICENCEXXX,MYCOMPANY:LICENCEVISIO"
titi@mycompany.com,"MYCOMPANY:LICENCEXXX,MYCOMPANY:LICENCEWORD"
tata@mycompany.com,"MYCOMPANY:LICENCEXXX,MYCOMPANY:LICENCEEXCEL"
'@
$csv | ConvertFrom-Csv | Foreach-Object {
Set-MsolUserLicense -UserPrincipalName $_.Users -AddLicenses $_.Licence.Split(',')
}
$_
is the current object (row in this case) processed by Foreach-Object
. $_.Licence.Split(',')
creates an array of two license strings by splitting the licence data at the ,
character. Note that the use of ConvertFrom-Csv
here is simply because I am processing CSV data as a string rather than from a file. If reading from a file, you would simply use Import-Csv file.csv | Foreach-Object {
.