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