Is there a way to troubleshoot an Unknown error when trying to use Set-MsolUserLicense in Powershell?

TristanDevOps 20 Reputation points
2023-09-18T15:45:25.3166667+00:00

I have generated this test code to attempt assigning user license assignments to Azure via PowerShell. However, everything functions except the License Assignment. I have debugged every line and narrowed it down to an Unknown error during the communication between PS and the Azure Tennant during the license assignment.

This is the error message I recieve:

Set-MsolUserLicense : Unknown error occurred.

At line:1 char:1

  • Set-MsolUserLicense -UserPrincipalName censoredUPN@UPN* ...
  • 
        + CategoryInfo          : OperationStopped: (:) [Set-MsolUserLicense], MicrosoftOnlineException 
    
        + FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.OperationNotAllowedException,Microsoft.Online.Administration.Aut   
    
       omation.SetUserLicense
    
    

Code Block:

###Variable Declarations left out for OpSec Purposes*** 

Try{
            Connect-MsolService -Credential $Credentials
            Set-MsolUser -UserPrincipalName $upn -UsageLocation US -ErrorAction Stop
            Write-Output "Trying to Assign License $azureLicense to user $upn, Error Action is to stop" # Debugging Write Statement, can be removed once functionality is restored.
            Set-MsolUserLicense -UserPrincipalName $upn -AddLicenses $azureLicense -ErrorAction Stop
            Write-Output "If this Line prints, code moved past the License set attempt"
            [Microsoft.Online.Administration.Automation.ConnectMsolService]::ClearUserSessionState()
        }
        #If assigning license fails, send failed outcome to Okta
        Catch{
            $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
            $headers.Add("Content-Type", "application/json")
            $body = "{
                    `n    `"outcome`":`"failed-license`",
                    `n    `"upn`":`"$upn`"
                    `n}"
            #$failResponse = Invoke-RestMethod $invokeURL -Method 'POST' -Headers $headers -Body $body
            Write-Host "License Assignment Failed: $_"
            $body
            Write-Output "Script attempted to write back to Okta via the Catch Block for License Failure, If no Logging in Okta, this step failed." #Debugging Write Statement, can be removed later.  
        }
Azure
Azure
A cloud computing platform and infrastructure for building, deploying and managing applications and services through a worldwide network of Microsoft-managed datacenters.
1,027 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,537 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
21,921 questions
0 comments No comments
{count} votes

Accepted answer
  1. Vasil Michev 106.7K Reputation points MVP
    2023-09-18T16:16:51.1433333+00:00

    Set-MsolUserLicense is now deprecated mate, you need to switch to using either the Azure AD module, or (recommended) the Graph SDK for PowerShell, both of which leverage the new (and only supported) licensing APIs.

    Here's a sample article with detailed steps: https://learn.microsoft.com/en-us/microsoft-365/enterprise/assign-licenses-to-user-accounts-with-microsoft-365-powershell?view=o365-worldwide

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.