c# - 如何在 C# 项目中添加 script.ps1

标签 c# powershell dll process resources

我在项目资源中添加了 script3.ps1 文件。 我构建了 dll 文件。

我想用 script.ps1 启动一个 powershell 进程。 我试过了

var proc = new Process
{
    StartInfo = new ProcessStartInfo
    {
        FileName = "powershell.exe",
        Arguments = "-ExecutionPolicy ByPass -Command \".'"+MyProject.Properties.Resources.script3+"'",
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = true
    }
};

我收到错误:

the term 'system.byte[]' 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 path is correct and try again. at line : 1 char

最佳答案

先将资源内容保存到文件中:

File.WriteAllBytes("file3.ps", MyProject.Properties.Resources.script3);
var proc = new Process
{
    StartInfo = new ProcessStartInfo
    {
        FileName = "powershell.exe",
        Arguments = "-ExecutionPolicy ByPass -Command \".\\file3.ps\"",
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = true
    }
};

或者如果它的一行只是转换为字符串:

var proc = new Process
{
    StartInfo = new ProcessStartInfo
    {
        FileName = "powershell.exe",
        Arguments = "-ExecutionPolicy ByPass -Command \""+Encoding.Default.GetString(MyProject.Properties.Resources.script3)+"\"",
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = true
    }
};

关于c# - 如何在 C# 项目中添加 script.ps1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31723685/

相关文章:

c# - ValueConverter 的依赖属性

c# - 用于有条件地呈现嵌套 HTML 的简单用户控件

c# - 带有自定义控件的 WPF 内存泄漏

PowerShell 多个动态参数

c# - 在 Entity Framework 中保存新记录

powershell - 文本文件搜索匹配字符串正则表达式

c++ - 在 Visual Studio 中导入 Dll

c - 将函数保密到 C 中的库中

qt - QPluginLoader.instance() - 它是如何工作的?

Powershell:比较两个数组,查找缺失项和更改的值