Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I installed ms sql server with chocolatey:

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. I can see the sqlps module with:

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:

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.

Any tips or ideas would be very helpful. Thanks.

like image 559
Chris Avatar asked Feb 05 '23 12:02

Chris


1 Answers

Good day,

I am guessing that you are using SQL Server 2017, since this is common issue in 2017, as I will explain below. 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

The error that you get includes the basic issue

Set-Location : Cannot find drive. A drive with the name 'SQLSERVER' does not exist.

It does not say that the module 'SQLPS' does not exist, but that the module 'SQLSERVER' does not exist

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. '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.

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:...

Then you can enforce the installation using the parameters: -Force and –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):

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

like image 192
Ronen Ariely Avatar answered Apr 29 '23 21:04

Ronen Ariely