2

I want to automate my Database creation. there are three Databases to create, I have a different powershell script for each DB creation. Now upon this powershell script i have one more layer batch file this batch file will invokes powershell script. say @"D:\Parent\Sub\InstallDB1.cmd"; will invoke @"D:\Parent\Powerscript1.ps1 like wise two other. Now i have single batch file say FinalDB.cmd. The batch file FinalDB.cmd. will invoke three command scripts one after the other will internally call powershell script.

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.

I cant avoid the above scenario because of my application design.

If I run the Final script manually by double clicking, the DB creation process happening without any fail.But failing when i use following C# code to invoke the 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;
    }

When i invoke using the above code i am getting the error "Invoke-Sqlcmd' is not recognized as the name of a cmdlet, function, script file, or operable program", which was not happening when i run my FinalDB.cmd file manually.

I have done the my googling it seems none of the suggested approaches are working.

Any help to fix the issue?Why does the error is coming when i use C# code.

thanks

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Srikanth
  • 980
  • 3
  • 16
  • 30
  • 1
    Just so we're clear - you have a C# app calling a series of `cmd` scripts which in turn call one or more `PowerShell scripts? Have you considered refactoring this into something a less [Rube Goldberg](http://en.wikipedia.org/wiki/Rube_Goldberg)-ian? I'm sure it would make youre life easier. – alroc Feb 27 '14 at 14:45
  • You say "*I have done the my googling it seems none of the suggested approaches are working*". You need to tell us what you have tried in more detail, so we don't repeat the same advice. – RBarryYoung Feb 27 '14 at 15:06
  • As i was saying @Rube Goldberg _I cant avoid the above scenario because of my application design_. RBarryYoung : following are best answers i felt [Link](http://social.technet.microsoft.com/Forums/windowsserver/en-US/309da0e1-e7eb-41a3-830d-3b14676635ee/invokesqlcmd-windows7?forum=winserverpowershell) [link](http://dbamohsin.wordpress.com/2012/01/23/powershell-invoke-sqlcmd-command-not-recognized/) [Link](http://blogs.msdn.com/b/mwories/archive/2008/06/14/sql2008_5f00_powershell.aspx) Now any other insights. – Srikanth Feb 28 '14 at 04:59
  • @RBarryYoung please find the updated comment. – Srikanth Feb 28 '14 at 05:00
  • Does this answer your question? [Invoke-Sqlcmd' is not recognized as the name of a cmdlet](https://stackoverflow.com/questions/37825030/invoke-sqlcmd-is-not-recognized-as-the-name-of-a-cmdlet) – Michael Freidgeim May 11 '21 at 01:22

3 Answers3

1

Invoke-SQLCmd is a cmdlet provided either by the snap-in SqlServerCmdletSnapin100 (prior to SQL Server 2012) or module SQLPS (SQL Server 2012+). You need one or the other loaded into PowerShell (for example, at the beginning of your script) before you can call the cmdlet.

alroc
  • 27,574
  • 6
  • 51
  • 97
0

Here is the solution i found my self:

It seems some of the components related to sql server is not installed so i followed the following steps.

Step1:

Open Visual studio Command prompt:

If you have Visual Studio installed on your computer: On the taskbar, click Start, click All Programs, click Visual Studio, click

Visual Studio Tools, and then click Visual Studio Command Prompt.

Step2:

Search the dll file Microsoft.SqlServer.Management.PSProvider.dll in your c: drive which I found under C:\Program Files (x86)\Microsoft SQL Server\100\Tools\PowerShell\Modules\SQLPS

or it may be under different path

Copy following dlls and paste under the location where your visual studio command window is pointing. So that you can run the

installUtil command from your visual studio command window

Microsoft.SqlServer.Management.PSProvider.dll

Microsoft.SqlServer.Management.PSSnapins.dll

Step3:

Run the Following commands from Visual studio Command prompt

installutil Microsoft.SqlServer.Management.PSProvider.dll

installutil Microsoft.SqlServer.Management.PSSnapins.dll

Srikanth
  • 980
  • 3
  • 16
  • 30
0

In my case, I had this error when I had a new development machine. Suddently, a powershell script that worked well started failing. It was because PowerShell Extensions for SQL Server 2008 R2 wasn't installed on the new machine.

Simply, you can download and install it from here. http://www.microsoft.com/en-us/download/details.aspx?id=16978#PowerShell

Andrew Chaa
  • 6,120
  • 2
  • 45
  • 33