Windows Vista Forums

Passing parameters to a cmdlet in a variable
  1. #1


    Alastair Smith Guest

    Passing parameters to a cmdlet in a variable

    I'm new to Powershell (although I have some knowledge of this
    scripting in BaSH and PHP), and am trying to write a copy script that
    passes some arguments to copy-item according to the arguments provided
    to the script. The arguments passed to copy-item will always be -
    force and -recurse at a minimum, and -verbose is passed to copy-item
    by default. However, if the user passes the -quiet option to the
    script, the -verbose option will not be passed to copy-item. The code
    I have is as follows:

    if (($args -contains "-quiet") -or ($args -contains "-q")) {
    $vbIndex = $copyArgs.IndexOf("-verbose")
    $copyArgs = $copyArgs.Remove($vbIndex, 8)

    }



    copy <srcpath> <destpath> $copyArgs

    However, when I run this script, I get an error back from PS stating
    that "A parameter cannot be found that matches parameter name '-
    recurse -force -verbose'." Evidently, PS is parsing $copyArgs as a
    single argument, rather than the individual arguments -recurse, etc.

    Many thanks in advance

    Alastair

      My System SpecsSystem Spec

  2. #2


    Marco Shaw [MVP] Guest

    Re: Passing parameters to a cmdlet in a variable

    Alastair Smith wrote:

    > I'm new to Powershell (although I have some knowledge of this
    > scripting in BaSH and PHP), and am trying to write a copy script that
    > passes some arguments to copy-item according to the arguments provided
    > to the script. The arguments passed to copy-item will always be -
    > force and -recurse at a minimum, and -verbose is passed to copy-item
    > by default. However, if the user passes the -quiet option to the
    > script, the -verbose option will not be passed to copy-item. The code
    > I have is as follows:
    >
    > if (($args -contains "-quiet") -or ($args -contains "-q")) {
    > $vbIndex = $copyArgs.IndexOf("-verbose")
    > $copyArgs = $copyArgs.Remove($vbIndex, 8)
    >
    > }
    >
    > copy <srcpath> <destpath> $copyArgs
    >
    > However, when I run this script, I get an error back from PS stating
    > that "A parameter cannot be found that matches parameter name '-
    > recurse -force -verbose'." Evidently, PS is parsing $copyArgs as a
    > single argument, rather than the individual arguments -recurse, etc.
    >
    > Many thanks in advance
    >
    > Alastair
    Here's an example that might help you. I'll play with this a bit more
    maybe later today.

    function write-host2 {
    param([switch]$plain,[string]$object)
    if($plain){
    write-host -object $object
    }
    else{
    write-host -object $object -foregroundcolor red
    }
    }

    Are you using v1 or the v2 CTP by any chance? In v2, what you're
    trying to accomplish should work much better using "scriptcmdlets".

    Marco

    --
    Microsoft MVP - Windows PowerShell
    http://www.microsoft.com/mvp

    PowerGadgets MVP
    http://www.powergadgets.com/mvp

    Blog:
    http://marcoshaw.blogspot.com

      My System SpecsSystem Spec

Passing parameters to a cmdlet in a variable problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing Parameters to Ps1 files james PowerShell 2 11 Jan 2010
Passing parameters to a PS script Dan PowerShell 11 11 Nov 2009
Passing dynamic number of parameters to a function as a variable Robin Moffatt PowerShell 10 30 Jan 2008
passing [switch] parameters Jeff PowerShell 4 28 Jul 2007
passing parameters to script (ps1) IT Staff PowerShell 1 19 Oct 2006