Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试将管道秘密变量传递到Powershell脚本时出错_Powershell_Azure Devops_Azure Pipelines - Fatal编程技术网

尝试将管道秘密变量传递到Powershell脚本时出错

尝试将管道秘密变量传递到Powershell脚本时出错,powershell,azure-devops,azure-pipelines,Powershell,Azure Devops,Azure Pipelines,我正在尝试将一个秘密变量作为Azure管道的一部分传递到Powershell脚本中。我通过进入“编辑管道”添加变量,然后单击变量按钮,添加变量并选中“保留此值机密”复选框。变量名为PAT 我引用Powershell脚本中的变量,它是一个单独的文件,如下所示: $url = '[Azure DevOps url]' <# pass PAT as pipeline variable #> $Token = "$env:PATSECRET" if ($PAT -eq

我正在尝试将一个秘密变量作为Azure管道的一部分传递到Powershell脚本中。我通过进入“编辑管道”添加变量,然后单击变量按钮,添加变量并选中“保留此值机密”复选框。变量名为PAT

我引用Powershell脚本中的变量,它是一个单独的文件,如下所示:

$url = '[Azure DevOps url]'
<# pass PAT as pipeline variable #>
$Token = "$env:PATSECRET"

if ($PAT -eq "") {
    exit 1
}
$AzureAuthHeader = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $Token)))

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", ("Basic {0}" -f $AzureAuthHeader))
$headers.Add("Content-Type", "application/json")

$response = Invoke-RestMethod -Uri $url -Method GET -Headers $headers

$lastcommit = $response.value[1].commitId

$packageFolder = git diff HEAD $lastcommit --name-only
- powershell: echo $env:MYSECRET
  env:
    MySecret: $(Foo)
管道运行时,我收到以下错误:
##[error]env:PAT:术语“env:PAT”无法识别为cmdlet、函数、脚本文件或可操作程序的名称…

更新: 我使用环境变量映射更新了Powershell构建步骤。我还是会犯同样的错误。我还删除了
$(env:PAT)
周围的括号,没有任何更改。

根据Microsoft的说法,“秘密变量不会自动映射。如果您有一个名为Foo的秘密变量,您可以这样映射它”:


您可以使用脚本的环境或将变量映射到variables块中,以向管道传递机密。这是我的样品:

1.脚本的环境(推荐方式):

测试1.ps1:

$token= "$env:MY_PAT"
if ($token -eq "") {
    exit 1
}
 
$url= "https://dev.azure.com/{Organization}/_apis/wit/wiql?api-version=6.1-preview.2"

$AzureAuthHeader = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $Token)))

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", ("Basic {0}" -f $AzureAuthHeader))
$headers.Add("Content-Type", "application/json")
 
$JSON = @'
{
  "query": "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = 'Task' AND [State] <> 'Closed' AND [State] <> 'Removed' order by [Microsoft.VSTS.Common.Priority] asc, [System.CreatedDate] desc"
}
'@
 
$response = Invoke-RestMethod -Uri $url -Headers $headers -Method Post -Body $JSON -ContentType application/json
Write-Host "result = $($response | ConvertTo-Json -Depth 100)" 

您可以在本文档中找到有关的详细信息。

如果仍然存在问题,请共享您的PowerShell脚本。请确保变量名称相同。我在PowerShell脚本中使用RESTAPI和PAT,效果很好。请核对我答案中的样本。另外,您使用的是哪种代理?请试着用其他的,我能用的。谢谢我的部分问题是在构建步骤中没有正确命名变量。
steps:
- task: PowerShell@2
  displayName: 'Detect Subfolder Changes'
  name: setvarStep
  env:
    MY_PAT: $(PAT)
  inputs:
    filePath: '$(System.DefaultWorkingDirectory)/test.ps1'
    failOnStderr: true
$token= "$env:MY_PAT"
if ($token -eq "") {
    exit 1
}
 
$url= "https://dev.azure.com/{Organization}/_apis/wit/wiql?api-version=6.1-preview.2"

$AzureAuthHeader = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $Token)))

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", ("Basic {0}" -f $AzureAuthHeader))
$headers.Add("Content-Type", "application/json")
 
$JSON = @'
{
  "query": "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = 'Task' AND [State] <> 'Closed' AND [State] <> 'Removed' order by [Microsoft.VSTS.Common.Priority] asc, [System.CreatedDate] desc"
}
'@
 
$response = Invoke-RestMethod -Uri $url -Headers $headers -Method Post -Body $JSON -ContentType application/json
Write-Host "result = $($response | ConvertTo-Json -Depth 100)" 
variables:
  MY_PAT: $(PAT)
steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      Write-Host "PAT: $(MY_PAT)"