【问题标题】:SQL Powershell Error: Invoke-Sqlcmd : The term 'Invoke-Sqlcmd' is not recognized as the name of a cmdletSQL Powershell 错误:Invoke-Sqlcmd:术语“Invoke-Sqlcmd”未被识别为 cmdlet 的名称
【发布时间】:2018-07-15 07:54:29
【问题描述】:

当我尝试运行 powershell 脚本时,出现以下错误:

Invoke-Sqlcmd :术语“Invoke-Sqlcmd”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。

所以在 Powershell 中我运行了以下命令:

install-module sqlserver
update-module sqlserver
import-module sqlserver

这一切运行良好。但是,当我重新运行我的脚本时,我仍然会遇到同样的错误。

我还安装了 powershelltools.msi,在此处作为 SQL Server 2014 功能包的一部分下载:https://www.microsoft.com/en-gb/download/details.aspx?id=42295

如果我运行这个命令:

Get-Command -Module sqlserver

我得到这个结果:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           Decode-SqlName                                     21.0.17224 sqlserver
Alias           Encode-SqlName                                     21.0.17224 sqlserver
Function        SQLSERVER:                                         21.0.17224 sqlserver

还有什么我应该尝试的想法吗?

我有 SQLServer 2014 和 Powershell 版本 5

【问题讨论】:

  • 试试Get-Command -Module sqlserver
  • 输入您正在使用的代码,它可能与此有关,例如远程查询等
  • 如果我在同一会话中执行“Import-module sqlserver”,然后立即执行“Invoke-Sqlcmd”,我会得到同样的错误。 “Get-Command -Module sqlserver”返回一个包含 2 个别名“Decode-sqlName”和“Encode-sqlName”的列表,以及一个函数“SQLSERVER:”
  • 这些行与此处的前 3 行匹配,但随后列出了所有 CmdLet's,

标签: sql-server powershell


【解决方案1】:

您使用的是 Powershell Core (v6.1) 吗?

我遇到了类似的问题,发现了这个SO answer。原来 Powershell Core 的 SQLServer 模块不包括 Invoke-SqlCmd (等等)。我切换回在 Windows 10 (v5.1) 上安装并安装的 64 位版本的 Powershell,然后导入 sqlserver 模块。现已列出 Invoke-SQLCmd。

Install-Module -Name SqlServer -AllowClobber
Import-Module -Name SqlServer -Force
Get-Command -Module SqlServer

【讨论】:

  • FYI 对于那些 7.2 有这个问题的人,我只使用了安装模块。在我重新启动 VSCODE 之前,我无法使用 invoke-sqlcmd。
【解决方案2】:

Import-Module 仅将模块导入当前的 powershell 会话,而不是全局导入。将导入添加到您的脚本或配置文件中。

【讨论】:

  • 如果我在同一会话中执行“Import-module sqlserver”,然后执行“Invoke-Sqlcmd”,我会得到同样的错误。
  • 试试 Import-Module SQLPS
【解决方案3】:

正在寻找相同问题的解决方案,发现以下对我有用。

find-module sqlserver | Install-Module -AllowClobber -Force

原答案:https://social.technet.microsoft.com/Forums/en-US/f3a52235-e62a-402e-9b1b-0b0c0cdd17aa/sql-powershell-error-invokesqlcmd-the-term-invokesqlcmd-is-not-recognized-as-the-name-of-a?forum=winserverpowershell

【讨论】:

    【解决方案4】:

    我遇到了同样的问题。显然我不得不取消阻止新模块文件夹中的所有 dll 文件。在我的情况下C:\Program Files\WindowsPowerShell\Modules\SqlServer。 我在这里找到了如何做的链接。 https://www.404techsupport.com/2016/06/24/unblock-files-powershell/

    dir -Path [directory path] -Recurse | Unblock-File
    

    如果您仍有会话打开,请关闭 powershell。

    【讨论】:

      猜你喜欢
      • 2016-10-15
      • 1970-01-01
      • 2018-08-06
      • 2012-09-13
      • 1970-01-01
      • 2014-03-30
      • 1970-01-01
      • 2016-01-15
      • 1970-01-01
      相关资源
      最近更新 更多