PowerShell 7.2.12是否破坏了Connect-MgGraph与AccessToken的连接?

huangapple go评论62阅读模式
英文:

Has PowerShell 7.2.12 broken Connect-MgGraph with AccessToken?

问题

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

无法绑定参数'AccessToken'无法将类型为System.String的***值转换为类型System.Security.SecureString”。

我们的脚本如下:

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

Connect-MgGraph -AccessToken $graphApiToken

这在之前是可以正常工作的,回滚到之前的构建代理镜像已解决了这个问题。

英文:

Since upgrading our build agents from PowerShell 7.2.11 to 7.2.12, they have been reporting the following error:

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

Our script is as follows:

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

Connect-MgGraph -AccessToken $graphApiToken

This was working previously, and rolling back to our previous build agent image has resolved the issue.

答案1

得分: 9

根据评论中提到的,这是 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
}
英文:

As mentioned in the comments, this is a change in behavior between v1.0 and v2.0 of the Microsoft Graph PowerShell module.

If you want your scripts to maintain compatibility with v1.0, simply convert the access token value conditionally:

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
}

huangapple
  • 本文由 发表于 2023年7月10日 20:41:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定