自从将我们的构建代理从 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
这之前是有效的,回滚到我们之前的构建代理映像已经解决了该问题。
正如评论中提到的,这是 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
}
如何将其转换为适用于 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
}