![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | RE: Script switch parameter as variable Try this: param([switch]$overwrite = $false) Quote: > if ($overwrite) {"Found overwrite."} "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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||