Jump to content

Recommended Posts

Posted (edited)

Hi All,

 

Having some trouble with a script I found on the net and modifying it to work for my own office 365 tenancy. I found this: [sOLVED]Can I assign a license to a group of users by PowerShell? | Manage Office 365 | Microsoft Office 365 Community which basically gives me this code:

 

I connect to my tenancy using the 'connect-msolservice' command

Use the 'Get-MsolAccountSku' to find out my license sku

Place a csv with all my users UPN in the c:\

 

Set the 4 variables

 

$AccountSkuId = "tenancy:OFFICESUBSCRIPTION"

$UsageLocation = "UK"

$LicenseOptions = New-MsolLicenseOptions -AccountSkuId $AccountSkuId

$Users = Import-Csv c:\students.csv

 

Then run this script

 

$Users | ForEach-Object {

Set-MsolUser -UserPrincipalName $_.UserPrincipalName -UsageLocation $UsageLocation

Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -AddLicenses $AccountSkuId

}

 

I just keep getting this error:

+ CategoryInfo : OperationStopped: (:) [set-MsolUser], MicrosoftO

nlineException

+ FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.Requi

redPropertyNotSetException,Microsoft.Online.Administration.Automation.SetU

ser

 

Set-MsolUserLicense : Cannot bind argument to parameter 'UserPrincipalName'

because it is null.

At line:3 char:40

+ Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -AddLicenses

$Accoun ...

+ ~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : InvalidData: (:) [set-MsolUserLicense], Paramete

rBindingValidationException

+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,M

icrosoft.Online.Administration.Automation.SetUserLicense

 

Set-MsolUser : You must provide a required property: Parameter name: ObjectId

At line:2 char:1

+ Set-MsolUser -UserPrincipalName $_.UserPrincipalName -UsageLocation

$UsageLocati ...

 

I don't think I'm understanding the $_. command correctly, using that I think is suppose to be pulling the UPN from my csv? Its saying in the error that 'UserPrincipalName' because it is null!!!

 

Can anyone see where I'm going wrong? I only tbh understand certain parts of powershell and still learning

Thanks

FB

Edited by FatBoy
Posted

365 is pushing us into powershell overload too! The script we use (which I must admit I can't remember if I found it here or on Technet) is thus:

 

#Set the license options for Office 365, which in this case is just Exchange access
$LicOptions = New-MsolLicenseOptions -AccountSkuId schoolname:STANDARDWOFFPACK_STUDENT -DisabledPlans MCOSTANDARD, SHAREPOINTWAC_EDU, SHAREPOINTSTANDARD_EDU
#Search for users that have no licences as they have just been syncronised and store into array where username begins STU
$users = Get-MsolUser -UnlicensedUsersOnly -Synchronized -MaxResults 5000 | Where-Object {$_.department -eq "Student" }
#If we have no users then we can exit out of the script
if (!($users)) {exit;}

#For each user we found, we will loop thought them and apply settings
Write-Host "Set Licence"
foreach ($user in $users)
{
#Displays on screen the user we are updaing purely for debugging purpose
Write-Host $user.UserPrincipalName
#Set the Location to GB for the user which resolves the "error" that is detected
Set-MsolUser -UserPrincipalName $user.UserPrincipalName -UsageLocation GB
#Set the Office 365 license for the user based on the value selected above. This is purely for students
Set-MsolUserLicense -UserPrincipalName $user.UserPrincipalName -AddLicenses "schoolname:STANDARDWOFFPACK_STUDENT" -LicenseOptions $LicOptions
}

 

That works on existing users which are unlicensed, and only those that have "Student" in the Department AD field.

  • Thanks 1
Posted (edited)

$_ is a shorthand for "this" in the foreach-object loop, i.e. $_.word means $this.word, or "the value in column word in this row I'm looking at right now" - so if your CSV has the columns username, upn, email, the variables $_.username, $_.upn and $_.email will refer to those values for each record as it is processed.

 

Looking at the error - Cannot bind argument to parameter 'UserPrincipalName' because it is null - the script isn't getting a value for $_.UserPrincipalName, so the parameter -UserPrincipalName is null, so the script is aborting. If your script was

Set-MsolUser -UserPrincipalName $_.pineapple -UsageLocation $UsageLocation

it would return the same error as it is at the moment, because it's complaining about the parameter -UserPrincipalName as a result of the variable $_.pineapple being empty. If mention of pineapples confuses you, ignore me - just trying to clarify as in the original script the parameter and the variable have the same name, which makes it a little ambiguous.

 

To fix it: amend the column header in your CSV to UserPrincipalName from upn, or amend the script to use your column header instead of $_.UserPrincipalName - I'm guessing you called that column upn, so use the variable $_.upn

Edited by sonofsanta
  • Thanks 1
Posted
synaesthesia thanks for the script, sonofsanta thanks for explaining the $_ that helps a lot and explains why my script isn't working I'll pick this up monday morning to knackered to bother now lol. Time for the weekend beers :0)
Posted

Hi guys... almost there when I run the 'Get-MsolAccountSku' command below the AccountSkuId I get 'schoolname:OFFICESUBSCRIP...' but it doesn't complete the line because there isn't enough room I have tried both schoolname:OFFICESUBSCRIP... and schoolname:OFFICESUBSCRIPTION but they both error in the script say invalid license how the hell do I get the full name of the license? Any ideas?

Thanks

FB

  • 2 weeks later...
Posted
Hi Sonofsanta, I have the same issue again with some staff licenses, all I can see is schoolname:ENTERPRISEPACK... without the end part of the license name. Would you happen to know what this was? In my powershell window there just isn't enough room from all the characters!
Posted
The standard set of licences - Outlook, Sharepoint etc. - are schoolname:STANDARDWOFFPACK_FACULTY for staff and schoolname:STANDARDWOFFPACK_STUDENT for students.
  • Thanks 1
Posted (edited)

Plus I have so many more options and I don't even know what some of the options are!!! :( I guess I have to state which options I want turned on?

 

But mine starts with ENTERPRISEPACK so it can't be STANDARDWOFFPACK can it?

365license.png

365dos.png

Edited by FatBoy
Posted
Ah, you're A3, we're A2, that's why you're ENTERPRISE and I'm STANDARDWOFFPACK :) by default it assigns all the options, and I've been happy to leave it at that. Others disable certain options, which you can do with another flag that I can't recall off the top of my head - there are other posts round abouts here that will mention it though.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...