Runbook to apply license to users won't work in Power Automate, but works in testing and Powershell

ServIT Administrator 1 Reputation point
2022-03-07T21:00:32.337+00:00

Summary:

I am working on a Power Automate Flow that creates a user from a SharePoint form and applies a Microsoft License to the user. The flow creates the user from the form correctly but will not apply a license to that user. The runbook seems to work as designed when testing, as I have seen it apply a license from the testing screen in Runbooks. I have also been able to copy and paste each line of code from the runbook into PowerShell and that correctly applied the license. For some reason, the flow just wont work, even when the flow shows no errors. Please help.

Code:

Param (
[string] $Employee = ""
)
$credObject = Get-AutomationPSCredential -Name "ScriptServiceAccount"
Connect-AzureAD -Credential $credObject
$User = Get-AzureADUser -ObjectId $Employee
Set-AzureADUser -ObjectId $User.ObjectId -UsageLocation US
$StandardLicense = Get-AzureADSubscribedSku | Where {$_.SkuId -eq "4b9405b0-7788-4568-add1-99614e613b69"}
$License = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$License.SkuId = $StandardLicense.SkuId
$LicensesToAssign = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
$LicensesToAssign.AddLicenses = $License
Set-AzureADUserLicense -ObjectId $User.ObjectId -AssignedLicenses $LicensesToAssign

Errors:

Get-AzureADUser : Error occurred while executing GetUser Code: Request_ResourceNotFound Message: Resource 'email@keyman .net' does not exist or one of its queried reference-property objects are not present. RequestId: c4ecf8e4-b895-4a19-b294-74cbd34e18ee DateTimeStamp: Mon, 07 Mar 2022 20:13:40 GMT HttpStatusCode: NotFound HttpStatusDescription: Not Found HttpResponseStatus: Completed At line:6 char:10 + $User = Get-AzureADUser -ObjectId $Employee + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Get-AzureADUser], ApiException + FullyQualifiedErrorId : Microsoft.Open.AzureAD16.Client.ApiException,Microsoft.Open.AzureAD16.PowerShell.GetUser

Set-AzureADUser : Cannot bind argument to parameter 'ObjectId' because it is null. At line:7 char:28 + Set-AzureADUser -ObjectId $User.ObjectId -UsageLocation US + ~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Set-AzureADUser], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Open.AzureAD16.PowerShell.SetUser

Set-AzureADUserLicense : Cannot bind argument to parameter 'ObjectId' because it is null. At line:13 char:35 + Set-AzureADUserLicense -ObjectId $User.ObjectId -AssignedLicenses ... + ~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Set-AzureADUserLicense], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Open.AzureAD16.PowerShell.SetUserLicenses

Flow:

180783-image.png

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
37,794 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2022-03-07T22:02:31.753+00:00

    Power automate is not currently supported here on Q&A. The product group for Power automate actively monitors questions in dedicated forums here.
    https://powerusers.microsoft.com/t5/Microsoft-Power-Automate/ct-p/MPACommunity

    --please don't forget to upvote and Accept as answer if the reply is helpful--