简体   繁体   中英

Import-Module SQLPS Not Recognized on on TFS 2015

I am adding integration tests to our CI build process.

I have a database project that is deployed (via dacpac) to a localDb instance on the TFS server. (working)

The next step is to load the test data into the database. I am trying to import the SQLPS module and then run the Invoke-SqlCmd commandlet.

Import-Module 'SQLPS' -DisableNameChecking;
Invoke-Sqlcmd -InputFile "$localDirectory\products\$branch\UnitTestData.sql" -ServerInstance "(localdb)\v12.0" -Database "[db]"

I keep getting an error that SQLPS is not recognized.

The TFS server has SQL Server 2012 installed - but I am using the 2014 objects. I have installed the SQL 2014 Feature pack so that the powertools is available on the server and the build agent.

This seems pretty routine - but I'm not sure where I am going wrong here.

Got this figured out. Powershell is not one of my stronger suits, so for anyone else that is looking for the SQL Server Powershell Tools on their build server.

You'll need to install the SQL Server Feature Pack (if SQL Server is not installed) on your build server. The feature pack is available for for 2012 and up editions.

The specific items you need are: SQL CLR Types SQL Shared Management Objects Powershell Tools

The Powershell tools will install in the C:\\Program Files\\Microsoft SQL Server{version}\\Tools\\PowerShell\\Modules directory. Where {version: 110=2012, 120=2014, 130=2016}

With the path reference to PS tools - all you need to do is create a local module path reference:

Param(
    [Parameter(Mandatory=$True)][string]$server,
    [Parameter(Mandatory=$True)][string]$database,
    [Parameter(Mandatory=$True)][string]$sqlPowerToolsPath,
    [Parameter(Mandatory=$True)][string]$sqlFile
)

$env:PSModulePath = $env:PSModulePath + ";$sqlPowerToolsPath"

My PS script takes parameters server, database, sqlPowerToolsPath, sqlFile and I set the local session PSModulePath to include the path in the parameter. In my case C:\\Program Files\\Microsoft SQL Server\\120\\Tools\\PowerShell\\Modules.

The rest of the script imports the SQLPS module and executes the SQL file specified in the parameter:

Import-Module 'SQLPS' -DisableNameChecking;
$sql = [Io.File]::ReadAllText($sqlFile);
Invoke-Sqlcmd -Query $sql -ServerInstance "$server" -Database "$database"

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