Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C中的参数调用PowerShell脚本文件#_C#_Powershell - Fatal编程技术网

C# 使用C中的参数调用PowerShell脚本文件#

C# 使用C中的参数调用PowerShell脚本文件#,c#,powershell,C#,Powershell,真的很挣扎。我试过各种不同的方法,但似乎都不管用 -使用addScript:我得到一个错误,告诉我不能用这种方式调用参数,应该使用像ISE这样的UI -使用FilePath参数,我找不到传递参数的正确方法(绑定困难) 这是我尝试过的最新版本,没有错误,但是脚本没有执行,什么都没有发生 非常感谢您的帮助 runspace = RunspaceFactory.CreateRunspace(); runspace.Open(); pipeline = runspace.CreatePipeline()

真的很挣扎。我试过各种不同的方法,但似乎都不管用

-使用addScript:我得到一个错误,告诉我不能用这种方式调用参数,应该使用像ISE这样的UI

-使用FilePath参数,我找不到传递参数的正确方法(绑定困难)

这是我尝试过的最新版本,没有错误,但是脚本没有执行,什么都没有发生

非常感谢您的帮助

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

string script =
@"{param($merchantName, $appType, $gruntDirectory, $merchantInstanceDirectory, $editorConnectionString) "+
_config.MerchantInstance.Directory + @"\Generate_And_Compile_LESS.ps1"
+ " –merchantName $merchantName"
+ " –appType $appType"
+ " –gruntDirectory $gruntDirectory"
+ " -merchantInstanceDirectory $merchantInstanceDirectory"
+ " -editorConnectionString $editorConnectionString }";

Command compileCommand = new Command("Invoke-Command");
compileCommand.Parameters.Add("Scriptblock", ScriptBlock.Create(script));

var args = new List<string>();
args.Add(merchantName);
args.Add(appType.GetHashCode().ToString());
args.Add("'" + _config.Grunt.Directory + "'");
args.Add("'" + _config.MerchantInstance.Directory + "'");
args.Add("'" + _connectionStrings.AppConnectionString + "'");

compileCommand.Parameters.Add("ArgumentList", String.Join(",", args));

pipeline.Commands.Add(compileCommand);
Collection<PSObject> results = pipeline.Invoke();
runspace=RunspaceFactory.CreateRunspace();
Open();
pipeline=runspace.CreatePipeline();
字符串脚本=
@{param($merchantName、$appType、$gruntDirectory、$merchantInstanceDirectory、$editorConnectionString)+
_config.MerchantInstance.Directory+@“\Generate\u And\u Compile\u LESS.ps1”
+“–merchantName$merchantName”
+“–appType$appType”
+“–gruntDirectory$gruntDirectory”
+“-merchantInstanceDirectory$merchantInstanceDirectory”
+“-editorConnectionString$editorConnectionString}”;
Command compileCommand=新命令(“调用命令”);
compileCommand.Parameters.Add(“Scriptblock”,Scriptblock.Create(script));
var args=新列表();
参数添加(商品名称);
Add(appType.GetHashCode().ToString());
args.Add(“'+\u config.Grunt.Directory+”);
args.Add(“'”+_config.MerchantInstance.Directory+”);
args.Add(“+”U ConnectionString.AppConnectionString+“”);
compileCommand.Parameters.Add(“ArgumentList”,String.Join(“,”,args));
pipeline.Commands.Add(compileCommand);
收集结果=pipeline.Invoke();

您可以使用我个人刚刚测试过的代码

static void Main(string[] args)
{
    PowerShell ps = PowerShell.Create();
    ps.AddScript(@"c:\test\test.ps1").AddParameter("param1", "paramvalue1");
    ps.Invoke();
}
这是我的测试脚本,位于
c:\test\test.ps1

[CmdletBinding()]
param (
    [string] $param1
)
Set-Content -Path $PSScriptRoot\test.txt -Value $param1;

仅供参考,请确保启动32位(x86)PowerShell,并将执行策略设置为无限制。Visual Studio是一个32位进程,默认情况下会调用32位PowerShell引擎。

忘了提到,我想使用管道对象,以便捕获脚本中抛出的特定错误。ps=ps.AddScript(…)。AddParameter(…)对我来说效果更好