如何从vNext构建代理上的自定义脚本加载PowerShell模块? [英] How to load PowerShell Module from custom script on vNext build agent?
问题描述
我正在使用标准的TFS vNext构建步骤来执行PowerShell脚本.在脚本内部,我试图利用标准TFS代理模块中的某些功能.
I am using the standard TFS vNext build step to execute a PowerShell script. Inside the script I am trying to take advantage of some of the functions within the standard TFS Agent modules.
在这里列出: http://blog.majcica. com/2015/11/14/available-modules-for-tfs-2015-build-tasks/
在构建步骤中找到的许多PowerShell脚本中,我已经看到以下两行:
I have seen the following two lines in many PowerShell scripts found in the build steps:
Import-Module "Microsoft.TeamFoundation.DistributedTask.Task.Internal"
Import-Module "Microsoft.TeamFoundation.DistributedTask.Task.Common"
我尝试在脚本中使用相同的行,但是出现错误:
I have tried to use the same lines in my script, however I get the error:
VERBOSE: Loading module from path
'C:\TFS2015-Agent\Agent1\agent\agent\worker\Modules\Microsoft.TeamFoundation.DistributedTask.Task.Common\Microsoft.TeamFoundation.DistributedTask.Task.Common.dll'.
Import-Module : Could not load file or assembly 'Microsoft.TeamFoundation.DistributedTask.Agent.Interfaces, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.
The system cannot find the file specified.
At C:\tfsVnBw1\3\s\Configuration\BuildScripts\CommonFunctions.ps1:25 char:5
+ Import-Module "Microsoft.TeamFoundation.DistributedTask.Task.Common" - Error ... + CategoryInfo : NotSpecified: (:) [Import-Module], FileNotFoundException + FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.ImportModuleCommand
如果我尝试不导入,它会写成这样:
If I try not to import, it writes something like:
The 'Find-Files' command was found in the module 'Microsoft.TeamFoundation.DistributedTask.Task.Common',
but the module could not be loaded. For more information, run 'Import-Module Microsoft.TeamFoundation.DistributedTask.Task.Common'.
At C:\tfsVnBw1\3\s\Configuration\BuildScripts\CommonFunctions.ps1:71 char:16
+ $files = @(FindFiles $filePattern)
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Find-Files:String) [FindFiles], CommandNotFoundException + FullyQualifiedErrorId : CouldNotAutoloadMatchingModule
是否不可能仅从常规" PowerShell脚本中使用模块,而不仅是从注册为实际构建步骤的PowerShell脚本中使用模块?
Is it not possible to use the Modules from 'normal' PowerShell scripts and not only from PowerShell scripts registered as an actual build step?
推荐答案
尝试指定构建代理的模块路径,例如:
Try to specify build agent’s module path, such as:
# Import the Task.Common and Task.Internal dll that has all the cmdlets we need for Build
$agentWorkerModulesPathRoot = "$($env:AGENT_HOMEDIRECTORY)\agent\worker"
$agentDistributedTaskInterfacesModulePath = "$agentWorkerModulesPathRoot\Microsoft.TeamFoundation.DistributedTask.Agent.Interfaces.dll"
$agentWorkerModulesPath = "$($env:AGENT_HOMEDIRECTORY)\agent\worker\Modules"
$agentDistributedTaskCommonModulePath = "$agentWorkerModulesPath\Microsoft.TeamFoundation.DistributedTask.Task.Common\Microsoft.TeamFoundation.DistributedTask.Task.Common.dll"
Write-Host "Importing VSTS Module $agentDistributedTaskInterfacesModulePath"
Import-Module $agentDistributedTaskInterfacesModulePath
Write-Host "Importing VSTS Module $agentDistributedTaskCommonModulePath"
Import-Module $agentDistributedTaskCommonModulePath
这篇关于如何从vNext构建代理上的自定义脚本加载PowerShell模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!