Search code examples
sqlsql-serverpowershellsmosqlps

invoke-sqlcmd failing after Automated SQL install with powershell


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


Solution

  • 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"