Powershell忽略通过SessionStateProxy.SetVariable传递的参数 [英] Powershell ignores parameter passed via SessionStateProxy.SetVariable
问题描述
我有以下Powershell脚本.
param([String]$stepx="Not Working")
echo $stepx
然后我尝试使用以下C#将参数传递给此脚本.
using (Runspace space = RunspaceFactory.CreateRunspace())
{
space.Open();
space.SessionStateProxy.SetVariable("stepx", "This is a test");
Pipeline pipeline = space.CreatePipeline();
pipeline.Commands.AddScript("test.ps1");
var output = pipeline.Invoke();
}
运行上述代码段后,输出变量中的值为不起作用".应该是这是一个测试".为什么忽略该参数?
谢谢
您正在将$stepx
定义为变量,这与将值传递给脚本的$stepx
不同参数.
变量的存在与参数无关,并且由于您没有将 argument 传递给脚本,因此其参数将绑定为其默认值.
因此,您需要将参数(参数值)传递给脚本的参数:
有些令人困惑的是,脚本文件是通过Command
实例调用的,您可以通过其.Parameters
集合向其中传递参数(参数值).>
相反, .AddScript()
用于添加字符串作为 in-memory 脚本(存储在字符串中)的 contents ),即 PowerShell源代码的片段 .
您可以使用 两种技术来调用带有参数的脚本 file ,尽管如果您想使用强类型的参数(其值不能(从其字符串表示形式中明确推断出),请使用基于Command
的方法(注释中提到了.AddScript()
替代方法):
using (Runspace space = RunspaceFactory.CreateRunspace())
{
space.Open();
Pipeline pipeline = space.CreatePipeline();
// Create a Command instance that runs the script and
// attach a parameter (value) to it.
// Note that since "test.ps1" is referenced without a path, it must
// be located in a dir. listed in $env:PATH
var cmd = new Command("test.ps1");
cmd.Parameters.Add("stepx", "This is a test");
// Add the command to the pipeline.
pipeline.Commands.Add(cmd);
// Note: Alternatively, you could have constructed the script-file invocation
// as a string containing a piece of PowerShell code as follows:
// pipeline.Commands.AddScript("test.ps1 -stepx 'This is a test'");
var output = pipeline.Invoke(); // output[0] == "This is a test"
}
I have the following Powershell script.
param([String]$stepx="Not Working")
echo $stepx
I then try using the following C# to pass a parameter to this script.
using (Runspace space = RunspaceFactory.CreateRunspace())
{
space.Open();
space.SessionStateProxy.SetVariable("stepx", "This is a test");
Pipeline pipeline = space.CreatePipeline();
pipeline.Commands.AddScript("test.ps1");
var output = pipeline.Invoke();
}
After the above code snippet is run, the value "not working" is in the output variable. It should be "This is a test". Why is that parameter ignored?
Thanks
You're defining $stepx
as a variable, which is not the same as passing a value to your script's $stepx
parameter.
The variable exists independently of the parameter, and since you're not passing an argument to your script, its parameter is bound to its default value.
Therefore, you need to pass an argument (parameter value) to your script's parameter:
Somewhat confusingly, a script file is invoked via a Command
instance, to which you pass arguments (parameter values) via its .Parameters
collection.
By contrast, .AddScript()
is used to add a string as the contents of an in-memory script (stored in a string), i.e., a snippet of PowerShell source code.
You can use either technique to invoke a script file with parameters, though if you want to use strongly typed arguments (whose values cannot be unambiguously inferred from their string representations), use the Command
-based approach (the .AddScript()
alternative is mentioned in comments):
using (Runspace space = RunspaceFactory.CreateRunspace())
{
space.Open();
Pipeline pipeline = space.CreatePipeline();
// Create a Command instance that runs the script and
// attach a parameter (value) to it.
// Note that since "test.ps1" is referenced without a path, it must
// be located in a dir. listed in $env:PATH
var cmd = new Command("test.ps1");
cmd.Parameters.Add("stepx", "This is a test");
// Add the command to the pipeline.
pipeline.Commands.Add(cmd);
// Note: Alternatively, you could have constructed the script-file invocation
// as a string containing a piece of PowerShell code as follows:
// pipeline.Commands.AddScript("test.ps1 -stepx 'This is a test'");
var output = pipeline.Invoke(); // output[0] == "This is a test"
}
这篇关于Powershell忽略通过SessionStateProxy.SetVariable传递的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!