Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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 Azure DevOps生成管道-无法访问脚本中的$(Build.BuildNumber)_Powershell_Azure Devops_Azure Pipelines - Fatal编程技术网

Powershell Azure DevOps生成管道-无法访问脚本中的$(Build.BuildNumber)

Powershell Azure DevOps生成管道-无法访问脚本中的$(Build.BuildNumber),powershell,azure-devops,azure-pipelines,Powershell,Azure Devops,Azure Pipelines,在我的构建管道中,我正在执行以下操作: 从Git还原 Powershell脚本-检索生成 编号并在…之前将其写入json文件 构建解决方案 归档文件 发布工件 在步骤2中,Powershell脚本非常简单: 定义的环境变量: Name: buildNumber Value: $(Build.BuildNumber) Name: rootPath Value:$(Build.ArtifactStagingDirectory) 代码: 由于某种原因,我的$buildNumber返回空值。$roo

在我的构建管道中,我正在执行以下操作:

  • 从Git还原
  • Powershell脚本-检索生成 编号并在…之前将其写入json文件
  • 构建解决方案
  • 归档文件
  • 发布工件
  • 在步骤2中,Powershell脚本非常简单:

    定义的环境变量:

    Name: buildNumber  Value: $(Build.BuildNumber)
    Name: rootPath Value:$(Build.ArtifactStagingDirectory)
    
    代码:

    由于某种原因,我的$buildNumber返回空值。$rootPath正在运行。 我不能在生成步骤之外访问$(Build.BuildNumber)吗?生成编号格式在管道的选项中定义,在标记生成时可以正常工作,但我无法在powershell脚本中访问它


    有什么想法吗?

    使用
    $env:BUILD\u BUILDNUMBER
    而不是
    $(…)
    符号


    请参阅。

    Ha,它起了作用!!非常感谢。澄清。。。。我删除了上面的环境变量,并在脚本中直接使用了这个符号:$env:BUILD\u BUILDNUMBER。再次感谢!
    $theFile = Get-ChildItem -Path $rootPath -Recurse -Filter "host.json" | Select-Object -First 1
    $propertyName = "BuildNumber"
    
    if($theFile)
    {
        $json = Get-Content "$theFile" | Out-String | ConvertFrom-Json
        if($json.$propertyName)
        { 
            $json.$propertyName = $buildNumber
        }else{    
            Add-Member -InputObject $json -MemberType NoteProperty -Name $propertyName -Value $buildNumber
        }
        $json | ConvertTo-Json -depth 100 | Out-File "$theFile"
    
    }
    else
    {
        Write-Warning "Found no files."
    }