尝试创建脚本文件作为单独的命令:
Command myCommand = new Command(scriptfile);
然后您可以添加参数
CommandParameter testParam = new CommandParameter("key","value");myCommand.Parameters.Add(testParam);最后
pipeline.Commands.Add(myCommand);
这是完整的,已编辑的代码:
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);runspace.Open();RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);Pipeline pipeline = runspace.CreatePipeline();//Here's how you add a new script with argumentsCommand myCommand = new Command(scriptfile);CommandParameter testParam = new CommandParameter("key","value");myCommand.Parameters.Add(testParam);pipeline.Commands.Add(myCommand);// Execute PowerShell scriptresults = pipeline.Invoke();


