函数powershell

Function powershell

所以我创建了一个为用户分配许可证的功能。当我执行函数时,它显示错误。

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

错误

get-MsolUser : User Not Found.  User: CQ-JeanPierreViseur@domain.onmicrosoft.com.
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

它向我显示了所有用户的相同错误。我告诉自己,我的函数 Addlicense 有问题。有人可以帮助我

我将我的程序所做的存储在一个日志文件中,这是它写给我的内容: “将 PhoneSystem 虚拟许可证分配给 CQ-EricDesbordes@domain.onmicrosoft.com 时出错 无法使用指定的命名参数解析参数集。

将 PhoneSystem 虚拟许可证分配给 CQ-JeanPierreViseur@ 时出错domain.onmicrosoft.com 无法使用指定的命名参数解析参数集。"

我们最后尝试了这个,我们也能够获得用户并分配许可证。一件需要快速考虑的事情是命令 Set-MsolUser should be used only to update basic properties. To update the user license please use Set-MsolUserLicense.

 Set-MsolUserLicense -UserPrincipalName User1@contoso.com -AddLicenses domain:MCOPSTN_5