Powershell Invoke-SQLCmd and SQLPS

Riskworks picture Riskworks · Oct 16, 2015 · Viewed 23.1k times · Source

Windows 7 workstation POSH 3.0 SS 2012 SP1

Abstract:
Developer 1 cannot run Invoke-SQLCmd
Developer 2 with similar SS Client tools installation can run Invoke-SQLCmd
Developer 1 cannot run Import-Module SQLPS

When I try to run a query from my laptop like the following:

Invoke-sqlcmd -ServerInstance <ServerName> -Query "Select top 10 * from <SomeTable>;"

I get the following error:

Invoke-sqlcmd : The 'Invoke-sqlcmd' command was found in the module 'SQLPS', 
but the module could not be loaded. For more information, run 'Import-Module 
SQLPS'. 

Yet another developer can successfully run the command, and we both have a local

I cannot run Import-Module sqlps on my machine for obvious reasons - ExecutionPolicy is Restricted.

I am not sure why the other developers machine allows the query, while mine does not. Is this a SQL Client install issue?

Answer

Navid Golforoushan picture Navid Golforoushan · Jul 31, 2017

It is all about Microsoft. They have the best technology but not simply one way and straight path to use their tool. :( You can use SMO object to Interact with your database. (You can use it with C#/VB.net/PowerShell) :$ You can use SQLPS You can use Invoke-SQLCmd

Just Run below Script to make sure feature pack installed in your machine and Use SQLPS which is easier to use than other solution (personal preference)

Install-module -Name SqlServer -Scope CurrentUser

# Import the SQL Server Module.    
Import-Module Sqlps -DisableNameChecking;

# To check whether the module is installed.
Get-Module -ListAvailable -Name Sqlps;
cd SQLSERVER:\SQL