Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import-Module : The specified module 'SqlServer' was not loaded because no valid module file was found in any module directory

Well, hello there!

I'm working on a Script to get the Sql Job History and need to use the "SqlServer" Module. It's installed but I can't import it due to the Error Message above. When I got to the Modules Path, the Folder "SqlServer" exists and isn't empty. Don't get the problem here.

Thanks in advance!

like image 205
mbr Avatar asked Feb 08 '18 12:02

mbr


People also ask

Was not loaded because no valid module file was?

Solution: This error is because the PowerShell console or ISE is unable to get the module from given module path's in the environment variable “PSModulePath”. To fix the issue, You have to make sure your custom module's path is appended to the PSModulePath environment variable.

How do I load a SQL module in PowerShell?

To install the SqlServer module from the PowerShell Gallery, start a PowerShell session and run Install-Module SQLServer . If running on Windows PowerShell you can use Install-Module SQLServer -Scope CurrentUser to install the module for just the current user and avoid needing elevated permissions.

How do I install a PowerShell module?

Installing PowerShell modules from the PowerShell Gallery is the easiest way to install modules. To install a package or module from the Gallery, we use the command: Install-Module or Install-Script cmdlet, depending on the package type.

Where is the PowerShell module directory?

The AllUsers location is $env:PROGRAMFILES\PowerShell\Modules on Windows. On Linux or Mac the modules are stored at /usr/local/share/powershell/Modules .


1 Answers

I just ran into this on SQL Server 2017 (on Windows Server 2016) and found your question in the process. I then went and fired up our older SQL Server 2014 (Windows Server 2012) machine and found the same issue. Here's a couple options everyone can try to save some time (as I realize the question is kind of old and I'm assuming the OP found a solution already):

1) At a powershell command run: Install-Module -Name SqlServer (you might need -AllowClobber parameter)

The thing here is that module is installed to the following path on both 2012 and 2016 servers:

C:\Program Files\WindowsPowerShell\Modules\SqlServer (rather than Microsoft SQL Server\120\Tools\PowerShell\Modules)

2) Another option is to try the SQLPS module by changing in your script:

Import-Module -Name SqlServer

To:

Import-Module -Name SQLPS

You will find SQLPS in the Microsoft SQL Server directory structure here:

C:\Program Files (x86)\Microsoft SQL Server\120\Tools\PowerShell\Modules

My script works with both SQL modules now, but the output actually looks better with SQLPS at this point...

You can find more on all of the above at this link

like image 155
Sum None Avatar answered Sep 20 '22 14:09

Sum None