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: