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 - Script switch parameter as variable

Reply
 
Old 01-26-2009   #1 (permalink)
ewannema


 
 

Script switch parameter as variable

I am trying to run a script using saved parameters and the switch
parameters do not seem to be interpreted correctly when they are
strings. Any ideas for a (good) way to do this?

The contents of script1.ps1
param([switch]$overwrite)
if ($overwrite) {"Found overwrite."}

Here is my simplistic testing. I would really like to see the
overwrite switch detected when used with a variable.

PS C:\> .\script1.ps1 -overwrite
Found overwrite.
PS C:\> .\script1.ps1 $argument
<No output>
PS C:\> .\script1.ps1 "-overwrite"
<No output>

My System SpecsSystem Spec
Old 01-27-2009   #2 (permalink)
Rob Campbell


 
 

RE: Script switch parameter as variable

Try this:

param([switch]$overwrite = $false)
Quote:

> if ($overwrite) {"Found overwrite."}
I know it looks strange, but it seems to work.


"ewannema@xxxxxx" wrote:
Quote:

> I am trying to run a script using saved parameters and the switch
> parameters do not seem to be interpreted correctly when they are
> strings. Any ideas for a (good) way to do this?
>
> The contents of script1.ps1
> param([switch]$overwrite)
> if ($overwrite) {"Found overwrite."}
>
> Here is my simplistic testing. I would really like to see the
> overwrite switch detected when used with a variable.
>
> PS C:\> .\script1.ps1 -overwrite
> Found overwrite.
> PS C:\> .\script1.ps1 $argument
> <No output>
> PS C:\> .\script1.ps1 "-overwrite"
> <No output>
>
My System SpecsSystem Spec
Old 01-27-2009   #3 (permalink)
Shay Levy [MVP]


 
 

Re: Script switch parameter as variable

Hello ewannema@xxxxxx,

Your first exmaple is the way to use switch parameters, ask if the param
exists (which is equivalent to $true):

param([switch]$overwrite)
if ($overwrite) {"Found overwrite."}


To assign $flase value, use:

.\script1.ps1 -overwrite:$false





---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar

Quote:

> I am trying to run a script using saved parameters and the switch
> parameters do not seem to be interpreted correctly when they are
> strings. Any ideas for a (good) way to do this?
>
> The contents of script1.ps1
> param([switch]$overwrite)
> if ($overwrite) {"Found overwrite."}
> Here is my simplistic testing. I would really like to see the
> overwrite switch detected when used with a variable.
>
> PS C:\> .\script1.ps1 -overwrite
> Found overwrite.
> PS C:\> .\script1.ps1 $argument
> <No output>
> PS C:\> .\script1.ps1 "-overwrite"
> <No output>

My System SpecsSystem Spec
Old 01-27-2009   #4 (permalink)
Rob Campbell


 
 

Re: Script switch parameter as variable

If you set the param default to $false, then it behaves like a switch
parameter to the script, which seems to be what he's after.

If the parameter name is included when the script is run, then a switch
object set to $true will be passed to the script, otherwise the script will
create one, set to $false.

"Shay Levy [MVP]" wrote:
Quote:

> Hello ewannema@xxxxxx,
>
> Your first exmaple is the way to use switch parameters, ask if the param
> exists (which is equivalent to $true):
>
> param([switch]$overwrite)
> if ($overwrite) {"Found overwrite."}
>
>
> To assign $flase value, use:
>
> .\script1.ps1 -overwrite:$false
>
>
>
>
>
> ---
> Shay Levy
> Windows PowerShell MVP
> http://blogs.microsoft.co.il/blogs/ScriptFanatic
> PowerShell Toolbar: http://tinyurl.com/PSToolbar
>
>
Quote:

> > I am trying to run a script using saved parameters and the switch
> > parameters do not seem to be interpreted correctly when they are
> > strings. Any ideas for a (good) way to do this?
> >
> > The contents of script1.ps1
> > param([switch]$overwrite)
> > if ($overwrite) {"Found overwrite."}
> > Here is my simplistic testing. I would really like to see the
> > overwrite switch detected when used with a variable.
> >
> > PS C:\> .\script1.ps1 -overwrite
> > Found overwrite.
> > PS C:\> .\script1.ps1 $argument
> > <No output>
> > PS C:\> .\script1.ps1 "-overwrite"
> > <No output>
>
>
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
using foreach-object as a parameter for another script PowerShell
return variable through script in script VB Script
Script parameter problem PowerShell
[SUGGESTION] New switch parameter for Get-WmiObject PowerShell
Potenial issue with the get-variable -scope parameter 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