+ Post New Thread
Results 1 to 6 of 6
Scripts Thread, Assigning licenses in Office 365 in Coding and Web Development; Hi All, Having some trouble with a script I found on the net and modifying it to work for my ...
  1. #1
    FatBoy's Avatar
    Join Date
    Oct 2007
    Location
    Kent, UK
    Posts
    246
    Thank Post
    54
    Thanked 20 Times in 16 Posts
    Rep Power
    17

    Angry Assigning licenses in Office 365

    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.Admini stration.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.SetUserL icense

    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
    Last edited by FatBoy; 11th July 2014 at 03:34 PM.

  2. #2

    synaesthesia's Avatar
    Join Date
    Jan 2009
    Location
    Northamptonshire
    Posts
    5,871
    Thank Post
    574
    Thanked 998 Times in 770 Posts
    Blog Entries
    15
    Rep Power
    461
    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:

    Code:
    #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.

  3. Thanks to synaesthesia from:

    FatBoy (11th July 2014)

  4. #3

    sonofsanta's Avatar
    Join Date
    Dec 2009
    Location
    Lincolnshire, UK
    Posts
    4,934
    Thank Post
    861
    Thanked 1,438 Times in 988 Posts
    Blog Entries
    47
    Rep Power
    616
    $_ 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
    Last edited by sonofsanta; 11th July 2014 at 03:55 PM.

  5. Thanks to sonofsanta from:

    FatBoy (11th July 2014)

  6. #4
    FatBoy's Avatar
    Join Date
    Oct 2007
    Location
    Kent, UK
    Posts
    246
    Thank Post
    54
    Thanked 20 Times in 16 Posts
    Rep Power
    17
    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)

  7. #5
    FatBoy's Avatar
    Join Date
    Oct 2007
    Location
    Kent, UK
    Posts
    246
    Thank Post
    54
    Thanked 20 Times in 16 Posts
    Rep Power
    17
    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

  8. #6

    sonofsanta's Avatar
    Join Date
    Dec 2009
    Location
    Lincolnshire, UK
    Posts
    4,934
    Thank Post
    861
    Thanked 1,438 Times in 988 Posts
    Blog Entries
    47
    Rep Power
    616
    Don't know why it's not showing for you, but you want schoolname:OFFICESUBSCRIPTION_STUDENT

SHARE:
+ Post New Thread

Similar Threads

  1. Removing ABP for All Users (Group) in Office 365
    By mattpant in forum Cloud Services
    Replies: 0
    Last Post: 3rd September 2013, 01:02 PM
  2. PSA: Address book policies in Office 365 :)
    By jamesbmarshall in forum Cloud Services
    Replies: 4
    Last Post: 23rd July 2013, 09:28 PM
  3. Replies: 3
    Last Post: 3rd July 2013, 03:32 PM
  4. Greyed out Outlook, Calendar and People in Office 365
    By Mr_Jiminy in forum Cloud Services
    Replies: 5
    Last Post: 7th May 2013, 10:39 AM
  5. Shibboleth Support in Office 365 for education
    By jamesbmarshall in forum Cloud Services
    Replies: 0
    Last Post: 3rd July 2012, 09:37 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •