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.
Listed here: http://blog.majcica.com/2015/11/14/available-modules-for-tfs-2015-build-tasks/
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
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
Is it not possible to use the Modules from 'normal' PowerShell scripts and not only from PowerShell scripts registered as an actual build step?
It is possible to have a powershell script in build step reference a module. I prefer to use the inline powershell task as it means I dont have to go through the rigmarole of gated checkin just to change the way the code is packaged. I use it quite often to do things like...
- download a common assembly versioning ps1 script from a shared project in TFVC and execute it to stamp assemblies, nuget packages, etc with the build number.
- change the build number
Save-Module
for a module in a private PS repository, and include it in an artifact (install helpers)Save-Module
again to a temp folder, then import the module and use it to do something to the code being built.- Generate release notes or similar docs
... but you can use the regular "Run a powershell script" to do all these too.
Perhaps simpler though is to put the module in the path and update powershell if its < 5.1, If you have powershell 3 or later on the build server, and the module is already installed on the server and in the agent account's $PSModulePath
, then you dont even need to Import -Module
, you can invoke the commands in that module directly.
来源:https://stackoverflow.com/questions/39404652/how-to-load-powershell-module-from-custom-script-on-vnext-build-agent