Function powershell

106 Views Asked by At

So I created a function that assigns licenses to users. When I execute the function it shows me an error.

Function Create_resource_account ($UserPrincipalName, $ApplicationId, $DisplayName) {
try {
    New-CsOnlineApplicationInstance -UserPrincipalName $UserPrincipalName -ApplicationId $ApplicationId -DisplayName $DisplayName
}    
catch {
    write-Log(“Error while creating a new application instance for a Call queue with $UserPrincipalName”)
    $ErrorMessage = $_.Exception.Message
    $FailedItem = $_.Exception.ItemName
    Write-Log($ErrorMessage)
    Write-Log($FailedItem)
    # exit(0)
}
}

Function AddLicense {
[CmdletBinding()]
param (
    [Parameter(Mandatory = $true, Position = 0)]
    [string] $UserPrincipalName,
    [Parameter(Mandatory = $true, Position = 1)]
    [string] $License,
    [Parameter(Mandatory = $true, Position = 2)]
    [string] $UsageLocation
)
process {
    $LicensedUser = (Get-MsolUser -UserPrincipalName $UserPrincipalName).licenses.AccountSkuId
    if ( $LicensedUser -eq $License) {
        Write-Output ("No licence to assign for $UserPrincipalName")
    }
    else {
        try {
            Get-MsolUser -UserPrincipalName $UserPrincipalName -UsageLocation $UsageLocation 
            Set-MsolUser -UserPrincipalName $UserPrincipalName -AddLicenses $License
        }
        catch {
            write-Log(“Error while assigning PhoneSystem Virtual Licence to $UserPrincipalName”)
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName
            Write-Log($ErrorMessage)
            Write-Log($FailedItem)
            exit(0)
        }
    }
}
}
$Util2 = Get-PnPListItem -List "2"
foreach ($temp2 in $Util2) {

        $CQNom = $temp2['CQ_nom']
        $CQMail = $temp2['CQ_Compte_de_ressource']
        $CQNum = $temp2['Tel_sda']    
  

    #Création des ressources
    $newCqRa = Create_resource_account $CQMail 11cd3e2e-fccb-42ad-ad00-878b93575e07 "RA_$CQNom"   #Call queue


    #Assign licence
    $User = Get-AzureADUser -objectid $newCqRa.ObjectId | select -ExpandProperty UserPrincipalName
    AddLicense $User "domain:PHONESYSTEM_VIRTUALUSER" "US"}

the error

get-MsolUser : User Not Found.  User: [email protected].
Au caractère Ligne:31 : 22
+ ... nsedUser = (get-MsolUser -UserPrincipalName $UserPrincipalName).licen ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OperationStopped: (:) [Get-MsolUser], MicrosoftOnlineException
+ FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.UserNotFoundException,Microsoft.Online.Administration.Automation.GetUser

It shows me the same error for all users. I told myself that there is a problem in my function Addlicense. Someone can help me please

I store what my program does in a logging file and this is what it writes to me: "Error while assigning PhoneSystem Virtual Licence to [email protected] The parameter set cannot be resolved using the specified named parameters.

Error while assigning PhoneSystem Virtual Licence to [email protected] The parameter set cannot be resolved using the specified named parameters."

1

There are 1 best solutions below

0
Meghana-MSFT On BEST ANSWER

We tried this at our end and we were able to get users and assign licenses as well. One quick thing to consider is that the command Set-MsolUser should be used only to update basic properties. To update the user license please use Set-MsolUserLicense.

 Set-MsolUserLicense -UserPrincipalName [email protected] -AddLicenses domain:MCOPSTN_5