首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用命令行参数从C#执行PowerShell脚本

使用命令行参数从C#执行PowerShell脚本
EN

Stack Overflow用户
提问于 2009-02-09 09:21:34
回答 7查看 232.9K关注 0票数 120

我需要从C#中执行一个PowerShell脚本。脚本需要命令行参数。

这就是我到目前为止所做的:

代码语言:javascript
复制
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();

Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();

RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);

Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.Add(scriptFile);

// Execute PowerShell script
results = pipeline.Invoke();

scriptFile包含类似于"C:\Program Files\MyProgram\Whatever.ps1“的内容。

脚本使用命令行参数,例如"-key Value“,而Value可以是类似于路径的内容,也可以包含空格。

我不能让它起作用。有人知道如何在C#中将命令行参数传递给PowerShell脚本并确保空格不成问题吗?

EN

回答 7

Stack Overflow用户

发布于 2010-12-24 00:43:04

我有另一个解决方案。我只想测试一下执行PowerShell脚本是否成功,因为可能会有人更改策略。作为参数,我只指定要执行的脚本的路径。

代码语言:javascript
复制
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"powershell.exe";
startInfo.Arguments = @"& 'c:\Scripts\test.ps1'";
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();

string output = process.StandardOutput.ReadToEnd();
Assert.IsTrue(output.Contains("StringToBeVerifiedInAUnitTest"));

string errors = process.StandardError.ReadToEnd();
Assert.IsTrue(string.IsNullOrEmpty(errors));

脚本的内容是:

代码语言:javascript
复制
$someVariable = "StringToBeVerifiedInAUnitTest"
$someVariable
票数 46
EN

Stack Overflow用户

发布于 2009-12-11 05:03:06

向Commands.AddScript方法传递参数时遇到了问题。

代码语言:javascript
复制
C:\Foo1.PS1 Hello World Hunger
C:\Foo2.PS1 Hello World

scriptFile = "C:\Foo1.PS1"

parameters = "parm1 parm2 parm3" ... variable length of params

我通过将null作为名称和参数作为值传递到CommandParameters的集合中来解决这个问题

下面是我的函数:

代码语言:javascript
复制
private static void RunPowershellScript(string scriptFile, string scriptParameters)
{
    RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
    Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
    runspace.Open();
    RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
    Pipeline pipeline = runspace.CreatePipeline();
    Command scriptCommand = new Command(scriptFile);
    Collection<CommandParameter> commandParameters = new Collection<CommandParameter>();
    foreach (string scriptParameter in scriptParameters.Split(' '))
    {
        CommandParameter commandParm = new CommandParameter(null, scriptParameter);
        commandParameters.Add(commandParm);
        scriptCommand.Parameters.Add(commandParm);
    }
    pipeline.Commands.Add(scriptCommand);
    Collection<PSObject> psObjects;
    psObjects = pipeline.Invoke();
}
票数 11
EN

Stack Overflow用户

发布于 2015-09-11 23:43:57

我的更小,更简单:

代码语言:javascript
复制
/// <summary>
/// Runs a PowerShell script taking it's path and parameters.
/// </summary>
/// <param name="scriptFullPath">The full file path for the .ps1 file.</param>
/// <param name="parameters">The parameters for the script, can be null.</param>
/// <returns>The output from the PowerShell execution.</returns>
public static ICollection<PSObject> RunScript(string scriptFullPath, ICollection<CommandParameter> parameters = null)
{
    var runspace = RunspaceFactory.CreateRunspace();
    runspace.Open();
    var pipeline = runspace.CreatePipeline();
    var cmd = new Command(scriptFullPath);
    if (parameters != null)
    {
        foreach (var p in parameters)
        {
            cmd.Parameters.Add(p);
        }
    }
    pipeline.Commands.Add(cmd);
    var results = pipeline.Invoke();
    pipeline.Dispose();
    runspace.Dispose();
    return results;
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/527513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档