gpt4 book ai didi

sql-server - 无法在 PS1 文件中运行 Invoke-Sqlcmd,但可以在控制台上运行

转载 作者:行者123 更新时间:2023-12-03 01:15:37 25 4
gpt4 key购买 nike

我目前在一台装有 SQL Server 2008 的机器上,根据这个问题 (How to execute .sql file using powershell?),我已经安装了 powershell 管理单元以与 SQL Server 交互。

问题是现在我已经安装了它,我可以像这样从命令行运行 Invoke-Sqlcmd。

Invoke-Sqlcmd -inputfile "myTestFile.sql" -serverinstance '.\sql2008';

但是如果我将该行放在 PS1 文件中运行并尝试像这样运行它
powershell ./runSQLscript.ps1

我收到这个错误

The term 'Invoke-Sqlcmd' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At D:\TEMP\powershellTests\runSQLscript.ps1:1 char:14 + Invoke-Sqlcmd <<<< -inputfile "myTestFile.sql" -serverinstance '.\sql2008'; + CategoryInfo : ObjectNotFound: (Invoke-Sqlcmd:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

最佳答案

在您的 PS1 中,您需要 Add-PSSnapin并加载 SQL 管理单元。

每个 Powershell 窗口都是一个尚未加载模块和管理单元的新环境。

关于sql-server - 无法在 PS1 文件中运行 Invoke-Sqlcmd,但可以在控制台上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30633472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com