Powershell hack: import-module sqlps prompt bug and suppress verbose output

When importing sqlps module implicitly you sometimes get the prompt changed to
sqlserver:> in your powershell session.
This can be hacked by explicitly loading sqlps in your script.
Since this hogs your verbosity log you have the option to disable it with the output stream redirection hack used below.
This script uses a defined path which is neccessary if running on teamteamcity where sqlps is not installed.


# begin hack because in some cases invoke-sql is bugged, leaving prompt hanging in sqlserver:>
try
{
 write-verbose "Importing sqlps"
 $psmodulepath = join-path $PsScriptRoot MsBuild\Powershell\Modules\SQLPS
 Push-Location
 Import-Module -name $psmodulepath -DisableNameChecking 4>$null;
 #use line below to load where sqlps exists on executing machine:
 #Import-Module sqlps -DisableNameChecking 4>$null;
 Pop-Location #reset invalid prompt location
}
catch { write-error ("There was an error importing sqlps: {0}" -f $_.Exception ) }
# end hack

Comments

Popular posts from this blog

SSIS: Set parent package variable from child package

How to decrypt stored password from SSMS registered servers

Insert bulk statement does not support recompile (SQL 2017)