Can't get a script to atribute a license to a batch of user

Start-Transcript
connect-azuread
$cohort = read-host "Enter Cohort tag to be licensed (eg. 4.3_DevOps)"
Get-AzureADUser -Filter "startswith(state,'$cohort')" | ForEach-Object { $azureUser = Get-AzureADUser -ObjectId $_
$License = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$License.SkuId = "f245ecc8-75af-4f8e-b61f-27d8114de5f3"
$LicensesToAssign = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
$LicensesToAssign.AddLicenses = $License
Set-AzureADUserLicense -ObjectId $azureUser.ObjectId -AssignedLicenses $LicensesToAssign
}

Can’t seem to find out how to do this, total noob and looking for help or at least being pointed in the right direction, as it always errors out with:

Get-AzureADUser : Unexpected character encountered while parsing value: <. Path '', line 0, position 0.
At AtributeLicenses.ps1:4 char:87
+ ... ohort')" | ForEach-Object { $azureUser = Get-AzureADUser -ObjectId $_
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-AzureADUser], JsonReaderException
+ FullyQualifiedErrorId : Newtonsoft.Json.JsonReaderException,Microsoft.Open.AzureAD16.PowerShell.GetUser

Set-AzureADUserLicense : Cannot bind argument to parameter 'ObjectId' because it is null.
At AtributeLicenses.ps1:10 char:38
+ Set-AzureADUserLicense -ObjectId $azureUser.ObjectId -AssignedLic ...
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-AzureADUserLicense], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Open.AzureAD16.PowerShell.SetUs
erLicenses

Any ideas?

Remove single quotes from variable. No need to query Azure AD twice.
I dont have an Azure environment to test, but this should work. Link below has examples.

Start-Transcript
connect-azuread
$cohort = read-host "Enter Cohort tag to be licensed (eg. 4.3_DevOps)"
Get-AzureADUser -Filter "startswith(state,$cohort)" | ForEach-Object {
    $azureUser = $_.ObjectId
    $License = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
    $License.SkuId = "f245ecc8-75af-4f8e-b61f-27d8114de5f3"
    $LicensesToAssign = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
    $LicensesToAssign.AddLicenses = $License
    Set-AzureADUserLicense -ObjectId $azureUser -AssignedLicenses $LicensesToAssign
}

Also worth noting that the Set-AzureADUserLicense command will stop working at the end of June 2022 when the Azure AD Graph API is retired. If you’re writing a new script, consider using the Microsoft.Graph SDK and Set-MgUserLicense instead.