2

I'm having an odd issue with running a PowerShell script as a TeamCity build step, particularly with SqlPs (though I'm not sure it's specific to SqlPs).

This one-line script fails in TeamCity:

Import-Module SqlPs

With the following error:

Import-Module : The specified module 'SqlPs' was not loaded because no valid
module file was found in any module directory.
At C:\TeamCity\buildAgent\temp\buildTmp\powershell2962302231054758126.ps1:13 
char:1
+ Import-Module SqlPs
+ ~~~~~~~~~~~~~~~~~~~
     + CategoryInfo          : ResourceUnavailable: (SqlPs:String) [Import-Module], 
     FileNotFoundException

     + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands
     .ImportModuleCommand

I'm pretty sure I have everything configured properly, because if I open PowerShell natively from the CI server that TeamCity is running on, I can run this with no problems.

I can even run the above temp script file that TeamCity generates from within the native PowerShell, and it works just fine.

Further, if I execute Get-Module from the native PowerShell:

Get-Module -ListAvailable

It will dump several system modules from:

C:\Windows\system32\WindowsPowerShell\v1.0\Modules

Along with the SqlSp modules from:

C:\Program Files\Microsoft SQL Server\110\Tools\PowerShell\Modules

But when I run the same Get-Module from the TeamCity build, it will only list the system modules -- no SqlSp module.

I thought it might be an issue with permissions, but when I log in to native PowerShell as the same account that TeamCity uses, I still have no issues.

I also tried selecting x86 instead of x64 for bitness just to rule that out, and it fails either way.

Putting "SqlPs" in quotes also didn't help.

Jerad Rose
  • 15,235
  • 18
  • 82
  • 153
  • Just had the same issue and here the problem is that TeamCity starts the 32bit version of powershell, which doesn't have a ceratin module, whereas the 64bit version does have it. `Write-Host $env:PSModulePath` in a script to debug this. – stijn Jun 03 '22 at 16:18

3 Answers3

0

While this doesn't address the issue of why this doesn't work:

Import-Module SqlPs

I can get this to at least work by specifying the fully-qualified path in the Import-Module:

Import-Module "C:\Program Files\Microsoft SQL Server\110\Tools\PowerShell\Modules\SQLPS\SQLPS.PS1"

Would still love to find out why the first one isn't working.

Jerad Rose
  • 15,235
  • 18
  • 82
  • 153
0

Due to an issue unrelated to this issue, I ended up needing to upgrade TeamCity from version 8 to 9. Not sure why, but looks like SqlSp now works just fine under TeamCity 9.

Jerad Rose
  • 15,235
  • 18
  • 82
  • 153
0

simple solution for your problem is add your module path to default module path see my example please
$env:PSModulePath this environment varibale for powershell module i want add another path to default psmodulepath
first see default module path

C:\Users\soheil\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\Wind
owsPowerShell\v1.0\Modules\

now try to add another path to default path

$env:PSModulePath = $env:PSModulePath + ";c:\module\"

try again to see default path

C:\Users\soheil\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\Wind
owsPowerShell\v1.0\Modules\;c:\module\
Soheil
  • 837
  • 8
  • 17