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 cmdlet in a variable

Reply
 
Old 02-08-2008   #1 (permalink)
Alastair Smith


 
 

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
Old 02-08-2008   #2 (permalink)
Marco Shaw [MVP]


 
 

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
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
Reply

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


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