1

I'm using Powershell 4 to install SQL 2014. everything goes ok except at the very end where I have a function that will run a script from a .sql file using invoke-sqlcmd. I get the following error:

"The term 'invoke-sqlcmd' is not recognized as the name of a cmdlet, function, script file..."

If I try and import the sqlps module I get:

The specified module 'sqlps' was not loaded because no valid module file was found in any module directory.

But here's the kicker. If I open a separate PowerShell terminal, IT WORKS THERE. :/ and continues to fail in the initial terminal.

I'm trying to understand why this is so any assistance would be greatly appreciated. I'd like to avoid writing in a reboot once script.

Thanks, Dan

ScubaManDan
  • 809
  • 8
  • 22

1 Answers1

2

Existing Powershell session isn't aware about Sql's modules that were just installed. Take a look at environment variable $env:PSModulePath. Compare new shell's variable to existing and you should see a missing path like ...\Microsoft SQL Server\110\Tools\PowerShell\Modules\.

For a work-around, modify the path to include module dir. Like so,

$env:PSModulePath += ";c:\some\path\to\sql"
vonPryz
  • 22,996
  • 7
  • 54
  • 65
  • Genius!! Thanks. Worked like a charm. :) Though I wish there was a way to simply refresh the session as I'll have to now put something in place to find out what directory the module is in (e.g. SQL 2014 is in 120 while 2012 is in 110). – ScubaManDan Apr 10 '15 at 14:56
  • Aha... turns out the path I need to add is in an environment variable. I guess that's what the $env is (kinda new to PowerShell), but as you say it just hasn't been updated in the PS session since SQL Server was installed. So I don't need to worry about any folders. I can just update the session environment variable to the actual and current environment variable. Found this: http://www.powershellmagazine.com/2013/06/06/pstip-refresh-the-psmodulepath-environment-variable-without-re-opening-console/ – ScubaManDan Apr 10 '15 at 15:20