Pass 3 parameter to powershell function can not show separate parameter [duplicate]
up vote
1
down vote
favorite
This question already has an answer here:
How do I pass multiple parameters into a function in PowerShell?
14 answers
I want to pass 3 parameter from c# to powershell function but the output it show all parameter in same line. How to fix it ?
This is powershell code
param (
[string] $param1,
[string] $param2,
[string] $param3
)
# begin
function AddStuff($x, $y ,$z)
{
"x: $x;"
"y: $y;"
"z: $z;"
}
AddStuff $param1, $param2, $param3
# end
This is c# code.
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
Pipeline pipeline = runspace.CreatePipeline();
string scriptfile = @"C:Userstest2.ps1";
Command myCommand = new Command(scriptfile);
CommandParameter testParam1 = new CommandParameter("param1", "paramvalue1");
CommandParameter testParam2 = new CommandParameter("param2", "paramvalue2");
CommandParameter testParam3 = new CommandParameter("param3", "paramvalue3");
myCommand.Parameters.Add(testParam1);
myCommand.Parameters.Add(testParam2);
myCommand.Parameters.Add(testParam3);
pipeline.Commands.Add(myCommand);
Collection<PSObject> results = pipeline.Invoke();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
MessageBox.Show(stringBuilder.ToString());
when I run it show MessageBox like this.
x: paramvalue1 paramvalue2 paramvalue3;
y:
z:
That is wrong. Why it not show like this.
x: paramvalue1 ;
y: paramvalue2 ;
z: paramvalue3 ;
powershell syntax
marked as duplicate by mklement0
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 17:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
1
down vote
favorite
This question already has an answer here:
How do I pass multiple parameters into a function in PowerShell?
14 answers
I want to pass 3 parameter from c# to powershell function but the output it show all parameter in same line. How to fix it ?
This is powershell code
param (
[string] $param1,
[string] $param2,
[string] $param3
)
# begin
function AddStuff($x, $y ,$z)
{
"x: $x;"
"y: $y;"
"z: $z;"
}
AddStuff $param1, $param2, $param3
# end
This is c# code.
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
Pipeline pipeline = runspace.CreatePipeline();
string scriptfile = @"C:Userstest2.ps1";
Command myCommand = new Command(scriptfile);
CommandParameter testParam1 = new CommandParameter("param1", "paramvalue1");
CommandParameter testParam2 = new CommandParameter("param2", "paramvalue2");
CommandParameter testParam3 = new CommandParameter("param3", "paramvalue3");
myCommand.Parameters.Add(testParam1);
myCommand.Parameters.Add(testParam2);
myCommand.Parameters.Add(testParam3);
pipeline.Commands.Add(myCommand);
Collection<PSObject> results = pipeline.Invoke();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
MessageBox.Show(stringBuilder.ToString());
when I run it show MessageBox like this.
x: paramvalue1 paramvalue2 paramvalue3;
y:
z:
That is wrong. Why it not show like this.
x: paramvalue1 ;
y: paramvalue2 ;
z: paramvalue3 ;
powershell syntax
marked as duplicate by mklement0
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 17:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
In PowerShell, functions are invoked like shell commands -foo arg1 arg2
- not like C# methods -foo(arg1, arg2)
; seeGet-Help about_Parsing
. If you accidentally use,
to separate arguments, you'll construct an array that a function sees as a single argument. To prevent accidental use of method syntax, useSet-StrictMode -Version 2
or higher, but note its side effects.
– mklement0
Nov 22 at 17:39
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
This question already has an answer here:
How do I pass multiple parameters into a function in PowerShell?
14 answers
I want to pass 3 parameter from c# to powershell function but the output it show all parameter in same line. How to fix it ?
This is powershell code
param (
[string] $param1,
[string] $param2,
[string] $param3
)
# begin
function AddStuff($x, $y ,$z)
{
"x: $x;"
"y: $y;"
"z: $z;"
}
AddStuff $param1, $param2, $param3
# end
This is c# code.
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
Pipeline pipeline = runspace.CreatePipeline();
string scriptfile = @"C:Userstest2.ps1";
Command myCommand = new Command(scriptfile);
CommandParameter testParam1 = new CommandParameter("param1", "paramvalue1");
CommandParameter testParam2 = new CommandParameter("param2", "paramvalue2");
CommandParameter testParam3 = new CommandParameter("param3", "paramvalue3");
myCommand.Parameters.Add(testParam1);
myCommand.Parameters.Add(testParam2);
myCommand.Parameters.Add(testParam3);
pipeline.Commands.Add(myCommand);
Collection<PSObject> results = pipeline.Invoke();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
MessageBox.Show(stringBuilder.ToString());
when I run it show MessageBox like this.
x: paramvalue1 paramvalue2 paramvalue3;
y:
z:
That is wrong. Why it not show like this.
x: paramvalue1 ;
y: paramvalue2 ;
z: paramvalue3 ;
powershell syntax
This question already has an answer here:
How do I pass multiple parameters into a function in PowerShell?
14 answers
I want to pass 3 parameter from c# to powershell function but the output it show all parameter in same line. How to fix it ?
This is powershell code
param (
[string] $param1,
[string] $param2,
[string] $param3
)
# begin
function AddStuff($x, $y ,$z)
{
"x: $x;"
"y: $y;"
"z: $z;"
}
AddStuff $param1, $param2, $param3
# end
This is c# code.
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
Pipeline pipeline = runspace.CreatePipeline();
string scriptfile = @"C:Userstest2.ps1";
Command myCommand = new Command(scriptfile);
CommandParameter testParam1 = new CommandParameter("param1", "paramvalue1");
CommandParameter testParam2 = new CommandParameter("param2", "paramvalue2");
CommandParameter testParam3 = new CommandParameter("param3", "paramvalue3");
myCommand.Parameters.Add(testParam1);
myCommand.Parameters.Add(testParam2);
myCommand.Parameters.Add(testParam3);
pipeline.Commands.Add(myCommand);
Collection<PSObject> results = pipeline.Invoke();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
MessageBox.Show(stringBuilder.ToString());
when I run it show MessageBox like this.
x: paramvalue1 paramvalue2 paramvalue3;
y:
z:
That is wrong. Why it not show like this.
x: paramvalue1 ;
y: paramvalue2 ;
z: paramvalue3 ;
This question already has an answer here:
How do I pass multiple parameters into a function in PowerShell?
14 answers
powershell syntax
powershell syntax
edited Nov 22 at 17:43
mklement0
124k20239266
124k20239266
asked Nov 22 at 17:10
user572575
32811236
32811236
marked as duplicate by mklement0
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 17:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by mklement0
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 17:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
In PowerShell, functions are invoked like shell commands -foo arg1 arg2
- not like C# methods -foo(arg1, arg2)
; seeGet-Help about_Parsing
. If you accidentally use,
to separate arguments, you'll construct an array that a function sees as a single argument. To prevent accidental use of method syntax, useSet-StrictMode -Version 2
or higher, but note its side effects.
– mklement0
Nov 22 at 17:39
add a comment |
In PowerShell, functions are invoked like shell commands -foo arg1 arg2
- not like C# methods -foo(arg1, arg2)
; seeGet-Help about_Parsing
. If you accidentally use,
to separate arguments, you'll construct an array that a function sees as a single argument. To prevent accidental use of method syntax, useSet-StrictMode -Version 2
or higher, but note its side effects.
– mklement0
Nov 22 at 17:39
In PowerShell, functions are invoked like shell commands -
foo arg1 arg2
- not like C# methods - foo(arg1, arg2)
; see Get-Help about_Parsing
. If you accidentally use ,
to separate arguments, you'll construct an array that a function sees as a single argument. To prevent accidental use of method syntax, use Set-StrictMode -Version 2
or higher, but note its side effects.– mklement0
Nov 22 at 17:39
In PowerShell, functions are invoked like shell commands -
foo arg1 arg2
- not like C# methods - foo(arg1, arg2)
; see Get-Help about_Parsing
. If you accidentally use ,
to separate arguments, you'll construct an array that a function sees as a single argument. To prevent accidental use of method syntax, use Set-StrictMode -Version 2
or higher, but note its side effects.– mklement0
Nov 22 at 17:39
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
In your PowerShell script, do not use commas to separate arguments when you call AddStuff:
AddStuff $param1 $param2 $param3
When you do
AddStuff $param1, $param2, $param3
you are actually creating a PowerShell array that contains $param1, $param2, and $param3; this array is then passed to the first parameter ($x) of AddStuff, which is why you got the unexpected output.
If you test your PowerShell script first in e.g., PowerShell ISE, you can spot these sort of problems much more easily, before worrying about how to call the script from C#.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
In your PowerShell script, do not use commas to separate arguments when you call AddStuff:
AddStuff $param1 $param2 $param3
When you do
AddStuff $param1, $param2, $param3
you are actually creating a PowerShell array that contains $param1, $param2, and $param3; this array is then passed to the first parameter ($x) of AddStuff, which is why you got the unexpected output.
If you test your PowerShell script first in e.g., PowerShell ISE, you can spot these sort of problems much more easily, before worrying about how to call the script from C#.
add a comment |
up vote
2
down vote
accepted
In your PowerShell script, do not use commas to separate arguments when you call AddStuff:
AddStuff $param1 $param2 $param3
When you do
AddStuff $param1, $param2, $param3
you are actually creating a PowerShell array that contains $param1, $param2, and $param3; this array is then passed to the first parameter ($x) of AddStuff, which is why you got the unexpected output.
If you test your PowerShell script first in e.g., PowerShell ISE, you can spot these sort of problems much more easily, before worrying about how to call the script from C#.
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
In your PowerShell script, do not use commas to separate arguments when you call AddStuff:
AddStuff $param1 $param2 $param3
When you do
AddStuff $param1, $param2, $param3
you are actually creating a PowerShell array that contains $param1, $param2, and $param3; this array is then passed to the first parameter ($x) of AddStuff, which is why you got the unexpected output.
If you test your PowerShell script first in e.g., PowerShell ISE, you can spot these sort of problems much more easily, before worrying about how to call the script from C#.
In your PowerShell script, do not use commas to separate arguments when you call AddStuff:
AddStuff $param1 $param2 $param3
When you do
AddStuff $param1, $param2, $param3
you are actually creating a PowerShell array that contains $param1, $param2, and $param3; this array is then passed to the first parameter ($x) of AddStuff, which is why you got the unexpected output.
If you test your PowerShell script first in e.g., PowerShell ISE, you can spot these sort of problems much more easily, before worrying about how to call the script from C#.
edited Nov 22 at 17:25
answered Nov 22 at 17:16
Polyfun
7,51842736
7,51842736
add a comment |
add a comment |
In PowerShell, functions are invoked like shell commands -
foo arg1 arg2
- not like C# methods -foo(arg1, arg2)
; seeGet-Help about_Parsing
. If you accidentally use,
to separate arguments, you'll construct an array that a function sees as a single argument. To prevent accidental use of method syntax, useSet-StrictMode -Version 2
or higher, but note its side effects.– mklement0
Nov 22 at 17:39