You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Invoke-Sqlcmd在PS 5正常运行但PS 7.3.1中报错求助

解决PowerShell 7中Invoke-Sqlcmd未识别的问题

问题原因

PowerShell 5.x中的Invoke-Sqlcmd来自SQL Server安装时附带的SQLPS模块,但该模块仅兼容Windows PowerShell(5.x及以下),无法在跨平台的PowerShell 7+中运行。你升级到PS7.3.1后,原有的SQLPS模块不会被自动加载,因此出现命令未识别的错误。

解决方案

1. 安装官方兼容PS7的SqlServer模块

以管理员身份启动PowerShell 7,执行以下命令安装模块(如果提示执行策略限制,先设置允许脚本执行):

# 临时设置执行策略(可选,若安装时提示权限问题)
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force

# 安装SqlServer模块
Install-Module -Name SqlServer -Scope CurrentUser -Force

2. 导入模块

安装完成后,在当前会话中导入模块:

Import-Module SqlServer

如果希望每次启动PS7都自动加载该模块,可以将上述命令添加到你的PowerShell配置文件中(通过$PROFILE查看配置文件路径)。

3. 验证命令可用性

执行以下命令确认Invoke-Sqlcmd已被系统识别:

Get-Command Invoke-Sqlcmd

4. 执行原有业务命令

模块导入完成后,即可正常运行你的SQL导出命令:

Invoke-Sqlcmd -ServerInstance "ServerName" -Database "DBName" -InputFile "D:\SCRIPTS\Adds.sql" | Export-Csv "C:\temp\Adds.txt"  -Delimiter "|" -UseQuotes AsNeeded -NoTypeInformation

额外说明

  • SqlServer模块是微软官方维护的跨平台模块,完全兼容PowerShell 7+,包含Invoke-Sqlcmd及其他SQL管理相关cmdlet
  • 若安装时遇到网络问题,可手动从PowerShell Gallery下载模块包进行离线安装

内容的提问来源于stack exchange,提问作者Larry Krauss

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 10:35:41