Azure PowerShell脚本报错:Identity参数为空字符串问题排查
PowerShell脚本分配Teams通话代理权限问题排查
初始脚本
我编写了一段PowerShell脚本,用于登录Azure并根据用户输入的邮箱地址为用户分配组通话成员权限,脚本内容如下:
Function SetUser { $script:user = Read-Host -Prompt "Enter the user's email address" Write-Host "You set the email address as '$user'." -ForegroundColor yellow } Function Assigndelegate($user) { $job = Start-Job -ScriptBlock { param($user) New-CsUserCallingDelegate -Identity $identity -Delegate $user -MakeCalls $true -ReceiveCalls $true -ManageSettings $false } -ArgumentList $user while ($job.State -eq 'Running') { Write-Host "Connecting to Azure..." Wait-Job -Job $job } $result = Receive-Job $job (Get-CsUserCallingSettings -Identity $user).Delegators | ft -Property Id, MakeCalls, ReceiveCalls, ManageSettings } Connect-MicrosoftTeams $script:identity = "user@contoso.com" SetUser Assigndelegate $user
初始运行错误
运行该脚本时出现以下错误:
Cannot bind argument to parameter 'Identity' because it is an empty string. + CategoryInfo : InvalidData: (:) [New-CsUserCallingDelegate], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,New-CsUserCallingDelegate + PSComputerName : localhost
我清楚脚本无法读取$identity变量,但不清楚具体原因:尝试使用$global:前缀(尽管后续该变量会变化,这种方式并不理想),仍出现相同错误;直接写入变量值则可正常运行。
添加参数后的错误
当我在param和ArgumentList中定义Identity参数后,错误变为:
Cannot bind argument to parameter 'Identity' because it is an empty string. + CategoryInfo : InvalidData: (:) [New-CsUserCallingDelegate], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,New-CsUserCallingDelegate + PSComputerName : localhost Get-CsUserCallingSettings : Cannot process argument transformation on parameter 'Identity'. Cannot convert value to type System.String. At line:36 char:42 + (Get-CsUserCallingSettings -Identity $user).Delegators | ft -Prop ... + ~~~~~ + CategoryInfo : InvalidData: (:) [Get-CsUserCallingSettings], ParameterBindingArgumentTransformationException + FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-CsUserCallingSettings
更新后的脚本及问题
修改参数列表后,我仍遇到错误,更新后的脚本如下:
#Set the user Function SetUser { $script:user = Read-Host -Prompt "Enter the user's email address" Write-Host "You set the email address as '$user'." -ForegroundColor yellow } Function Assigndelegate($user, $identity) { $job = Start-Job -ScriptBlock { param($user, $identity) New-CsUserCallingDelegate -Identity $identity -Delegate $user -MakeCalls $true -ReceiveCalls $true -ManageSettings $false } -ArgumentList @($user, $identity) while ($job.State -eq 'Running') { Write-Host "Connecting to Azure..." Wait-Job -Job $job } $result = Receive-Job $job #(Get-CsUserCallingSettings -Identity $user).Delegators | ft -Property Id, MakeCalls, ReceiveCalls, ManageSettings } Connect-MicrosoftTeams $script:identity = "user@contoso.com" SetUser Assigndelegate $user, $identity
简化测试后的问题
根据建议简化脚本后:
Function SetUser { $script:user = Read-Host -Prompt "Enter the user's email address" Write-Host "You set the email address as '$user'." -ForegroundColor yellow } Function Assigndelegate($user, $identity) { Write-Output $identity } $script:identity = "user@contoso.com" SetUser Assigndelegate $user, $identity
运行后除了提示You set the email address as '$user'." -ForegroundColor yellow外无其他输出。
内容的提问来源于stack exchange,提问作者PSrookie
相关产品推荐
相关产品推荐

