Azure:New-AzRoleAssignment 输入字符串格式不正确 - 双重解析错误...没有任何数字

Azure: New-AzRoleAssignment Input string was not in a correct format - error with double parsing... without any number

我的 PowerShell 脚本在创建 Azure 用户帐户时遇到了一个奇怪的问题。

正在执行命令

New-AzRoleAssignment -RoleDefinitionName 'Contributor' -ObjectId "$Usr.Id" -ResourceGroupName "$rgname"

异常结果:

New-AzRoleAssignment : Input string was not in a correct format.
At myscript.ps1:151 char:5
+     New-AzRoleAssignment -RoleDefinitionName 'Contributor' -ObjectId  ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [New-AzRoleAssignment], FormatException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Resources.NewAzureRoleAssignmentCommand

我已经提取了这个异常的堆栈跟踪,它看起来像这样:

at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)
at Microsoft.Azure.Commands.Resources.NewAzureRoleAssignmentCommand.ExecuteCmdlet()
at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord()

这太奇怪了 - 我在这里没有使用任何双数。我已经深入研究了 NewAzureRoleAssignmentCommand 的源代码 - 但那里没有 System.Number.ParseDouble 调用。

唯一与 double 相关的是 -ConditionVersion,但我不想使用任何 Coditions。

接下来我应该检查什么?我不是高级 Azure/Powershell 用户,欢迎提出任何建议!

编辑: 我已经尝试使用相同的参数值手动 运行 命令,带引号和不带引号 - 每次我得到相同的错误

New-AzRoleAssignment -RoleDefinitionName 'Contributor' -ObjectId f01b4003-8a47-4a9d-8935-cd000c8cd9c5 -ResourceGroupName myValidResourceGrupName

去掉 $usr.id

两边的双引号

如果您只有这样的字符串:

$usr = <GUID goes here> 

然后你可以传递“$usr”。但在这种情况下,您正在尝试访问 $usr 对象的 ID 属性。

我会尝试让你的 Get-AzRoleDefinition。我倾向于获取所有对象并使用 ID。这对我有用。

$Role = Get-AzRoleDefinition -Name Contributor
$ResourceGroup = Get-AzResourceGroup -Name "ResourceGroupName"
$User = Get-AzADUser -UserPrincipalName "User-UPN-Here"
New-AzRoleAssignment -ObjectId $User.Id -RoleDefinitionId $Role.Id -Scope $ResourceGroup.ResourceId

尝试使用 Azure 云 Shell 检查问题是否出在您的机器上。

我遇到了同样的问题。这个脚本曾经工作:

$qa = get-azadgroup -displayname qa;
New-AzRoleAssignment -ObjectId $qa.Id -RoleDefinitionName Owner -ResourceGroupName RG-QA

开始在一台计算机上出现此错误,但在另一台计算机上没有。当我检查 az.resources 的模块版本时,我有 2.1.0、2.4.0 和 2.5.0。删除版本 2.4.0 和 2.5.0 后,脚本可以正常工作。我将 Powershell 的语言设置为法语。

https://github.com/Azure/azure-powershell/issues/13096 如果其他人遇到这个问题,那就是这个问题。上述解决方法是使用 Az.resources 的 2.1.0,但应尽快修复。