首页 > 解决方案 > 如何使用 System.Management.Automation.Runspaces.Command 从 powershell 文件运行命令

问题描述

我正在尝试使用System.Management.Automation.Runspaces.Pipeline类从 powershell 文件运行命令。

当我将脚本直接作为字符串传递时它可以工作

    Runspace runspace = RunspaceFactory.CreateRunspace();
    runspace.Open();
    Pipeline pipeline = runspace.CreatePipeline();

    pipeline.Commands.AddScript(script);
    
    List<PSObject> results = pipeline.Invoke().ToArray().ToList();

但我不知道如何从 powershell 文件运行命令。基本上我想从powershell文件运行脚本

我也尝试检查System.Management.Automation.Runspaces.Command类的方法,但在那里找不到任何有用的东西。

有人可以帮忙吗?

标签: c#powershell

解决方案


推荐阅读