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.
}