![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
| | #6 (permalink) |
| | 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 Specs![]() |
| | #7 (permalink) |
| | 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 Specs![]() |
| | #8 (permalink) |
| | 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 Specs![]() |
| | #9 (permalink) |
| | 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 Specs![]() |
| | #10 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||