简体   繁体   English

从C#使用Invoke-SqlCmd cmdlet执行PowerShell脚本

[英]Executing PowerShell Script with Invoke-SqlCmd cmdlet from C#

I need to execute a simple PS script that contains a Invoke-Sqlcmd cmdlet from a C# app. 我需要执行一个简单的PS脚本,其中包含来自C#应用程序的Invoke-Sqlcmd cmdlet。 When the script is executed through the PS window it works fine. 当通过PS窗口执行脚本时,脚本可以正常工作。 In the C# app nothing happens. 在C#应用中,什么都没有发生。

I've tried other scripts from the C# app and got results, but with this specific script something went wrong. 我尝试了C#应用程序中的其他脚本并获得了结果,但是使用此特定脚本时出现了问题。

using (var powerShell = PowerShell.Create())
            {
                powerShell.AddScript(psScript);
                powerShell.AddParameter("Username", "user");
                powerShell.AddParameter("Password", "password");
                powerShell.AddParameter("Server", server);
                powerShell.AddParameter("Script", script);

                var result = powerShell.Invoke();
            }

PS script: PS脚本:

param ([String]$Username, [String]$Password, [String]$Server, [String]$Script)

Import-Module SqlPs 导入模块SqlPs

Invoke-Sqlcmd -ServerInstance $Server -Username $Username -Password $Password -Query $Script -QueryTimeout 750 -ConnectionTimeout 600 调用Sqlcmd -ServerInstance $ Server -Username $ Username -Password $ Password -Query $ Script -QueryTimeout 750 -ConnectionTimeout 600

Does anyone know how to solve the problem? 有人知道如何解决问题吗?

After adding some some error logging code to your function: 将一些错误记录代码添加到函数后:

        foreach (var error in powerShell.Streams.Error)
        {
           Console.WriteLine("Error: {0}", error);
        }

It printed an issue saying your parameters were not reaching the script. 它打印出一个问题,表明您的参数未达到脚本的水平。 After some digging around I found this SO post on how to pass parameters to Powershell: 经过一番挖掘之后,我发现了这篇关于如何将参数传递给Powershell的文章:

Execute PowerShell Script from C# with Commandline Arguments 使用命令行参数从C#执行PowerShell脚本

Cut down version of that solution here: 在此处缩小该解决方案的版本:

        using (Runspace runspace = RunspaceFactory.CreateRunspace(RunspaceConfiguration.Create()))
        {
            runspace.Open();

            using (Pipeline pipeline = runspace.CreatePipeline())
            {
                Command scriptCommand = new Command(psScript);
                scriptCommand.Parameters.Add(new CommandParameter("Username", "user"));
                scriptCommand.Parameters.Add(new CommandParameter("Password", "password"));
                scriptCommand.Parameters.Add(new CommandParameter("Server", server));
                scriptCommand.Parameters.Add(new CommandParameter("Script", script));
                pipeline.Commands.Add(scriptCommand);

                var results = pipeline.Invoke();

                foreach (var item in results)
                {
                    Console.WriteLine("Output: {0}", item);
                }
            }
        }

As mentioned by @bymyself, if your running into issues with exception: 如@bymyself所述,如果您遇到异常问题:

Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.

Try adding this to your app.config (for a Console app): 尝试将其添加到您的app.config中(对于控制台应用程序):

<startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup>

If your using MSTest, try the solution at: http://reedcopsey.com/2011/09/15/setting-uselegacyv2runtimeactivationpolicy-at-runtime/ 如果您使用的是MSTest,请尝试以下解决方案: http ://reedcopsey.com/2011/09/15/setting-uselegacyv2runtimeactivationpolicy-at-runtime/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM