首页 > 解决方案 > TFS 2017 构建执行 powershell 由于第 1 行上的术语无法识别错误而失败

问题描述

我对 TFS 很陌生,我仍在学习如何使用它(所以希望这不仅仅是我的愚蠢疏忽)。今年夏天,我在季节性开发人员职位上实习,基本上我的最终目标是为公司网站自动进行负载测试。我正在使用 TFS 来实现这个目标;我目前的构建现在只有两个任务:一个是启动控制器和测试环境,另一个是停止它们。我的问题是,由于第 1 行出现“术语无法识别”错误,构建在真正开始之前一直失败,特别是由于默认工作文件夹似乎无法识别。

以下是相关的日志文件:

2019-05-30T20:00:02.0942883Z Executing the following powershell script. (workingFolder = D:\RM_agent\_work\11\s)
2019-05-30T20:00:02.0942883Z D:\RM_agent\_work\11\s 
2019-05-30T20:00:02.4999117Z ##[error]. : The term 'D:\RM_agent\_work\11\s' is not recognized as the name of a 
2019-05-30T20:00:02.4999117Z ##[error]cmdlet, function, script file, or operable program. Check the spelling of the 
2019-05-30T20:00:02.4999117Z ##[error]name, or if a path was included, verify that the path is correct and try again.
2019-05-30T20:00:02.4999117Z ##[error]At line:1 char:3
2019-05-30T20:00:02.4999117Z ##[error]+ . 'D:\RM_agent\_work\11\s'
2019-05-30T20:00:02.4999117Z ##[error]+   ~~~~~~~~~~~~~~~~~~~~~~~~
2019-05-30T20:00:02.4999117Z ##[error]    + CategoryInfo          : ObjectNotFound: (D:\RM_agent\_work\11\s:String)  
2019-05-30T20:00:02.4999117Z ##[error]   [], CommandNotFoundException
2019-05-30T20:00:02.4999117Z ##[error]    + FullyQualifiedErrorId : CommandNotFoundException

我知道工作文件夹默认为$(Build.SourcesDirectory),所以我假设这D:\RM_agent\_work\11\s$(Build.SourcesDirectory)评估结果。RM_agent显然是一个代理,所以/_work/11/s应该是它存储源代码的本地路径。那为什么不被认出来呢?

我尝试通过 tfs 手动将脚本的工作文件夹设置为存储构建的文件夹,但构建仍然失败并且日志仍然显示workingFolder = D:\RM_agent\_work\11\s.

在此处输入图像描述

此外,构建失败的代码行在Executing the following powershell script. (workingFolder = D:\RM_agent\_work\11\s)我尝试执行的脚本中没有,这让我感到困惑。这个脚本是从哪里来的?

(如果它不符合指南/偏离主题,我可以将其删除,但如果有人能指出我有关 tfs 和/或负载测试的任何资源,它也会非常有帮助)

编辑:这是第一个任务的 powershell 脚本

########################################
# start environment
########################################

# import modules
Import-Module '\\neenah-san1\TSbuild\Deployment\Tools\PowerShell\Azure\JJK.TS.Azure.psm1' -Force -Prefix 'TS'

# provide azure credentials
$credential = Get-Credential

# login to azure subscription
Login-AzureRmAccount -Credential $credential

# start the controller
Get-AzureRmVM -ResourceGroupName 'TS-LoadTest-TST' | Where-Object {$_.Name -match 'vstc'} | Start-TSAzureVM -Credential $credential

# wait for controller to fully start
Start-Sleep -Seconds 120

# start the agents
Get-AzureRmVM -ResourceGroupName 'TS-LoadTest-TST' | Where-Object {$_.Name -match 'vsta'} | Start-TSAzureVM -Credential $credential

# check status of all servers
Get-AzureRmVM -ResourceGroupName 'TS-LoadTest-TST' -Status | Sort-Object -Property Name | Select-Object -Property Name, PowerState | Format-Table -AutoSize

解决方案结构: 在此处输入图像描述

编辑 2:[已解决] 现在一切都已修复,谢谢!我进入存储库并将我的脚本所在的文件夹直接映射到$(build.sourcesDirectory). 因此,我能够将文件路径更改为,$(build.sourcesDirectory)\StartControllerAndAgents.ps1并且构建现在能够找到要运行的文件。

标签: powershelltfsazure-pipelines

解决方案


您需要将脚本的路径指定为$(Build.SourcesDirectory)\Path\To\Script. 不是您现在配置的 TFVC 路径$/Project/Path/To/Script

确切的路径取决于构建定义的工作区映射。

这同样适用于工作目录。

Azure Pipelines(TFS/Azure DevOps 中构建中心的当前名称)中有许多变量可解析为代理上的不同标准化路径。几乎所有任务都采用这些变量的相对路径。


推荐阅读