Windows Vista Forums

Passing parameters to a PS script
  1. #1


    Dan Guest

    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

  2. #2


    Brandon Shell Guest

    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

  3. #3


    RichS Guest

    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

  4. #4


    Dan Guest

    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

  5. #5


    Dan Guest

    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

  6. #6


    Roman Kuzmin Guest

    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

  7. #7


    Dan Guest

    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

  8. #8


    Shay Levi Guest

    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

  9. #9


    Shay Levi Guest

    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

  10. #10


    John Vottero Guest

    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

Page 1 of 2 12 LastLast
Passing parameters to a PS script problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing Parameters to Ps1 files james PowerShell 2 11 Jan 2010
Hosting PowerShell & passing parameters to script Dmitry Naumov PowerShell 9 24 Sep 2007
Passing parameters to PS script and escaping spaces James PowerShell 2 14 Aug 2007
passing [switch] parameters Jeff PowerShell 4 28 Jul 2007
passing parameters to script (ps1) IT Staff PowerShell 1 19 Oct 2006