Invoke-Sqlcmd unable to run
If import-module fails then you'll need to run install-module first. Installing SSMS or SSDT doesn't include sqlserver module by default.
install-module sqlserver
update-module sqlserver
import-module sqlserver
PowerShell v2 doesn't auto-load modules, so you need to load the relevant modules or snapins yourself:
Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100
[Source]
Make sure you've installed Powershell module with SQL Server:
Then try to import module SQLPS:
Import-Module SQLPS
See also: https://blogs.msdn.microsoft.com/psssql/2008/09/02/sql-server-2008-management-tools-basic-vs-complete-explained/
This should help your debugging!
if (-not (Get-Command Invoke-Sqlcmd -ErrorAction SilentlyContinue)) {
Write-Error "Unabled to find Invoke-SqlCmd cmdlet"
}
if (-not (Get-Module -Name SqlServer | Where-Object {$_.ExportedCommands.Count -gt 0})) {
Write-Error "The SqlServer module is not loaded"
}
if (-not (Get-Module -ListAvailable | Where-Object Name -eq SqlServer)) {
Write-Error "Can't find the SqlServer module"
}
Import-Module SqlServer -ErrorAction Stop