我有一个System.Collections.Hashtable
类型的变量,我想将此值写入Powershell脚本中的Azure DevOps变量中,并且需要在以下任务中使用该变量。
在Azure DevOps中创建的变量:header
任务1
Connect-PowerBIServiceAccount -ServicePrincipal -Credential $credential -TenantId $env:tenant_id
$head = $null
$head = @{}
$head = Get-PowerBIAccessToken
Write-Host ("##vso[task.setvariable variable=headers]$head")
任务2
Write-Host "Header is " $env:headers
Invoke-RestMethod -Headers $env:headers -Uri 'https://api.powerbi.com/v1.0/myorg/groups'
但是任务2中的问题是
Header is System.Collections.Hashtable
Invoke-RestMethod : Cannot bind parameter 'Headers'. Cannot convert the "System.Collections.Hashtable" value of type "System.String" to type "System.Collections.IDictionary".
由于 header 的值只是分配
System.Collections.Hashtable
的字符串而不是实际值
最佳答案
如果在同一任务中调用Invoke-RestMethod,则可以避免将 token 写入Azure DevOps变量的复杂性。
Connect-PowerBIServiceAccount -ServicePrincipal -Credential $credential -TenantId $env:tenant_id
$head = $null
$head = @{}
$head = Get-PowerBIAccessToken
Invoke-RestMethod -Headers $head -Uri 'https://api.powerbi.com/v1.0/myorg/groups'
关于powershell - 如何在Azure DevOps PowerShell中编写 “System.Collections.Hashtable”变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57543426/