PowerShell Invoke-Sqlcmd switches into sqlps session

时光毁灭记忆、已成空白 提交于 2019-12-03 09:45:56

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.

A simple cd 'directory of your choice' will put you back to the previous directory you were in.

PS SQLSERVER:\> cd D:

Result:

PS D:\>
Tim Ferrill

There was a similar question over at SuperUser today: https://superuser.com/questions/767427/stuck-in-powershell-sqlserver

You should be able to just go back to C:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!