简体   繁体   English

如何通过 PowerShell 脚本正确设置变量,然后使用 Azure DevOps 在另一个脚本中使用它?

[英]How can I properly set a variable through a PowerShell script and then consume it in another using Azure DevOps?

I have a first inline PowerShell script where I am filtering my test project directory and setting its value to a variable called testProjectPath .我有一个第一个内联 PowerShell 脚本,我在其中过滤我的测试项目目录并将其值设置为一个名为testProjectPath的变量。

- task: PowerShell@2
  displayName: "Get tests path"
  inputs:
    targetType: "inline"
    script: |
      $testpath = Get-ChildItem -Filter *Tests.csproj -Recurse | Select-Object Directory -First 1
      
      Write-Host "##vso[task.setvariable variable=testProjectPath]$testpath"

And then I have a second script which tries to Set-Location based on the path present in my testProjectPath variable.然后我有第二个脚本,它尝试根据我的testProjectPath变量中存在的路径Set-Location

- task: PowerShell@2
  displayName: "Run mutations"
  inputs:
    targetType: "inline"
    script: |
      Set-Location $env:testProjectPath

But every time I try to run this pipeline I get the following error message:但是每次我尝试运行此管道时,都会收到以下错误消息:

@{Directory=D:\a\1\s\Wms.PickingCheck.Tests}
Set-Location : Cannot find drive. A drive with the name '@{Directory=D' does not exist.
At D:\a\_temp\752e8e31-b8ac-4f43-9ee0-248f6b12577c.ps1:4 char:1
+ Set-Location $env:testProjectPath
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (@{Directory=D:String) [Set-Location], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
 

What am I doing wrong here?我在这里做错了什么?

In your original script, the value of $testpath is @{Directory=xxxxxx} .在您的原始脚本中, $testpath的值为@{Directory=xxxxxx} However, Set-Location doesn't recognize this format, Set-Location sets the current working location to a specified location, that's why you got error:但是, Set-Location无法识别此格式, Set-Location将当前工作位置设置为指定位置,这就是您收到错误的原因:

在此处输入图像描述

You could modify your script by adding -Expand as @AdminOfThings mentioned: $testpath = Get-ChildItem -Filter *Tests.csproj -Recurse | Select-Object -Expand DirectoryName -First 1您可以通过添加-Expand作为@AdminOfThings 提到的修改您的脚本: $testpath = Get-ChildItem -Filter *Tests.csproj -Recurse | Select-Object -Expand DirectoryName -First 1 $testpath = Get-ChildItem -Filter *Tests.csproj -Recurse | Select-Object -Expand DirectoryName -First 1

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure DevOps 条件无法评估 Powershell 设置变量 - Azure DevOps conditional can't evaluate Powershell set-variable 如何将 Azure DevOps 的变量替换为 PowerShell 的变量? - How can instead of the variable of Azure DevOps to variable of PowerShell? 如何在 Azure Devops 发布管道中使用 PowerShell 设置环境变量? - How to set an environment variable with PowerShell in Azure Devops release pipeline? 在 Powershell 中通过 CLI 设置 Azure Devops 权限 - Set Azure Devops Permissions through CLI in Powershell 如何使用 Azure Devops 将 powershell 列表变量传输到 terraform? - How to transfer a powershell list variable to terraform by using Azure Devops? 将变量值从 Powershell 任务中的一个脚本传递到 Azure DevOps 发布管道中下一个 Powershell 任务中的另一个脚本 - Pass variable value from one Powershell script in Powershell task to another script in the next Powershell task in Azure DevOps Release Pipeline 如何将 azure cli 变量传递给 Azure Devops 变量? - How can i pass an azure cli variable to Azure Devops variables? 您可以在 Azure Azure Devops 的内联脚本中使用 IF 语句吗? - can you use IF statement in inline Azure Powershell script in Azure Devops? Azure DevOps PowerShell - 我可以为库组变量构建变量字符串吗 - Azure DevOps PowerShell - Can I build the variable string for a library group variable 在具有Linux容器代理的Azure DevOps上的Powershell脚本中使用环境变量 - Use environment variable in powershell script on Azure DevOps with linux container agent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM