none
Need help in shaping the Office 365 license assigning Script. RRS feed

  • Question

  • I am using the following script for assigning licesne for new office 365 users. While doing this bulk process some of the users are already assigend license manuaaly due to some urgent purpose, they will be getting error saying - Already a plan is active for the user.(One more point is - We only assign Lync & Exchange license for all users + We have ENTERPRISEPACK n ENTERPRISEPACKWITHOUTPROPLUS, this script i am trying is for ENTERPRISEPACK

    I posted it in o365 community and the script details in below

    https://community.office365.com/en-us/f/148/t/407775


    Manju Gowda

    Friday, September 11, 2015 8:54 AM

Answers

  • Hi Manju,

    if you want to handle errors in PowerShell, you need to use the try/catch statements. Here an Example:

    try
    {
        Remove-Item "C:\temp" -ErrorAction "Stop"
    }
    catch
    {
        Write-Warning "Failed to delete item: $($_.Exception.Message)"
    }


     

    Notes:

    • ErrorAction "Stop" means the catch will trigger even on minor errors.
    • While within the catch block, $_ represents that red stuff you usually see. $_.Exception.Message is the easily readable text of the exception. You can use this (with if/elseif/else statements) to differentiate between different kinds of exceptions or use it to pass along the readable part of the error.
      Note: There's a better way to differentiate between Exception types, but I won't go into full error handling here.

    Cheers and good luck with your script,
    Fred


    There's no place like 127.0.0.1

    Friday, September 11, 2015 10:11 AM
  • Start with this and manage the errors as you discover them.

    $cred = Get-Credential 
    Connect-MsolService -Credential $cred
    
    Write-Host 'Getting License Information' -Fore green
    $AccountSkuId='domain:ENTERPRISEPACK'
    $LicenseOptions = New-MsolLicenseOptions -AccountSkuId $AccountSkuId -DisabledPlans RMS_S_ENTERPRISE,OFFICESUBSCRIPTION,YAMMER_ENTERPRISE,SHAREPOINTWAC,SHAREPOINTENTERPRISE
    
    Write-Host 'Getting List of users to assign license' -Fore GREEN
    Import-Csv C:\Scripts\L\WithPro-Final-U.csv |
    	ForEach-Object{
            	Try{
        			Set-MsolUser -UserPrincipalName $_.UserPrincipalName -UsageLocation $_.UsageLocation -ea Stop
    			Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -AddLicenses $AccountSkuId -LicenseOptions $LicenseOptions -ea Stop
    			Get-MsolUser -UserPrincipalName $_.UserPrincipalName -ea Stop
    		}
    		Catch{
    			"$_"
    		}
    	} |
    	Select-Object userprincipalname,islicensed,overallprovisioningstatus,usagelocation


    \_(ツ)_/


    Friday, September 11, 2015 12:19 PM
    Moderator

All replies

  • Hi Manju,

    if you want to handle errors in PowerShell, you need to use the try/catch statements. Here an Example:

    try
    {
        Remove-Item "C:\temp" -ErrorAction "Stop"
    }
    catch
    {
        Write-Warning "Failed to delete item: $($_.Exception.Message)"
    }


     

    Notes:

    • ErrorAction "Stop" means the catch will trigger even on minor errors.
    • While within the catch block, $_ represents that red stuff you usually see. $_.Exception.Message is the easily readable text of the exception. You can use this (with if/elseif/else statements) to differentiate between different kinds of exceptions or use it to pass along the readable part of the error.
      Note: There's a better way to differentiate between Exception types, but I won't go into full error handling here.

    Cheers and good luck with your script,
    Fred


    There's no place like 127.0.0.1

    Friday, September 11, 2015 10:11 AM
  • Start with this and manage the errors as you discover them.

    $cred = Get-Credential 
    Connect-MsolService -Credential $cred
    
    Write-Host 'Getting License Information' -Fore green
    $AccountSkuId='domain:ENTERPRISEPACK'
    $LicenseOptions = New-MsolLicenseOptions -AccountSkuId $AccountSkuId -DisabledPlans RMS_S_ENTERPRISE,OFFICESUBSCRIPTION,YAMMER_ENTERPRISE,SHAREPOINTWAC,SHAREPOINTENTERPRISE
    
    Write-Host 'Getting List of users to assign license' -Fore GREEN
    Import-Csv C:\Scripts\L\WithPro-Final-U.csv |
    	ForEach-Object{
            	Try{
        			Set-MsolUser -UserPrincipalName $_.UserPrincipalName -UsageLocation $_.UsageLocation -ea Stop
    			Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -AddLicenses $AccountSkuId -LicenseOptions $LicenseOptions -ea Stop
    			Get-MsolUser -UserPrincipalName $_.UserPrincipalName -ea Stop
    		}
    		Catch{
    			"$_"
    		}
    	} |
    	Select-Object userprincipalname,islicensed,overallprovisioningstatus,usagelocation


    \_(ツ)_/


    Friday, September 11, 2015 12:19 PM
    Moderator