powershell - 如何在Azure DevOps PowerShell中编写 “System.Collections.Hashtable”变量

标签 powershell azure-devops hashtable

我有一个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/

相关文章:

multithreading - 引用计数资源的线程安全映射

Python - 找不到 get-pip.py

powershell - 如何从 Notepad++ 执行 PowerShell 脚本

build - 如何在 VSTS/VSO Build vNext 中创建 Web 部署包?

java - 在 XSLT 中检索 HashMap 值

java - Hashtable 和 Collections.synchronizedMap(HashMap) 的区别

powershell - 如何将PowerShell版本从2.0升级到3.0

c# - 从 C# 错误调用的 PowerShell cmdlet Install-WindowsFeature 未在命令集合中列出

c# - .NET 4.7 xunit 测试项目在引用 .NET Standard 1.6 项目时无法构建

azure - Azure Devops Pipeline 的开发工作流程是怎样的?