Powershell Invoke-SQLCmd 和 SQLPS

标签 powershell import-module sqlps executionpolicy

Windows 7 工作站
时尚 3.0
SS 2012 SP1

抽象的:
开发人员 1 无法运行 Invoke-SQLCmd
Developer 2 安装类似 SS Client 工具可以运行 Invoke-SQLCmd
开发人员 1 无法运行 Import-Module SQLPS

当我尝试从笔记本电脑运行查询时,如下所示:

Invoke-sqlcmd -ServerInstance <ServerName> -Query "Select top 10 * from <SomeTable>;"

我收到以下错误:
Invoke-sqlcmd : The 'Invoke-sqlcmd' command was found in the module 'SQLPS', 
but the module could not be loaded. For more information, run 'Import-Module 
SQLPS'. 

另一个开发人员可以成功运行该命令,我们都有本地

由于显而易见的原因,我无法在我的机器上运行 Import-Module sqlps - ExecutionPolicy 受到限制。

我不确定为什么其他开发人员机器允许查询,而我的机器不允许。这是 SQL 客户端安装问题吗?

最佳答案

这一切都与微软有关。他们拥有最好的技术,但不仅仅是使用他们工具的一种方式和直接途径。 :(
您可以使用 SMO 对象与您的数据库进行交互。 (您可以将它与 C#/VB.net/PowerShell 一起使用):$
您可以使用 SQLPS
您可以使用 Invoke-SQLCmd

只需运行以下脚本以确保您的机器中安装了功能包并使用比其他解决方案更易于使用的 SQLPS(个人偏好)

Install-module -Name SqlServer -Scope CurrentUser

# Import the SQL Server Module.    
Import-Module Sqlps -DisableNameChecking;

# To check whether the module is installed.
Get-Module -ListAvailable -Name Sqlps;
cd SQLSERVER:\SQL

关于Powershell Invoke-SQLCmd 和 SQLPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33172754/

相关文章:

powershell - 登录后 Azure 凭据不可用

powershell - 如何使用 PowerShell Desired State Configuration 升级 Windows 服务

iis-7 - 使用 powershell 管理单元配置 IIS 时如何在 powershell 中使用枚举类型

PowerShell 4 - 导入模块 : The specified module 'SQLPS' was not loaded because no valid module file was found in any module directory

powershell - 如何获取我执行的PowerShell脚本的目录?

powershell - 如何将模块导入到 powershell 中而不等待其完成?

powershell - 如何正确忽略 PowerShell 中的 Import-Module 错误

sql - 无法使用带有 -InputObject 参数的 SQLPS CmdLet Backup-SQLDatabase

sql-server - 已安装 SQL,但 powershell 未获取 sqlps 命令(如 invoke-sqlcmd)