Microsoft Graph 2.0.0更新致Azure DevOps发布管道异常求助
问题:Microsoft Graph 2.0.0导致Azure DevOps发布管道异常
自2023年7月12日起,Microsoft Graph PowerShell模块2.0.0版本更新后,Azure DevOps发布管道执行以下PowerShell脚本时出现异常:
$context = Get-AzContext $graphToken = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account, $context.Environment, $context.Tenant.Id.ToString(), $null, [Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never, $null, "https://graph.microsoft.com").AccessToken $secureAccessToken = $graphToken | ConvertTo-SecureString -AsPlainText -Force Connect-MgGraph -AccessToken $secureAccessToken Get-InstalledModule Microsoft.Graph $UserReadAllId = Find-MgGraphPermission User.Read.All -ExactMatch -PermissionType Application | Select-Object -ExpandProperty Id write-host $UserReadAllId
触发错误如下:
Error record: Invoke-MgGraphRequest : Method not found: 'Void Microsoft.Graph.Authentication.AzureIdentityAccessTokenProvider..ctor(Azure.Core.TokenCredential, System.String[], Microsoft.Kiota.Authentication.Azure.ObservabilityOptions, System.String[])'. At C:\Program Files\WindowsPowerShell\Modules\Microsoft.Graph.Authentication\2.0.0\custom\common\Permissions.ps1:37 char:27 + ... estResult = Invoke-MgGraphRequest -method GET -OutputType PSObject $_ ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Invoke-MgGraphRequest], MissingMethodException + FullyQualifiedErrorId : NotSpecified,Microsoft.Graph.PowerShell.Authentication.Cmdlets.InvokeMgGraphRequest Script stack trace: at Permissions_GetPermissionsData, C:\Program Files\WindowsPowerShell\Modules\Microsoft.Graph.Authentication\2.0.0\custom\common\Permissions.ps1: line 37 at Find-MgGraphPermission<Begin>, C:\Program Files\WindowsPowerShell\Modules\Microsoft.Graph.Authentication\2.0.0\custom\Find-MgGraphPermission.ps1: line 202
已尝试明确安装1.27.0版本,但问题仍未解决,想确认是否有其他用户遇到同类问题,以及如何在Azure DevOps发布管道中正常连接MgGraph。
解决方案
- 清理模块残留并重新安装指定版本:Azure DevOps代理可能存在多版本模块冲突,先彻底卸载所有Microsoft Graph相关模块,再安装稳定版本:
Uninstall-Module Microsoft.Graph -AllVersions -Force -ErrorAction SilentlyContinue Uninstall-Module Microsoft.Graph.Authentication -AllVersions -Force -ErrorAction SilentlyContinue Install-Module Microsoft.Graph -RequiredVersion 1.27.0 -Force -AllowClobber
- 显式导入指定版本模块:在脚本开头明确导入所需版本,避免自动加载最新版本:
Import-Module Microsoft.Graph.Authentication -RequiredVersion 1.27.0
检查PowerShell版本兼容性:Microsoft Graph 2.0.0要求PowerShell 7.2及以上版本,若使用Windows PowerShell 5.1可能存在依赖缺失问题,建议将Azure DevOps任务切换为PowerShell 7。
改用服务主体认证方式:避免直接使用AccessToken连接,改用服务主体认证更稳定可靠:
$tenantId = "你的租户ID" $clientId = "你的服务主体ID" $clientSecret = ConvertTo-SecureString "你的服务主体密钥" -AsPlainText -Force $credential = New-Object System.Management.Automation.PSCredential($clientId, $clientSecret) Connect-MgGraph -TenantId $tenantId -ClientSecretCredential $credential
内容的提问来源于stack exchange,提问作者Shahid Sayyed
相关产品推荐
相关产品推荐

