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 - Passing parameters to a PS script

Reply
 
Old 07-14-2007   #1 (permalink)
Dan


 
 

Passing parameters to a PS script

Is it possible to pass parameters to a PS script when invoking it from
Command Prompt or another (say VB.net) program?

Thanks,
Dan


My System SpecsSystem Spec
Old 07-14-2007   #2 (permalink)
Brandon Shell


 
 

Re: Passing parameters to a PS script

Yes. -ParamName

In the script you define the params like so

=== Script ===
Param($Param1,$Param2)
write-host $param1
write-host $param1
===========

From the DOS prompt or batch
powershell.exe Path\ScripName -Param1 Hello -Param2 There




"Dan" <dan@discussions.microsoft.com> wrote in message
news:F4BE4862-BC4A-4284-99ED-6B748B0B2713@microsoft.com...
> Is it possible to pass parameters to a PS script when invoking it from
> Command Prompt or another (say VB.net) program?
>
> Thanks,
> Dan
>


My System SpecsSystem Spec
Old 07-14-2007   #3 (permalink)
RichS


 
 

RE: Passing parameters to a PS script

Assume you have a script test.ps1

param([string]$msg = "test")
write-host $msg

you can invoke the script and pass the parameters like this

powershell.exe -command "& c:\scripts\monad\test\test.ps1 'Hello'"

--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Dan" wrote:

> Is it possible to pass parameters to a PS script when invoking it from
> Command Prompt or another (say VB.net) program?
>
> Thanks,
> Dan
>

My System SpecsSystem Spec
Old 07-14-2007   #4 (permalink)
Dan


 
 

RE: Passing parameters to a PS script

Many thanks for the answers, Dan

"Dan" wrote:

> Is it possible to pass parameters to a PS script when invoking it from
> Command Prompt or another (say VB.net) program?
>
> Thanks,
> Dan
>

My System SpecsSystem Spec
Old 07-15-2007   #5 (permalink)
Dan


 
 

RE: Passing parameters to a PS script

Thank you again Brandon and RichS.
Both answers were helpful.

However with set-PSdebug -strict I get the message:

The term 'param' is not recognized as a cmdlet, function, operable program,
or script file. Verify the term and try again.
At E:\iris\scripts\powershell\Parametertest.ps1:7 char:6
+ Param <<<< ($a, $b)

Does this work as intended?

And how can I use get-help to get more details about param?

Thank you very much,
Dan
My System SpecsSystem Spec
Old 07-15-2007   #6 (permalink)
Roman Kuzmin


 
 

Re: Passing parameters to a PS script

Is your script like this (Perl habits, perhaps?)?

Set-PSdebug -strict
param (...)

Then it is not correct. 'param' (if exists) must be the first statement. So,
use this:

param (...)
Set-PSdebug -strict

--
Thanks,
Roman Kuzmin
PowerShellFar and FarNET: http://code.google.com/p/farnet/


"Dan" <dan@discussions.microsoft.com> wrote in message
news:00E23327-3F12-4830-86B8-29D87B1BE433@microsoft.com...
> Thank you again Brandon and RichS.
> Both answers were helpful.
>
> However with set-PSdebug -strict I get the message:
>
> The term 'param' is not recognized as a cmdlet, function, operable
> program,
> or script file. Verify the term and try again.
> At E:\iris\scripts\powershell\Parametertest.ps1:7 char:6
> + Param <<<< ($a, $b)
>
> Does this work as intended?
>
> And how can I use get-help to get more details about param?
>
> Thank you very much,
> Dan



My System SpecsSystem Spec
Old 07-17-2007   #7 (permalink)
Dan


 
 

Re: Passing parameters to a PS script from VB 2005

I need now to invoke a PS script with parameters from a VB 2005 program.
I tried both versioned mentioned above:

Shell("powershell.exe e:\test.ps1 -param1 pathA -param2 computerB ")

Shell(""powershell.exe -command "& e:\test.ps1 -param1 pathA -param2
computerB """).

Both are accepted syntactically, both execute the script but without passing
the parameters.

What am I doing wrong?

Thank you very much,
Dan

My System SpecsSystem Spec
Old 07-17-2007   #8 (permalink)
Shay Levi


 
 

Re: Passing parameters to a PS script from VB 2005

This one works for me

Shell("powershell.exe c:\scripts\test.ps1 -param1 pathA -param2 computerB")



Shay
http://www.scriptolog.blogspot.com

> I need now to invoke a PS script with parameters from a VB 2005
> program. I tried both versioned mentioned above:
>
> Shell("powershell.exe e:\test.ps1 -param1 pathA -param2 computerB ")
>
> Shell(""powershell.exe -command "& e:\test.ps1 -param1 pathA -param2
> computerB """).
>
> Both are accepted syntactically, both execute the script but without
> passing the parameters.
>
> What am I doing wrong?
>
> Thank you very much,
> Dan



My System SpecsSystem Spec
Old 07-17-2007   #9 (permalink)
Shay Levi


 
 

Re: Passing parameters to a PS script from VB 2005

Consider the following. create a VBS file and a PS1 test file then execute
the VBS file.
On my machine, both run successfully.

###### vbs file ####
Set oShell = CreateObject("WScript.Shell")

'Option 1
oshell.run "powershell.exe c:\test.ps1 -param1 pathA -param2 computerB"

'Option 2
oshell.run "powershell.exe -command & {c:\test.ps1 -param1 pathA -param2
computerB}"


###### c:\test.ps1 ####
param($param1,$param2)

$param1
$param2


###### PowerShell output ####
pathA
computerB


Shay
http://www.scriptolog.blogspot.com



> I need now to invoke a PS script with parameters from a VB 2005
> program. I tried both versioned mentioned above:
>
> Shell("powershell.exe e:\test.ps1 -param1 pathA -param2 computerB ")
>
> Shell(""powershell.exe -command "& e:\test.ps1 -param1 pathA -param2
> computerB """).
>
> Both are accepted syntactically, both execute the script but without
> passing the parameters.
>
> What am I doing wrong?
>
> Thank you very much,
> Dan



My System SpecsSystem Spec
Old 07-17-2007   #10 (permalink)
John Vottero


 
 

Re: Passing parameters to a PS script from VB 2005

"Dan" <dan@discussions.microsoft.com> wrote in message
news:BA41996C-134A-47AF-A0A3-6377198ABBA1@microsoft.com...
>I need now to invoke a PS script with parameters from a VB 2005 program.
> I tried both versioned mentioned above:
>
> Shell("powershell.exe e:\test.ps1 -param1 pathA -param2 computerB ")
>
> Shell(""powershell.exe -command "& e:\test.ps1 -param1 pathA -param2
> computerB """).
>
> Both are accepted syntactically, both execute the script but without
> passing
> the parameters.
>


You don't need to create a new process and run powershell.exe. Just do
this:

using (RunspaceInvoke ri = new RunspaceInvoke())
{
Collection<PSObject> results =
ri.Invoke("e:\test.ps1 -param1 pathA -param2 computerB");
}

Sorry about the C#, it shouldn't be difficult to convert to VB.

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Help with PS wretched way of passing parameters to executables PowerShell
Hosting PowerShell & passing parameters to script PowerShell
Passing parameters to PS script and escaping spaces PowerShell
passing [switch] parameters 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