简体   繁体   English

使用自动化DLL在C#中调用ps脚本文件

[英]Calling ps script file in c# using automation dll

I am trying to call Powershell script file with parameters in C# using the System.Management.Automation like this: 我正在尝试使用System.Management.Automation在C#中使用参数调用Powershell脚本文件,如下所示:

using (var ps = PowerShell.Create())
{
    try
    {
        var script = System.IO.File.ReadAllText("MyPsFile.ps1");
        ps.Commands.AddScript(script);
         ps.AddScript(script);
        ps.AddParameter("MyKey1", value1);
        ps.AddParameter("MyKey2", value2);
        var results = ps.Invoke();
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex.Message);
    }
}

The Powershell file looks like this: Powershell文件如下所示:

function MyFunction {
    Param (
        [Parameter(Mandatory=$true)][string] $MyKey1,
        [Parameter(Mandatory=$true)][string] $MyKey1,
    )
    Process {        
      ..do some stuff
    }
}
MyFunction $args[0] $args[1]

If I run the script file inside Powershell like this: 如果我像这样在Powershell中运行脚本文件:

powershell -file MyPsFile.ps1 "Value1" "Value2"

It works fine. 工作正常。 But calling the file from within C# is not working. 但是从C#中调用文件不起作用。 Any idea why? 知道为什么吗?

So I have updated the code like this 所以我更新了这样的代码

var runspace = RunspaceFactory.CreateRunspace();
                    runspace.Open();

                    var runSpaceInvoker = new RunspaceInvoke(runspace);
                    runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted");

                    // create a pipeline and feed it the script text
                    var pipeline = runspace.CreatePipeline();
                    var command = new Command(@". .\MyScript.ps1");
                    command.Parameters.Add("MyParam1", value1);
                    command.Parameters.Add("MyParam2", value2);
                    pipeline.Commands.Add(command);

                    pipeline.Invoke();
                    runspace.Close();

But I am getting the error Powershell ps1 file “is not recognized as a cmdlet, function, operable program, or script file.” 但是我得到了错误的Powershell ps1文件“未被识别为cmdlet,函数,可操作程序或脚本文件。”

I found this Powershell ps1 file "is not recognized as a cmdlet, function, operable program, or script file." 我发现此Powershell ps1文件“未被识别为cmdlet,函数,可操作程序或脚本文件。”

But it did not solve the problem. 但这并没有解决问题。 Any idea what else could cause this error? 知道还有什么可能导致此错误吗?

Finally I found a solution. 终于我找到了解决方案。 Two things: This code solved the "is not recognized as a cmdlet, function, operable program, or script file bug. Thanks to Problem with calling a powershell function from c# 两件事:这段代码解决了“未被识别为cmdlet,函数,可操作程序或脚本文件的错误。这要归功于从c#调用powershell函数的问题

using (var runspace = RunspaceFactory.CreateRunspace())
{
    try
    {
        var script = File.ReadAllText("MyScript.ps1");
        runspace.Open();
        var ps = PowerShell.Create();
        ps.Runspace = runspace;
        ps.AddScript(script);
        ps.Invoke();
        ps.AddCommand("MyFunction").AddParameters(new Dictionary<string, string>
        {
            { "Param1" , value1},
            { "Param2", value2},
            { "Param3", value3},            
        });

        foreach (var result in ps.Invoke())
        {
        }
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex.Message);
    }
}

Important, I also had to remove the process wrapper attribute from the script file 重要的是,我还必须从脚本文件中删除进程包装器属性。

****MyScripts.ps1

    function MyFunction 
    {
        Param (
            [Parameter(Mandatory=$true)][string] myParam1,        
            [Parameter(Mandatory=$true)][string] myParam2,        
            [Parameter(Mandatory=$true)][string] myParam3,  
        )
        //  Removing the Process wrapper solved the problem, the code didn't run with it 
        Process {     
            //DOSTUFF
        }
    }

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

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