简体   繁体   English

安装了 SQL 但 powershell 没有接收 sqlps 命令(如 invoke-sqlcmd)

[英]SQL installed but powershell not picking up sqlps commands (like invoke-sqlcmd)

I installed ms sql server with chocolatey:我用巧克力安装了 ms sql server:

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

SQL seems to be installed and working well outside of powershell where it doesn't work. SQL 似乎已安装并在无法正常工作的 powershell 之外运行良好。 I can see the sqlps module with:我可以看到 sqlps 模块:

Get-Module -listavailable

... ...

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

Manifest   1.0        SQLASCMDLETS
Manifest   1.0        SQLPS

The commands seem to be missing though.但是命令似乎丢失了。 I don't have invoke-sqlcmd etc. In theory I should get access to them if I install the module but when I try to import-module sqlps but I get an error about not having a sqlserver drive:我没有 invoke-sqlcmd 等。理论上,如果我安装模块,我应该可以访问它们,但是当我尝试导入模块 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

I know several people in my group who went through these steps and did get the correct sql ps setup working.我知道我的小组中有几个人完成了这些步骤并且确实使正确的 sql ps 设置正常工作。

Any tips or ideas would be very helpful.任何提示或想法都会非常有帮助。 Thanks.谢谢。

Good day,再会,

I am guessing that you are using SQL Server 2017, since this is common issue in 2017, as I will explain below.我猜您正在使用 SQL Server 2017,因为这是 2017 年的常见问题,我将在下面解释。 I am not sure what version is used since this question is a bit old and was asked on May 2 '17 at 22:58我不确定使用的是什么版本,因为这个问题有点旧,并且在 2017 年 5 月 2 日 22:58 被问到

The error that you get includes the basic issue您得到的错误包括基本问题

Set-Location : Cannot find drive.设置位置:找不到驱动器。 A drive with the name 'SQLSERVER' does not exist.名为“SQLSERVER”的驱动器不存在。

It does not say that the module 'SQLPS' does not exist, but that the module 'SQLSERVER' does not exist它并不是说模块“SQLPS”不存在,而是模块“SQLSERVER”不存在

The explanation is that until 2016 SQLPS module was included with the SQL Server installation, but the PowerShell module which we use is the 'SqlServer' module.解释是直到 2016 年 SQLPS 模块都包含在 SQL Server 安装中,但我们使用的 PowerShell 模块是“SqlServer”模块。 'SqlServer' module was included with SQL Server Management Studio (SSMS) 16.x, but if you are using SSMS 2017 (17.x) then 'SqlServer' module must be installed from the PowerShell Gallery . “SqlServer”模块包含在 SQL Server Management Studio (SSMS) 16.x 中,但如果您使用的是 SSMS 2017 (17.x),则必须从 PowerShell Gallery 安装“SqlServer”模块

The procedure to install it is to execute the command:安装它的过程是执行命令:

Install-Module -Name SqlServer

If you get error like PackageManagement\\Install-Package : The following commands are already available on this system:...如果您收到类似PackageManagement\\Install-Package : The following commands are already available on this system:...错误PackageManagement\\Install-Package : The following commands are already available on this system:...

Then you can enforce the installation using the parameters: -Force and –AllowClobber然后您可以使用以下参数强制安装: -Force–AllowClobber

Since I am not familiar with your system, I will NOT advice you what to do or say if you should enforce the installation, but this is the solution which I would probably do in most cases like this (according to the information I noticed in this thread)由于我不熟悉你的系统,我不会建议你做什么或说你是否应该强制安装,但这是我在大多数情况下可能会做的解决方案(根据我在此注意到的信息)线)

Install-Module -Name SqlServer -Force –AllowClobber

In order to confirm that the module is instead you can execute the following command:为了确认该模块是相反的,您可以执行以下命令:

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

Check the version of your installation using the command above, and use it in the following command in order to import the newest version (at the time I write this answer the version is 21.0.17279):使用上面的命令检查您的安装版本,并在以下命令中使用它以导入最新版本(在我写这个答案时,版本是 21.0.17279):

Import-Module SqlServer -Version 21.0.17279

That is all... If all went well then you should be able to use all the SQL Server PowerShell commands就是这样...如果一切顺利,那么您应该能够使用所有 SQL Server PowerShell 命令

As pointed, here are the links that you should refer now.正如所指出的,这里是您现在应该参考的链接。

MSDN Link MSDN链接

Running SQL Server Powershell运行 SQL Server Powershell

Cannot find path 'SQLSERVER' Issue 找不到路径“SQLSERVER”问题

There is an answer given by Jarret. Jarret 给出了答案。 Simply loading the module won't help actually.简单地加载模块实际上无济于事。 These set of commands have to run after that.这组命令必须在那之后运行。

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

Hope it helps.希望能帮助到你。

Just a side-note for future readers, I was trying to create a sql backup via powershell.只是给未来读者的一个旁注,我试图通过 powershell 创建一个 sql 备份。 The cmdlets ran as they should under an Administrator account, however running the script under a regular user account, I got the following error: cmdlet 在Administrator帐户下正常运行,但是在普通用户帐户下运行脚本,我收到以下错误:

Cannot find a provider with the name 'SqlServer'

Googling that question brought me here, but the answer to my issue was in a forum post here: https://social.technet.microsoft.com/Forums/windowsserver/en-US/626bb81a-00ba-4239-ad0a-fec32546350a/check-if-drive-exists-if-not-map?forum=winserverpowershell谷歌搜索这个问题把我带到了这里,但我的问题的答案在这里的论坛帖子中: https : //social.technet.microsoft.com/Forums/windowsserver/en-US/626bb81a-00ba-4239-ad0a-fec32546350a/ check-if-drive-exists-if-not-map?forum=winserverpowershell

I encountered a weird issue and hope that somebody may have a fix for this.我遇到了一个奇怪的问题,希望有人能解决这个问题。

Microsoft SQL Server 2014 (SP2-GDR) (KB4505217) - 12.0.5223.6 (X64) (yeah I know...it's a dev server) Microsoft SQL Server 2014 (SP2-GDR) (KB4505217) - 12.0.5223.6 (X64)(是的,我知道......这是一个开发服务器)

Windows 2012 R2视窗 2012 R2

PSVersion 4.0 PS版本 4.0

I load the SQL assemblies in the PS script我在 PS 脚本中加载 SQL 程序集

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null出空

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null出空

The following will fail if I run this immediately after loading the assemblies如果我在加载程序集后立即运行它,以下将失败

Get-ChildItem SQLSERVER:\\SQL$env:COMPUTERNAME Get-ChildItem SQLSERVER:\\SQL$env:COMPUTERNAME

Get-ChildItem : Cannot find drive. Get-ChildItem:找不到驱动器。 A drive with the name 'SQLSERVER' does not exist.名为“SQLSERVER”的驱动器不存在。

If I run the following first, I'm fine.如果我先运行以下,我很好。

Invoke-Sqlcmd -Query "SELECT TOP 1 * FROM sys.sysobjects" | Invoke-Sqlcmd -Query "SELECT TOP 1 * FROM sys.sysobjects" | Out-Null出空

Get-ChildItem SQLSERVER:\\SQL$env:COMPUTERNAME Get-ChildItem SQLSERVER:\\SQL$env:COMPUTERNAME

I just wanted to amend that I just need to run我只是想修改我只需要运行

Invoke-Sqlcmd |调用-Sqlcmd | Out-Null出空

This seems to then fully load the assemblies into memory and everything is OK after.这似乎然后将程序集完全加载到内存中,之后一切正常。

So I added this to my script:所以我将此添加到我的脚本中:

# 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

Seems like valuable information that shouldn't get lost, so I thought i'd post it in the highest SO when googling for that particular error.似乎有价值的信息不应该丢失,所以我想我会在谷歌搜索该特定错误时将其发布在最高的 SO 中。

One could argue that installing the module rather than this dummy method would be cleaner.有人可能会争辩说,安装模块而不是这种虚拟方法会更干净。

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

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