简体   繁体   中英

How to use a custom Powershell module in a VSTS build task

I'm trying to set up VSTS build definitions that use custom Powershell scripts. I've created a BuildPackage.psm1 module file with two functions that I can use in two different build definitions. I'm trying to get one of these working, and have set up a task to import the module, followed by a task to run one of the functions.

I've copied my module into this location:

.\\src\\Tools\\PSModules\\BuildFontPackage\\BuildFontPackage.psm1

For the first task, to import the module, I've added a PowerShell Script task using the following inline script:

$env:PSModulePath = "$env:BUILD_SOURCESDIRECTORY\\src\\Tools\\PSModules;$env:PSModulePath"
Import-Module -Name BuildFontPackage.psm1

The Import-Module command runs and it appears to work: if I add the -Verbose parameter to Import-Module, I get the following:

VERBOSE: Loading module from path 'C:\\BA\\47\\s\\src\\Tools\\PSModules\\BuildFontPackage\\BuildFontPackage.psm1'.
VERBOSE: Exporting function 'New-Package'.
VERBOSE: Exporting function 'New-AllPackages'.
VERBOSE: Importing function 'New-AllPackages'.
VERBOSE: Importing function 'New-Package'.

However, in the following task, when I try to use the New-Package function within the module, I get this error:

New-Package : The term 'New-Package' is not recognized as the name of a cmdlet, function, script file, or Process completed with exit code 0 and had 1 error(s) written to the error stream.

It's as though my module isn't loaded.

How can I make this work?

It seems you are using private agent. So you can copy BuildFontPackage\\BuildFontPackage.psm1 into $env:PSModulePath . Then you can use the powershell module directly in VSTS build.

First, you can print the paths by executing echo $env:PSModulePath in your local machine (private agent). It will shows a list of paths as below:

C:\Users\username\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\Modules;C:
\windows\system32\WindowsPowerShell\v1.0\Modules\;c:\Program Files\Microsoft Security Client\MpPro
vider\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\PowerShell\Modules\;C:\Program Files\
AppFabric 1.1 for Windows Server\PowershellModules;C:\Program Files\Microsoft System Center 2012 R
2\Virtual Machine Manager\bin\psModules\;C:\Program Files\Microsoft System Center 2012 R2\Service 
Manager\PowerShell\;C:\Program Files\WindowsPowerShell\Modules\;C:\Program Files (x86)\Microsoft S
DKs\Azure\PowerShell\ResourceManager\AzureResourceManager\;C:\Program Files (x86)\Microsoft SDKs\A
zure\PowerShell\ServiceManagement\;C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\Storage\

Then copy BuildFontPackage\\BuildFontPackage.psm1 into any of the path in $env:PSModulePath .

Now you can build again, the powershell module should be recongnized by VSTS build.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM