在 c# 中执行 Powershell 脚本文本,将值传递给参数部分中的参数
Execute Powershell Script text in c#, passing values to arguments in Param Section
我有一个简单的 powershell 脚本
param
(
[Parameter(Mandatory=$true)]
[int]$loop = 2
)
for ($i=0; $i -le $loop; $i++)
{
$v += get-process
}
$v
我想通过C#来执行。我能够执行简单的脚本,但现在当我想将值传递给 $loop 参数时,它说
{"Cannot process command because of one or more missing mandatory
parameters: loop."}
我正在使用以下代码:
using (PowerShell powerShellInstance = PowerShell.Create(RunspaceMode.NewRunspace))
{
powerShellInstance.Runspace = runspace;
powerShellInstance.AddScript(script);
if (parameters != null && parameters.Any())
{
foreach (var parameter in parameters)
{
if (parameter.Type == ParameterType.Int32)
{
int value = Convert.ToInt32(parameter.Value.Trim());
powerShellInstance.AddParameter(parameter.Name.Trim(), value);
}
else
{
powerShellInstance.AddParameter(parameter.Name.Trim(), parameter.Value.Trim());
}
}
}
在这里,我在visual studio的调试模式下看到参数名称是$loop,它的值是通过明确设置的添加参数Api
但是当我调用
时出现上述异常
Collection<PSObject> output = powerShellInstance.Invoke();
不确定,我哪里错了。请帮助
查看定义参数和脚本的其余代码会有所帮助。需要注意的一点是,参数名称实际上是loop
,而不是$loop
。
这里是一些非常简化的代码,显示了这个工作原理。
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;
namespace PSTest
{
class Program
{
static void Main(string[] args)
{
using (PowerShell powerShellInstance = PowerShell.Create(RunspaceMode.NewRunspace))
{
var script = "param($param1) $output = 'testing params in C#:' + $param1; $output";
powerShellInstance.AddScript(script);
powerShellInstance.AddParameter("param1", "ParamsinC#");
Collection<PSObject> PSOutput = powerShellInstance.Invoke();
foreach (PSObject outputItem in PSOutput)
{
if (outputItem != null)
{
Console.WriteLine(outputItem);
}
}
Console.ReadKey();
}
}
}
}
我有一个简单的 powershell 脚本
param
(
[Parameter(Mandatory=$true)]
[int]$loop = 2
)
for ($i=0; $i -le $loop; $i++)
{
$v += get-process
}
$v
我想通过C#来执行。我能够执行简单的脚本,但现在当我想将值传递给 $loop 参数时,它说
{"Cannot process command because of one or more missing mandatory parameters: loop."}
我正在使用以下代码:
using (PowerShell powerShellInstance = PowerShell.Create(RunspaceMode.NewRunspace))
{
powerShellInstance.Runspace = runspace;
powerShellInstance.AddScript(script);
if (parameters != null && parameters.Any())
{
foreach (var parameter in parameters)
{
if (parameter.Type == ParameterType.Int32)
{
int value = Convert.ToInt32(parameter.Value.Trim());
powerShellInstance.AddParameter(parameter.Name.Trim(), value);
}
else
{
powerShellInstance.AddParameter(parameter.Name.Trim(), parameter.Value.Trim());
}
}
}
在这里,我在visual studio的调试模式下看到参数名称是$loop,它的值是通过明确设置的添加参数Api
但是当我调用
时出现上述异常Collection<PSObject> output = powerShellInstance.Invoke();
不确定,我哪里错了。请帮助
查看定义参数和脚本的其余代码会有所帮助。需要注意的一点是,参数名称实际上是loop
,而不是$loop
。
这里是一些非常简化的代码,显示了这个工作原理。
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;
namespace PSTest
{
class Program
{
static void Main(string[] args)
{
using (PowerShell powerShellInstance = PowerShell.Create(RunspaceMode.NewRunspace))
{
var script = "param($param1) $output = 'testing params in C#:' + $param1; $output";
powerShellInstance.AddScript(script);
powerShellInstance.AddParameter("param1", "ParamsinC#");
Collection<PSObject> PSOutput = powerShellInstance.Invoke();
foreach (PSObject outputItem in PSOutput)
{
if (outputItem != null)
{
Console.WriteLine(outputItem);
}
}
Console.ReadKey();
}
}
}
}