![](/img/trans.png)
[英]the term is not recognized as the name of a cmdlet, function, script file, or operable program
[英]'Invoke-Sqlcmd' is not recognized as the name of a cmdlet, function, script file, or operable program
我想自动化我的数据库创建。 有三个数据库要创建,每个数据库创建都有一个不同的powershell脚本。 现在,在此powershell脚本之后,我还有一个分层批处理文件,该批处理文件将调用powershell脚本。 说@“ D:\\ Parent \\ Sub \\ InstallDB1.cmd”; 将会像其他两个一样调用@"D:\\Parent\\Powerscript1.ps1
。现在我有一个批处理文件,如FinalDB.cmd.
批处理文件FinalDB.cmd.
将FinalDB.cmd.
调用三个命令脚本,另一个将在内部调用powershell脚本。
So now the calls in `FinalDB.cmd`
call InstallDB1.cmd //comment: which internally calls Powerscript1.ps1.
call InstallDB2.cmd //comment: which internally calls Powerscript2.ps1.
call InstallDB3.cmd //comment: which internally calls Powerscript3.ps1.
由于我的应用程序设计,我无法避免上述情况。
如果我通过双击手动运行Final脚本,则数据库创建过程不会失败。但是,当我使用以下C#代码调用FinalDB.cmd
时,该过程将FinalDB.cmd
。
public static RunCommand RunCommandInDir(string workingDirectory, string arguments, string executablePath)
{
RunCommand runResults = new RunCommand
{
Output = new StringBuilder(),
Error = new StringBuilder(),
RunException = null
};
try
{
using (Process proc = new Process())
{
proc.StartInfo.FileName = executablePath;
proc.StartInfo.Arguments = arguments;
proc.StartInfo.WorkingDirectory = workingDirectory;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = false;
proc.OutputDataReceived +=
(o, e) => runResults.Output.Append(e.Data).Append(Environment.NewLine);
proc.ErrorDataReceived +=
(o, e) => runResults.Error.Append(e.Data).Append(Environment.NewLine);
proc.Start();
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
proc.WaitForExit();
runResults.ExitCode = proc.ExitCode;
}
}
catch (Exception e)
{
runResults.RunException = e;
}
return runResults;
}
当我使用上述代码调用时,出现错误“ Invoke-Sqlcmd'未识别为cmdlet,函数,脚本文件或可操作程序的名称”,当我手动运行FinalDB.cmd文件时未发生。
我已经完成了谷歌搜索,似乎所有建议的方法都没有用。
解决此问题有帮助吗?为什么我使用C#代码时会出现错误?
谢谢
Invoke-SQLCmd
是由管理单元SqlServerCmdletSnapin100
(SQL Server 2012之前)或模块SQLPS
(SQL Server 2012+)提供的cmdlet。 您需要先将一个或另一个加载到PowerShell中(例如,在脚本的开头),然后才能调用cmdlet。
这是我发现自己的解决方案:
似乎未安装与sql server相关的某些组件,因此我按照以下步骤操作。
第1步:
打开Visual Studio命令提示符:
如果您的计算机上安装了Visual Studio:在任务栏上,单击“开始”,单击“所有程序”,单击“ Visual Studio”,再单击“
单击Visual Studio工具,然后单击Visual Studio命令提示符。
第2步:
在c:驱动器中搜索我在C:\\ Program Files(x86)\\ Microsoft SQL Server \\ 100 \\ Tools \\ PowerShell \\ Modules \\ SQLPS下找到的dll文件Microsoft.SqlServer.Management.PSProvider.dll
或者可能在不同的路径下
复制以下dll,并粘贴到Visual Studio命令窗口指向的位置下。 这样您就可以运行
从Visual Studio命令窗口安装installUtil命令
Microsoft.SqlServer.Management.PSProvider.dll
Microsoft.SqlServer.Management.PSSnapins.dll
第三步:
从Visual Studio命令提示符处运行以下命令
installutil Microsoft.SqlServer.Management.PSProvider.dll
installutil Microsoft.SqlServer.Management.PSSnapins.dll
就我而言,当我有一台新的开发机器时,我遇到了这个错误。 突然,运行良好的Powershell脚本开始失败。 这是因为未在新计算机上安装用于SQL Server 2008 R2的PowerShell扩展。
简单来说,您可以从这里下载并安装它。 http://www.microsoft.com/en-us/download/details.aspx?id=16978#PowerShell
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.