PowerShell Invoke-Sqlcmd切换到sqlps会话 [英] PowerShell Invoke-Sqlcmd switches into sqlps session

查看:139
本文介绍了PowerShell Invoke-Sqlcmd切换到sqlps会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在PowerShell ISE中编写脚本,并且正在使用Invoke-Sqlcmd. 执行命令后,Powershell会话将切换到sqlps会话(PS SQLSERVER:>),并且我第二次无法执行脚本.我必须退出PowerShell ISE并重新启动.

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't execute script for the second time. I have to quit PowerShell ISE and start it again.

所以我的问题是:如何从sqlps切换回常规ps或如何防止Invoke-Sqlcmd切换会话.

So my question is: how to switch back from sqlps to regular ps or how to prevent Invoke-Sqlcmd from switching session.

Invoke-Sqlcmd -ServerInstance $server -Database master -Username $user -Password $password   -InputFile $file `
   -ErrorAction Stop -OutputSqlErrors $true -Variable $variable

这不起作用:

Push-Location
Invoke-Sqlcmd -ServerInstance $server -Database master -Username $user -Password $password   -InputFile $file `
-ErrorAction Stop -OutputSqlErrors $true -Variable $variable
Pop-Location

推荐答案

sqlps模块的行为是将您留在它创建的psdrive中.我可以肯定地说,人们已经要求Microsoft修复此问题,因为它很烦人并且具有破坏性.

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.

PowerShell 3.0引入的模块自动导入使这一点更加烦人,因为您甚至可能直到事后才意识到自己正在导入模块.

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.

当我使用sqlps时,我显式导入了它,以便可以如下控制我的工作目录:

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.

非常晚的随着SQL Server Management Studio 2016的到来,我们有了一个新的PowerShell模块sqlserver,它取代了sqlps并解决了此问题.

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.

这篇关于PowerShell Invoke-Sqlcmd切换到sqlps会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆