PowerShell 7.2.12 是否破坏了 Connect-MgGraph 与 AccessToken 的关系?

问题描述 投票:0回答:2

自从将我们的构建代理从 PowerShell 7.2.11 升级到 7.2.12 以来,他们一直报告以下错误:

Cannot bind parameter 'AccessToken'. Cannot convert the *** value of type "System.String" to type "System.Security.SecureString".

我们的脚本如下:

param(
    [Parameter(Mandatory)]
    [string]$graphApiToken
)

Connect-MgGraph -AccessToken $graphApiToken

这之前是有效的,回滚到我们之前的构建代理映像已经解决了该问题。

powershell microsoft-graph-api azure-powershell
2个回答
11
投票

正如评论中提到的,这是 Microsoft Graph PowerShell 模块 v1.0 和 v2.0 之间行为的变化。

如果您希望脚本保持与 v1.0 的兼容性,只需有条件地转换访问令牌值即可:

param(
    [Parameter(Mandatory)]
    [string]$graphApiToken
)

$targetParameter = (Get-Command Connect-MgGraph).Parameters['AccessToken']

if ($targetParameter.ParameterType -eq [securestring]){
  Connect-MgGraph -AccessToken ($graphApiToken |ConvertTo-SecureString -AsPlainText -Force)
}
else {
  Connect-MgGraph -AccessToken $graphApiToken
}

0
投票

如何将其转换为适用于 7.2.12?

Function MSGraph {
    $tokenBody = @{
            Grant_Type = "client_credentials"
            Scope = "https://graph.microsoft.com/.default"
            client_Id = $clientID
            Client_Secret = $Clientsecret
    }

    $ConnectGraph = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token" -Method POST -Body $tokenBody
    $token = $ConnectGraph.access_token

    Connect-MgGraph -AccessToken $token

}
© www.soinside.com 2019 - 2024. All rights reserved.