[英]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.