英文:
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论