PowerShell Invoke-Sqlcmd switches into sqlps session

前端 未结 3 1437
梦如初夏
梦如初夏 2021-02-12 09:56

I am writing a script in PowerShell ISE and I am using Invoke-Sqlcmd. After the command is executed the Powershell session switches into sqlps session (PS SQLSERVER:>) and I can

3条回答
  •  鱼传尺愫
    2021-02-12 10:39

    The sqlps module's behavior is to leave you in the psdrive that it creates. I'm fairly certain that people have asked Microsoft to fix this, as it's very annoying and disruptive.

    The automatic importing of modules introduced by PowerShell 3.0 makes this even more annoying, because you may not even realize that you're importing the module until after the fact.

    When I use sqlps, I explicitly import it so that I can control my working directory as follows:

    push-location
    import-module sqlps -disablenamechecking
    pop-location
    

    This returns you to your previous directory after the module is loaded.

    Very late edit: With the advent of SQL Server Management Studio 2016 we have a new PowerShell module, sqlserver, which supersedes sqlps and resolves this problem.

提交回复
热议问题