Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Hosting PowerShell & passing parameters to script

Reply
 
Old 09-18-2007   #1 (permalink)
Dmitry Naumov


 
 

Hosting PowerShell & passing parameters to script

I am hosting PowerShell in my application. There are number of scripts I want
to execute, some of them have arguments which can't be passed as a string.
For example:

param ([Exception]$e)

echo $e.Message

$MailMessage = New-Object -typename System.Net.Mail.MailMessage
$MailMessage.From = "somebody@xxxxxx"
$MailMessage.To.Add("somebody@xxxxxx")
$MailMessage.Subject = "Message from PowerShell"

$MailMessage.Body += $e.Message + "`n"
$MailMessage.Body += $e.CallStack

$smtp = New-Object -typename System.Net.Mail.SmtpClient
$smtp.Host = "domain.com"
$smtp.Port = 25
$smtp.Send($MailMessage)


It is a simple script but the point is: I want to pass objects to my script,
which can't be represented as string.

If I run this script from PowerShell console it works fine:
PS D:\> $e = New-Object -typename System.IO.FileNotFoundException -argument
"Test"
PS D:\> .\script.ps1 $e
Test

and email is sent. But when I try to run this script in my application
(through RunspaceInvoke or creating pipeline with command) it fails with:

The term 'param' is not recognized as a cmdlet, function, operable program,
or script file. Verify the term and try again.

And I have no idea why it happens.
Any thoughts?
Thanks.


My System SpecsSystem Spec
Old 09-18-2007   #2 (permalink)
Oisin Grehan


 
 

Re: Hosting PowerShell & passing parameters to script

On Sep 18, 7:26 am, Dmitry Naumov <Dmitry
Nau...@xxxxxx> wrote:
Quote:

> I am hosting PowerShell in my application. There are number of scripts I want
> to execute, some of them have arguments which can't be passed as a string.
> For example:
>
> param ([Exception]$e)
>
> echo $e.Message
>
> $MailMessage = New-Object -typename System.Net.Mail.MailMessage
> $MailMessage.From = "someb...@xxxxxx"
> $MailMessage.To.Add("someb...@xxxxxx")
> $MailMessage.Subject = "Message from PowerShell"
>
> $MailMessage.Body += $e.Message + "`n"
> $MailMessage.Body += $e.CallStack
>
> $smtp = New-Object -typename System.Net.Mail.SmtpClient
> $smtp.Host = "domain.com"
> $smtp.Port = 25
> $smtp.Send($MailMessage)
>
> It is a simple script but the point is: I want to pass objects to my script,
> which can't be represented as string.
>
> If I run this script from PowerShell console it works fine:
> PS D:\> $e = New-Object -typename System.IO.FileNotFoundException -argument
> "Test"
> PS D:\> .\script.ps1 $e
> Test
>
> and email is sent. But when I try to run this script in my application
> (through RunspaceInvoke or creating pipeline with command) it fails with:
>
> The term 'param' is not recognized as a cmdlet, function, operable program,
> or script file. Verify the term and try again.
>
> And I have no idea why it happens.
> Any thoughts?
> Thanks.
I'm going to take an educated guess here, but this is probably going
to be fixed in v2. It looks related to the issue whereby you cannot
use "param" in ScriptMethods either, and are forced to use the
automatic variable $args instead. See:

http://groups.google.com/group/micro...17595f7c2cdecf

- Oisin

My System SpecsSystem Spec
Old 09-18-2007   #3 (permalink)
John Vottero


 
 

Re: Hosting PowerShell & passing parameters to script


"Dmitry Naumov" <Dmitry Naumov@xxxxxx> wrote in message
news:1DC6ACB9-3EB6-4D51-861D-BB4960987205@xxxxxx
Quote:

>I am hosting PowerShell in my application. There are number of scripts I
>want
> to execute, some of them have arguments which can't be passed as a string.
> For example:
>
> param ([Exception]$e)
>
> echo $e.Message
>
> $MailMessage = New-Object -typename System.Net.Mail.MailMessage
> $MailMessage.From = "somebody@xxxxxx"
> $MailMessage.To.Add("somebody@xxxxxx")
> $MailMessage.Subject = "Message from PowerShell"
>
> $MailMessage.Body += $e.Message + "`n"
> $MailMessage.Body += $e.CallStack
>
> $smtp = New-Object -typename System.Net.Mail.SmtpClient
> $smtp.Host = "domain.com"
> $smtp.Port = 25
> $smtp.Send($MailMessage)
>
>
> It is a simple script but the point is: I want to pass objects to my
> script,
> which can't be represented as string.
>
> If I run this script from PowerShell console it works fine:
> PS D:\> $e = New-Object -typename
> System.IO.FileNotFoundException -argument
> "Test"
> PS D:\> .\script.ps1 $e
> Test
>
> and email is sent. But when I try to run this script in my application
> (through RunspaceInvoke or creating pipeline with command) it fails with:
>
> The term 'param' is not recognized as a cmdlet, function, operable
> program,
> or script file. Verify the term and try again.
>
> And I have no idea why it happens.
Can you post more detail on how you're creating a pipeline with command? It
works for me when I create the command line this:

Pipeline pipe = runSpace.CreatePipeline();
Command cmd = new Command("C:\Script.ps1");
cmd.Parameters.Add("name", value);
pipe.Commands.Add(cmd);
pipe.Commands.Add("out-default");
pipe.Invoke();



My System SpecsSystem Spec
Old 09-19-2007   #4 (permalink)
Dmitry Naumov


 
 

Re: Hosting PowerShell & passing parameters to script

"John Vottero" wrote:
Quote:

>
> "Dmitry Naumov" <Dmitry Naumov@xxxxxx> wrote in message
> news:1DC6ACB9-3EB6-4D51-861D-BB4960987205@xxxxxx
Quote:

> >I am hosting PowerShell in my application. There are number of scripts I
> >want
> > to execute, some of them have arguments which can't be passed as a string.
> > For example:
> >
> > param ([Exception]$e)
> >
> > echo $e.Message
> >
> > $MailMessage = New-Object -typename System.Net.Mail.MailMessage
> > $MailMessage.From = "somebody@xxxxxx"
> > $MailMessage.To.Add("somebody@xxxxxx")
> > $MailMessage.Subject = "Message from PowerShell"
> >
> > $MailMessage.Body += $e.Message + "`n"
> > $MailMessage.Body += $e.CallStack
> >
> > $smtp = New-Object -typename System.Net.Mail.SmtpClient
> > $smtp.Host = "domain.com"
> > $smtp.Port = 25
> > $smtp.Send($MailMessage)
> >
> >
> > It is a simple script but the point is: I want to pass objects to my
> > script,
> > which can't be represented as string.
> >
> > If I run this script from PowerShell console it works fine:
> > PS D:\> $e = New-Object -typename
> > System.IO.FileNotFoundException -argument
> > "Test"
> > PS D:\> .\script.ps1 $e
> > Test
> >
> > and email is sent. But when I try to run this script in my application
> > (through RunspaceInvoke or creating pipeline with command) it fails with:
> >
> > The term 'param' is not recognized as a cmdlet, function, operable
> > program,
> > or script file. Verify the term and try again.
> >
> > And I have no idea why it happens.
>
> Can you post more detail on how you're creating a pipeline with command? It
> works for me when I create the command line this:
>
> Pipeline pipe = runSpace.CreatePipeline();
> Command cmd = new Command("C:\Script.ps1");
> cmd.Parameters.Add("name", value);
> pipe.Commands.Add(cmd);
> pipe.Commands.Add("out-default");
> pipe.Invoke();
>
>
>
>
John,
There is the difference between your sample and mine. You invoke script
located in file, I try to run script which is located in System.String.
My System SpecsSystem Spec
Old 09-19-2007   #5 (permalink)
Oisin Grehan


 
 

Re: Hosting PowerShell & passing parameters to script

On Sep 19, 1:46 am, Dmitry Naumov
<DmitryNau...@xxxxxx> wrote:
Quote:

> "John Vottero" wrote:
>
Quote:

> > "Dmitry Naumov" <Dmitry Nau...@xxxxxx> wrote in message
> >news:1DC6ACB9-3EB6-4D51-861D-BB4960987205@xxxxxx
Quote:

> > >I am hosting PowerShell in my application. There are number of scripts I
> > >want
> > > to execute, some of them have arguments which can't be passed as a string.
> > > For example:
>
Quote:
Quote:

> > > param ([Exception]$e)
>
Quote:
Quote:

> > > echo $e.Message
>
Quote:
Quote:

> > > $MailMessage = New-Object -typename System.Net.Mail.MailMessage
> > > $MailMessage.From = "someb...@xxxxxx"
> > > $MailMessage.To.Add("someb...@xxxxxx")
> > > $MailMessage.Subject = "Message from PowerShell"
>
Quote:
Quote:

> > > $MailMessage.Body += $e.Message + "`n"
> > > $MailMessage.Body += $e.CallStack
>
Quote:
Quote:

> > > $smtp = New-Object -typename System.Net.Mail.SmtpClient
> > > $smtp.Host = "domain.com"
> > > $smtp.Port = 25
> > > $smtp.Send($MailMessage)
>
Quote:
Quote:

> > > It is a simple script but the point is: I want to pass objects to my
> > > script,
> > > which can't be represented as string.
>
Quote:
Quote:

> > > If I run this script from PowerShell console it works fine:
> > > PS D:\> $e = New-Object -typename
> > > System.IO.FileNotFoundException -argument
> > > "Test"
> > > PS D:\> .\script.ps1 $e
> > > Test
>
Quote:
Quote:

> > > and email is sent. But when I try to run this script in my application
> > > (through RunspaceInvoke or creating pipeline with command) it fails with:
>
Quote:
Quote:

> > > The term 'param' is not recognized as a cmdlet, function, operable
> > > program,
> > > or script file. Verify the term and try again.
>
Quote:
Quote:

> > > And I have no idea why it happens.
>
Quote:

> > Can you post more detail on how you're creating a pipeline with command? It
> > works for me when I create the command line this:
>
Quote:

> > Pipeline pipe = runSpace.CreatePipeline();
> > Command cmd = new Command("C:\Script.ps1");
> > cmd.Parameters.Add("name", value);
> > pipe.Commands.Add(cmd);
> > pipe.Commands.Add("out-default");
> > pipe.Invoke();
>
> John,
> There is the difference between your sample and mine. You invoke script
> located in file, I try to run script which is located in System.String.- Hide quoted text -
>
> - Show quoted text -
Ahh, I misunderstood your original question. Hmm, if you're trying to
invoke that string in an interactive context, param will indeed be
invalid. You can verify this yourself by firing up powershell and
trying to run "param" as a command:

PS C:\> param
The term 'param' is not recognized as a cmdlet, function, operable
program, or script file. Verify the term and try again.
At line:1 char:5
+ param <<<<

If you don't invoke the script as a ps1 file, you won't get implicit
parameter support. It's hard to help you with this without seeing how
you are hosting the runtime.

- Oisin

My System SpecsSystem Spec
Old 09-20-2007   #6 (permalink)
Dmitry Naumov


 
 

Re: Hosting PowerShell & passing parameters to script



"Oisin Grehan" wrote:
Quote:

> On Sep 19, 1:46 am, Dmitry Naumov
> <DmitryNau...@xxxxxx> wrote:
Quote:

> > "John Vottero" wrote:
> >
Quote:

> > > "Dmitry Naumov" <Dmitry Nau...@xxxxxx> wrote in message
> > >news:1DC6ACB9-3EB6-4D51-861D-BB4960987205@xxxxxx
> > > >I am hosting PowerShell in my application. There are number of scripts I
> > > >want
> > > > to execute, some of them have arguments which can't be passed as a string.
> > > > For example:
> >
Quote:

> > > > param ([Exception]$e)
> >
Quote:

> > > > echo $e.Message
> >
Quote:

> > > > $MailMessage = New-Object -typename System.Net.Mail.MailMessage
> > > > $MailMessage.From = "someb...@xxxxxx"
> > > > $MailMessage.To.Add("someb...@xxxxxx")
> > > > $MailMessage.Subject = "Message from PowerShell"
> >
Quote:

> > > > $MailMessage.Body += $e.Message + "`n"
> > > > $MailMessage.Body += $e.CallStack
> >
Quote:

> > > > $smtp = New-Object -typename System.Net.Mail.SmtpClient
> > > > $smtp.Host = "domain.com"
> > > > $smtp.Port = 25
> > > > $smtp.Send($MailMessage)
> >
Quote:

> > > > It is a simple script but the point is: I want to pass objects to my
> > > > script,
> > > > which can't be represented as string.
> >
Quote:

> > > > If I run this script from PowerShell console it works fine:
> > > > PS D:\> $e = New-Object -typename
> > > > System.IO.FileNotFoundException -argument
> > > > "Test"
> > > > PS D:\> .\script.ps1 $e
> > > > Test
> >
Quote:

> > > > and email is sent. But when I try to run this script in my application
> > > > (through RunspaceInvoke or creating pipeline with command) it fails with:
> >
Quote:

> > > > The term 'param' is not recognized as a cmdlet, function, operable
> > > > program,
> > > > or script file. Verify the term and try again.
> >
Quote:

> > > > And I have no idea why it happens.
> >
Quote:

> > > Can you post more detail on how you're creating a pipeline with command? It
> > > works for me when I create the command line this:
> >
Quote:

> > > Pipeline pipe = runSpace.CreatePipeline();
> > > Command cmd = new Command("C:\Script.ps1");
> > > cmd.Parameters.Add("name", value);
> > > pipe.Commands.Add(cmd);
> > > pipe.Commands.Add("out-default");
> > > pipe.Invoke();
> >
> > John,
> > There is the difference between your sample and mine. You invoke script
> > located in file, I try to run script which is located in System.String.- Hide quoted text -
> >
> > - Show quoted text -
>
> Ahh, I misunderstood your original question. Hmm, if you're trying to
> invoke that string in an interactive context, param will indeed be
> invalid. You can verify this yourself by firing up powershell and
> trying to run "param" as a command:
>
> PS C:\> param
> The term 'param' is not recognized as a cmdlet, function, operable
> program, or script file. Verify the term and try again.
> At line:1 char:5
> + param <<<<
>
> If you don't invoke the script as a ps1 file, you won't get implicit
> parameter support. It's hard to help you with this without seeing how
> you are hosting the runtime.
>
> - Oisin
>
>
Actually nothing special in the hosting:

// runspace is created and opened somewhere before...
using (Pipeline pipeline = runspace.CreatePipeline())
{
Command command = new Command(myScript, true);
// Command command = new Command(pathToTempFileWithScript);
pipeline.Commands.Add(command);

foreach (WorkflowParameterBinding parameterBinding in parameterBindings)
{
command.Parameters.Add(parameterBinding.ParameterName,
parameterBinding.Value);
}

pipeline.Invoke();
}

It even works if I save my script to temporary file (commented line), but I
want the same functionality without saving to file.
My System SpecsSystem Spec
Old 09-20-2007   #7 (permalink)
Oisin Grehan


 
 

Re: Hosting PowerShell & passing parameters to script

On Sep 20, 1:56 am, Dmitry Naumov
<DmitryNau...@xxxxxx> wrote:
Quote:

> "Oisin Grehan" wrote:
Quote:

> > On Sep 19, 1:46 am, Dmitry Naumov
> > <DmitryNau...@xxxxxx> wrote:
Quote:

> > > "John Vottero" wrote:
>
Quote:
Quote:

> > > > "Dmitry Naumov" <Dmitry Nau...@xxxxxx> wrote in message
> > > >news:1DC6ACB9-3EB6-4D51-861D-BB4960987205@xxxxxx
> > > > >I am hosting PowerShell in my application. There are number of scripts I
> > > > >want
> > > > > to execute, some of them have arguments which can't be passed as a string.
> > > > > For example:
>
Quote:
Quote:

> > > > > param ([Exception]$e)
>
Quote:
Quote:

> > > > > echo $e.Message
>
Quote:
Quote:

> > > > > $MailMessage = New-Object -typename System.Net.Mail.MailMessage
> > > > > $MailMessage.From = "someb...@xxxxxx"
> > > > > $MailMessage.To.Add("someb...@xxxxxx")
> > > > > $MailMessage.Subject = "Message from PowerShell"
>
Quote:
Quote:

> > > > > $MailMessage.Body += $e.Message + "`n"
> > > > > $MailMessage.Body += $e.CallStack
>
Quote:
Quote:

> > > > > $smtp = New-Object -typename System.Net.Mail.SmtpClient
> > > > > $smtp.Host = "domain.com"
> > > > > $smtp.Port = 25
> > > > > $smtp.Send($MailMessage)
>
Quote:
Quote:

> > > > > It is a simple script but the point is: I want to pass objects to my
> > > > > script,
> > > > > which can't be represented as string.
>
Quote:
Quote:

> > > > > If I run this script from PowerShell console it works fine:
> > > > > PS D:\> $e = New-Object -typename
> > > > > System.IO.FileNotFoundException -argument
> > > > > "Test"
> > > > > PS D:\> .\script.ps1 $e
> > > > > Test
>
Quote:
Quote:

> > > > > and email is sent. But when I try to run this script in my application
> > > > > (through RunspaceInvoke or creating pipeline with command) it fails with:
>
Quote:
Quote:

> > > > > The term 'param' is not recognized as a cmdlet, function, operable
> > > > > program,
> > > > > or script file. Verify the term and try again.
>
Quote:
Quote:

> > > > > And I have no idea why it happens.
>
Quote:
Quote:

> > > > Can you post more detail on how you're creating a pipeline with command? It
> > > > works for me when I create the command line this:
>
Quote:
Quote:

> > > > Pipeline pipe = runSpace.CreatePipeline();
> > > > Command cmd = new Command("C:\Script.ps1");
> > > > cmd.Parameters.Add("name", value);
> > > > pipe.Commands.Add(cmd);
> > > > pipe.Commands.Add("out-default");
> > > > pipe.Invoke();
>
Quote:
Quote:

> > > John,
> > > There is the difference between your sample and mine. You invoke script
> > > located in file, I try to run script which is located in System.String.- Hide quoted text -
>
Quote:
Quote:

> > > - Show quoted text -
>
Quote:

> > Ahh, I misunderstood your original question. Hmm, if you're trying to
> > invoke that string in an interactive context, param will indeed be
> > invalid. You can verify this yourself by firing up powershell and
> > trying to run "param" as a command:
>
Quote:

> > PS C:\> param
> > The term 'param' is not recognized as a cmdlet, function, operable
> > program, or script file. Verify the term and try again.
> > At line:1 char:5
> > + param <<<<
>
Quote:

> > If you don't invoke the script as a ps1 file, you won't get implicit
> > parameter support. It's hard to help you with this without seeing how
> > you are hosting the runtime.
>
Quote:

> > - Oisin
>
> Actually nothing special in the hosting:
>
> // runspace is created and opened somewhere before...
> using (Pipeline pipeline = runspace.CreatePipeline())
> {
> Command command = new Command(myScript, true);
> // Command command = new Command(pathToTempFileWithScript);
> pipeline.Commands.Add(command);
>
> foreach (WorkflowParameterBinding parameterBinding in parameterBindings)
> {
> command.Parameters.Add(parameterBinding.ParameterName,
> parameterBinding.Value);
> }
>
> pipeline.Invoke();
>
> }
>
> It even works if I save my script to temporary file (commented line), but I
> want the same functionality without saving to file.- Hide quoted text -
>
> - Show quoted text -
Hi Dmitry,

A piece of script executed like this does not qualify to have
parameters - it needs be in an explicit "container," like a function,
or a ps1 script file. Parameter binding like this also works for
binding to native Cmdlets parameters. However...

I'll take an educated guess that you're trying to use a powershell as
a windows workflow activity. I suggest the best way to get this to
work in a friendly way is to dynamically wrap your activity script in
a function declaration, perhaps named after the activityID; this will
keep it unique in your runspace's function namespace (that I presume
you're keeping persistant throughout the workflow, if not, then I
guess that doesn't matter). First, create a Command object -- like
you're currently doing -- and specify that it is script, but wrap it
in a function declaration with named parameters, e.g.

string myScript = @"param([Exception]$ex); ... rest of script
here ...";
string wrappedScript = String.Format("function perform-activity_{0}
{ {1} }", activityID, myScript);


Execute this; next, create another pipeline and specify false to
indicate it's a command (not script) and specify the command text
(e.g. "perform-activity_25") as the name of your previously generated
function. You can now bind your workflow parameters to this command
and execute it.

Hope this gets you going in the right direction, and if you get it
working, I'm sure the group will appreciate some wwf/powershell code
like this ;-)

- Oisin

My System SpecsSystem Spec
Old 09-21-2007   #8 (permalink)
Dmitry Naumov


 
 

Re: Hosting PowerShell & passing parameters to script



"Oisin Grehan" wrote:
Quote:

> On Sep 20, 1:56 am, Dmitry Naumov
> <DmitryNau...@xxxxxx> wrote:
Quote:

> > "Oisin Grehan" wrote:
Quote:

> > > On Sep 19, 1:46 am, Dmitry Naumov
> > > <DmitryNau...@xxxxxx> wrote:
> > > > "John Vottero" wrote:
> >
Quote:

> > > > > "Dmitry Naumov" <Dmitry Nau...@xxxxxx> wrote in message
> > > > >news:1DC6ACB9-3EB6-4D51-861D-BB4960987205@xxxxxx
> > > > > >I am hosting PowerShell in my application. There are number of scripts I
> > > > > >want
> > > > > > to execute, some of them have arguments which can't be passed as a string.
> > > > > > For example:
> >
Quote:

> > > > > > param ([Exception]$e)
> >
Quote:

> > > > > > echo $e.Message
> >
Quote:

> > > > > > $MailMessage = New-Object -typename System.Net.Mail.MailMessage
> > > > > > $MailMessage.From = "someb...@xxxxxx"
> > > > > > $MailMessage.To.Add("someb...@xxxxxx")
> > > > > > $MailMessage.Subject = "Message from PowerShell"
> >
Quote:

> > > > > > $MailMessage.Body += $e.Message + "`n"
> > > > > > $MailMessage.Body += $e.CallStack
> >
Quote:

> > > > > > $smtp = New-Object -typename System.Net.Mail.SmtpClient
> > > > > > $smtp.Host = "domain.com"
> > > > > > $smtp.Port = 25
> > > > > > $smtp.Send($MailMessage)
> >
Quote:

> > > > > > It is a simple script but the point is: I want to pass objects to my
> > > > > > script,
> > > > > > which can't be represented as string.
> >
Quote:

> > > > > > If I run this script from PowerShell console it works fine:
> > > > > > PS D:\> $e = New-Object -typename
> > > > > > System.IO.FileNotFoundException -argument
> > > > > > "Test"
> > > > > > PS D:\> .\script.ps1 $e
> > > > > > Test
> >
Quote:

> > > > > > and email is sent. But when I try to run this script in my application
> > > > > > (through RunspaceInvoke or creating pipeline with command) it fails with:
> >
Quote:

> > > > > > The term 'param' is not recognized as a cmdlet, function, operable
> > > > > > program,
> > > > > > or script file. Verify the term and try again.
> >
Quote:

> > > > > > And I have no idea why it happens.
> >
Quote:

> > > > > Can you post more detail on how you're creating a pipeline with command? It
> > > > > works for me when I create the command line this:
> >
Quote:

> > > > > Pipeline pipe = runSpace.CreatePipeline();
> > > > > Command cmd = new Command("C:\Script.ps1");
> > > > > cmd.Parameters.Add("name", value);
> > > > > pipe.Commands.Add(cmd);
> > > > > pipe.Commands.Add("out-default");
> > > > > pipe.Invoke();
> >
Quote:

> > > > John,
> > > > There is the difference between your sample and mine. You invoke script
> > > > located in file, I try to run script which is located in System.String.- Hide quoted text -
> >
Quote:

> > > > - Show quoted text -
> >
Quote:

> > > Ahh, I misunderstood your original question. Hmm, if you're trying to
> > > invoke that string in an interactive context, param will indeed be
> > > invalid. You can verify this yourself by firing up powershell and
> > > trying to run "param" as a command:
> >
Quote:

> > > PS C:\> param
> > > The term 'param' is not recognized as a cmdlet, function, operable
> > > program, or script file. Verify the term and try again.
> > > At line:1 char:5
> > > + param <<<<
> >
Quote:

> > > If you don't invoke the script as a ps1 file, you won't get implicit
> > > parameter support. It's hard to help you with this without seeing how
> > > you are hosting the runtime.
> >
Quote:

> > > - Oisin
> >
> > Actually nothing special in the hosting:
> >
> > // runspace is created and opened somewhere before...
> > using (Pipeline pipeline = runspace.CreatePipeline())
> > {
> > Command command = new Command(myScript, true);
> > // Command command = new Command(pathToTempFileWithScript);
> > pipeline.Commands.Add(command);
> >
> > foreach (WorkflowParameterBinding parameterBinding in parameterBindings)
> > {
> > command.Parameters.Add(parameterBinding.ParameterName,
> > parameterBinding.Value);
> > }
> >
> > pipeline.Invoke();
> >
> > }
> >
> > It even works if I save my script to temporary file (commented line), but I
> > want the same functionality without saving to file.- Hide quoted text -
> >
> > - Show quoted text -
>
> Hi Dmitry,
>
> A piece of script executed like this does not qualify to have
> parameters - it needs be in an explicit "container," like a function,
> or a ps1 script file. Parameter binding like this also works for
> binding to native Cmdlets parameters. However...
>
> I'll take an educated guess that you're trying to use a powershell as
> a windows workflow activity. I suggest the best way to get this to
> work in a friendly way is to dynamically wrap your activity script in
> a function declaration, perhaps named after the activityID; this will
> keep it unique in your runspace's function namespace (that I presume
> you're keeping persistant throughout the workflow, if not, then I
> guess that doesn't matter). First, create a Command object -- like
> you're currently doing -- and specify that it is script, but wrap it
> in a function declaration with named parameters, e.g.
>
> string myScript = @"param([Exception]$ex); ... rest of script
> here ...";
> string wrappedScript = String.Format("function perform-activity_{0}
> { {1} }", activityID, myScript);
>
>
> Execute this; next, create another pipeline and specify false to
> indicate it's a command (not script) and specify the command text
> (e.g. "perform-activity_25") as the name of your previously generated
> function. You can now bind your workflow parameters to this command
> and execute it.
>
> Hope this gets you going in the right direction, and if you get it
> working, I'm sure the group will appreciate some wwf/powershell code
> like this ;-)
>
> - Oisin
>
>
Oisin,
thank you for great idea. I did some research in this direction (wrapping
script in function), but didn't catch idea that I need two different
pipelines. Thanks a lot.

Now it works perfectly: script parameters are visible in activity designer
as activity properties, their are even can be strong typed and available for
binding. The only thing I am concerned is that Runspace, which I am going to
add to WorkflowRuntime services, will be soiled with lot of "wrapped
scripts". Is there a way to clean up after script execution?
My System SpecsSystem Spec
Old 09-21-2007   #9 (permalink)
Oisin Grehan


 
 

Re: Hosting PowerShell & passing parameters to script

"Dmitry Naumov" wrote:

<snip>
Quote:

> Oisin,
> thank you for great idea. I did some research in this direction (wrapping
> script in function), but didn't catch idea that I need two different
> pipelines. Thanks a lot.
>
> Now it works perfectly: script parameters are visible in activity designer
> as activity properties, their are even can be strong typed and available for
> binding. The only thing I am concerned is that Runspace, which I am going to
> add to WorkflowRuntime services, will be soiled with lot of "wrapped
> scripts". Is there a way to clean up after script execution?
Hi Dmitry,

Glad it's worked out for you - yes, you can clean up by deleting the
function. Functions are stored on the "function" drive, and can be deleted
via remote-item like any other item on a powershell drive. e.g.

remove-item functionerform-activity_26

I think you know enough to work out the rest ;-)

- Oisin / x0n

--
@("PSEventing","PSSharePoint","PowerShellCX","PSMobile") |
% { "http://www.codeplex.com/{0}" -f $_ }


My System SpecsSystem Spec
Old 09-24-2007   #10 (permalink)
Dmitry Naumov


 
 

Re: Hosting PowerShell & passing parameters to script



"Oisin Grehan" wrote:
Quote:

> "Dmitry Naumov" wrote:
>
> <snip>
Quote:

> > Oisin,
> > thank you for great idea. I did some research in this direction (wrapping
> > script in function), but didn't catch idea that I need two different
> > pipelines. Thanks a lot.
> >
> > Now it works perfectly: script parameters are visible in activity designer
> > as activity properties, their are even can be strong typed and available for
> > binding. The only thing I am concerned is that Runspace, which I am going to
> > add to WorkflowRuntime services, will be soiled with lot of "wrapped
> > scripts". Is there a way to clean up after script execution?
>
> Hi Dmitry,
>
> Glad it's worked out for you - yes, you can clean up by deleting the
> function. Functions are stored on the "function" drive, and can be deleted
> via remote-item like any other item on a powershell drive. e.g.
>
> remove-item functionerform-activity_26
>
> I think you know enough to work out the rest ;-)
>
> - Oisin / x0n
>
> --
> @("PSEventing","PSSharePoint","PowerShellCX","PSMobile") |
> % { "http://www.codeplex.com/{0}" -f $_ }
>
>
Oisin,
thanks for your help!
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Passing parameters to a PS script PowerShell
Passing arguments from .BAT to PowerShell script PowerShell
Passing parameters to PS script and escaping spaces PowerShell
passing parameters to an external program using PowerShell variables PowerShell
passing parameters to script (ps1) PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46