【问题标题】:SQL installed but powershell not picking up sqlps commands (like invoke-sqlcmd)已安装 SQL,但 powershell 未获取 sqlps 命令(如 invoke-sqlcmd)
【发布时间】:2017-09-30 14:30:04
【问题描述】:

我用chocolatey安装了ms sql server:

choco install SQLServer2012DeveloperEditionWithSP1 -y -f -source 'http://choco.developers.tcpl.ca/chocolatey' -c "$env:WINDIR\temp"

SQL 似乎已在无法运行的 powershell 之外安装并运行良好。我可以看到 sqlps 模块:

Get-Module -listavailable

...

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------

Manifest   1.0        SQLASCMDLETS
Manifest   1.0        SQLPS

这些命令似乎不见了。我没有 invoke-sqlcmd 等。理论上,如果我安装模块,我应该可以访问它们,但是当我尝试 import-module sqlps 但我收到一个关于没有 sqlserver 驱动器的错误:

PS C:\WINDOWS\system32> Import-Module SQLPS
Set-Location : Cannot find drive. A drive with the name 'SQLSERVER' does not exist.
At C:\Program Files (x86)\Microsoft SQL Server\110\Tools\PowerShell\Modules\SQLPS\SqlPsPostScript.ps1:1 char:1
+ Set-Location SQLSERVER:
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (SQLSERVER:String) [Set-Location], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

我认识我小组中的几个人,他们完成了这些步骤,并且确实使正确的 sql ps 设置正常工作。

任何提示或想法都会非常有帮助。谢谢。

【问题讨论】:

标签: sql-server powershell sqlps invoke-sqlcmd


【解决方案1】:

早安,

我猜您使用的是 SQL Server 2017,因为这是 2017 年的常见问题,我将在下面解释。我不确定使用的是什么版本,因为这个问题有点老了,是在 2017 年 5 月 2 日 22:58 提出的

您收到的错误包括基本问题

设置位置:找不到驱动器。名为“SQLSERVER”的驱动器不存在。

并不是说模块“SQLPS”不存在,而是模块“SQLSERVER”不存在

解释是直到 2016 年 SQLPS 模块都包含在 SQL Server 安装中,但我们使用的 PowerShell 模块是“SqlServer”模块。 “SqlServer”模块包含在 SQL Server Management Studio (SSMS) 16.x 中,但如果您使用的是 SSMS 2017 (17.x),那么 'SqlServer' module must be installed from the PowerShell Gallery。

安装过程是执行命令:

Install-Module -Name SqlServer

如果您收到PackageManagement\Install-Package : The following commands are already available on this system:... 之类的错误

然后您可以使用参数强制安装:-Force 和 –AllowClobber

由于我不熟悉你的系统,我不会建议你做什么或者说你是否应该强制安装,但这是我在大多数情况下可能会做的解决方案,比如这个(根据我在这个帖子中注意到的信息)

Install-Module -Name SqlServer -Force –AllowClobber

为了确认模块是不是你可以执行以下命令:

Get-Module -Name SqlServer -listAvailable | select Name, ModuleType, Version

使用上面的命令检查您的安装版本,并在以下命令中使用它以导入最新版本(在我写这个答案时版本是 21.0.17279):

Import-Module SqlServer -Version 21.0.17279

就是这样......如果一切顺利,那么您应该能够使用所有 SQL Server PowerShell 命令

【讨论】:

    【解决方案2】:

    对于未来的读者来说,只是一个旁注,我试图通过 powershell 创建一个 sql 备份。 cmdlet 在Administrator 帐户下正常运行,但是在普通用户帐户下运行脚本时,出现以下错误:

    Cannot find a provider with the name 'SqlServer'

    谷歌搜索该问题将我带到这里,但我的问题的答案在论坛帖子中: https://social.technet.microsoft.com/Forums/windowsserver/en-US/626bb81a-00ba-4239-ad0a-fec32546350a/check-if-drive-exists-if-not-map?forum=winserverpowershell

    我遇到了一个奇怪的问题,希望有人能解决这个问题。

    Microsoft SQL Server 2014 (SP2-GDR) (KB4505217) - 12.0.5223.6 (X64)(是的,我知道...这是一个开发服务器)

    Windows 2012 R2

    PSVersion 4.0

    我在 PS 脚本中加载 SQL 程序集

    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") |外空

    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") |外空

    如果我在加载程序集后立即运行,以下将失败

    Get-ChildItem SQLSERVER:\SQL$env:COMPUTERNAME

    Get-ChildItem:找不到驱动器。名为“SQLSERVER”的驱动器不存在。

    如果我先运行以下,我很好。

    Invoke-Sqlcmd -Query "SELECT TOP 1 * FROM sys.sysobjects" |外空

    Get-ChildItem SQLSERVER:\SQL$env:COMPUTERNAME

    我只是想修改一下我只需要运行

    调用-Sqlcmd |外空

    这似乎将程序集完全加载到内存中,之后一切正常。

    所以我将这个添加到我的脚本中:

    # SQL cmdlets below need some dll imports from system
    # These do not get loaded sometime when running under an non-admin account (Cannof find a provider with the name 'SqlServer')
    # Running this dummy command seems to load all needed dlls
    # Also see: https://www.sqlservercentral.com/forums/topic/unable-access-sql-provider-in-powershell-without-running-an-invoke-sqlcmd-first
    Invoke-Sqlcmd | Out-Null
    

    似乎是不应该丢失的有价值的信息,所以我想在谷歌搜索该特定错误时将其发布在最高 SO 中。

    有人可能会争辩说,安装模块而不是这种虚拟方法会更干净。

    【讨论】:

      【解决方案3】:

      如前所述,这里是您现在应该参考的链接。

      MSDN Link

      Running SQL Server Powershell

      Cannot find path 'SQLSERVER' Issue

      Jarret 给出了一个答案。简单地加载模块实际上并没有帮助。这组命令必须在那之后运行。

      Push-Location
      cd $sqlpsPath
      Add-PSSnapin SqlServerCmdletSnapin100
      Add-PSSnapin SqlServerProviderSnapin100
      Update-TypeData -PrependPath SQLProvider.Types.ps1xml 
      update-FormatData -prependpath SQLProvider.Format.ps1xml 
      Pop-Location
      

      希望对你有帮助。

      【讨论】:

      • 好的,意识到我只看到了我认为是同一件事的 SqlServerProviderSnapin110。然后我能够执行这些步骤(没有 $sqlPath,但在 C:\Program Files (x86)\Microsoft SQL Server\110\Tools\PowerShell\Modules\SQLPS 中有适当的 *.ps1xm1 文件)。运行这些步骤后,仍然没有 invoke-sqlcmd :(
      • 我想我仍然收到您所说的错误:在导入(导入模块 SQLPS)时,我仍然收到“设置位置:找不到驱动器。名称为“SQLSERVER”的驱动器' 不存在。”
      • 所以我尝试了几个不同版本的sql,这似乎不是问题。这让我觉得它的 powershell ......但我当然不能卸载 powershell 来解决这个问题。想知道是否可能有另一种方法来重置 powershell....
      • 如果我使用“powershell -version 2”降级我的powershell vesion(一直到2)显然有帮助,那么我最终可以导入模块并运行invoke-sqlcmd。请注意,我不太喜欢以这种方式工作。
      • 那是个很糟糕的主意。你可以卸载模块,然后如果 uvr 没有进入它,则将其升级到版本。然后再次尝试导入?我没有足够的时间仔细看,但我会尝试
      猜你喜欢
      • 2016-01-15
      • 1970-01-01
      • 2015-06-16
      • 1970-01-01
      • 2018-07-15
      • 1970-01-01
      • 1970-01-01
      • 2010-12-29
      • 2021-10-25
      相关资源
      最近更新 更多