简体   繁体   English

TFS 2015上无法识别导入模块SQLPS

[英]Import-Module SQLPS Not Recognized on on TFS 2015

I am adding integration tests to our CI build process. 我正在将集成测试添加到我们的CI构建过程中。

I have a database project that is deployed (via dacpac) to a localDb instance on the TFS server. 我有一个数据库项目(通过dacpac)部署到TFS服务器上的localDb实例。 (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. 我正在尝试导入SQLPS模块,然后运行Invoke-SqlCmd命令集。

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. 我不断收到无法识别SQLPS的错误。

The TFS server has SQL Server 2012 installed - but I am using the 2014 objects. TFS服务器已安装SQL Server 2012-但我正在使用2014对象。 I have installed the SQL 2014 Feature pack so that the powertools is available on the server and the build agent. 我已经安装了SQL 2014 Feature Pack,以便可以在服务器和构建代理上使用powertools。

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. Powershell不是我的强项之一,因此对于在其构建服务器上寻找SQL Server Powershell工具的其他任何人。

You'll need to install the SQL Server Feature Pack (if SQL Server is not installed) on your build server. 您需要在构建服务器上安装SQL Server Feature Pack(如果未安装SQL Server)。 The feature pack is available for for 2012 and up editions. 该功能包适用于2012年及更高版本。

The specific items you need are: SQL CLR Types SQL Shared Management Objects Powershell Tools 您需要的特定项目是:SQL CLR类型SQL共享管理对象Powershell工具

The Powershell tools will install in the C:\\Program Files\\Microsoft SQL Server{version}\\Tools\\PowerShell\\Modules directory. Powershell工具将安装在C:\\ Program Files \\ Microsoft SQL Server {version} \\ Tools \\ PowerShell \\ Modules目录中。 Where {version: 110=2012, 120=2014, 130=2016} 其中{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: 使用PS工具的路径参考-您要做的就是创建本地模块路径参考:

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. 我的PS脚本采用了参数server,数据库,sqlPowerToolsPath,sqlFile,并且我设置了本地会话PSModulePath以将路径包括在参数中。 In my case C:\\Program Files\\Microsoft SQL Server\\120\\Tools\\PowerShell\\Modules. 在我的情况下,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: 脚本的其余部分将导入SQLPS模块并执行参数中指定的SQL文件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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