![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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 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 Specs![]() |
| | #2 (permalink) |
| | Re: Passing parameters to a cmdlet in a variable Alastair Smith wrote: Quote: > 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 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 Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Passing parameters to a PS script | PowerShell | |||
| Passing dynamic number of parameters to a function as a variable | PowerShell | |||
| passing [switch] parameters | PowerShell | |||
| Help with foreach-object and passing parameters | PowerShell | |||
| passing parameters to script (ps1) | PowerShell | |||